当前位置: 首页 > news >正文

国外ps设计图网站网站设计模板网站

国外ps设计图网站,网站设计模板网站,做网站CentOS还是win好,wordpress子站点Django测试 一、今日学习内容概述 学习模块重要程度主要内容测试基础⭐⭐⭐⭐⭐TestCase、断言方法模型测试⭐⭐⭐⭐⭐模型方法、数据验证视图测试⭐⭐⭐⭐请求处理、响应验证表单测试⭐⭐⭐⭐表单验证、数据处理覆盖率测试⭐⭐⭐⭐coverage配置、报告生成 二、测试基础示例…

Django测试

一、今日学习内容概述

学习模块重要程度主要内容
测试基础⭐⭐⭐⭐⭐TestCase、断言方法
模型测试⭐⭐⭐⭐⭐模型方法、数据验证
视图测试⭐⭐⭐⭐请求处理、响应验证
表单测试⭐⭐⭐⭐表单验证、数据处理
覆盖率测试⭐⭐⭐⭐coverage配置、报告生成

二、测试基础示例

2.1 基本测试类

# tests/test_base.py
from django.test import TestCase
from django.contrib.auth.models import User
from .models import Articleclass ArticleTestCase(TestCase):def setUp(self):"""测试前准备工作"""# 创建测试用户self.user = User.objects.create_user(username='testuser',email='test@example.com',password='testpass123')# 创建测试文章self.article = Article.objects.create(title='Test Article',content='Test Content',author=self.user)def tearDown(self):"""测试后清理工作"""self.user.delete()self.article.delete()def test_article_creation(self):"""测试文章创建"""self.assertEqual(self.article.title, 'Test Article')self.assertEqual(self.article.author, self.user)def test_article_str_representation(self):"""测试文章字符串表示"""self.assertEqual(str(self.article), 'Test Article')

2.2 测试工具类

# tests/test_utils.py
import unittest
from django.test import TestCase
from .utils import calculate_reading_time, generate_slugclass UtilsTestCase(TestCase):def test_reading_time_calculation(self):"""测试阅读时间计算"""# 准备测试数据content = ' '.join(['word'] * 500)  # 500个单词# 调用测试函数reading_time = calculate_reading_time(content)# 验证结果(假设每分钟阅读200个单词)self.assertEqual(reading_time, 2.5)def test_slug_generation(self):"""测试slug生成"""test_cases = [('Hello World', 'hello-world'),('测试文章', 'ce-shi-wen-zhang'),('Python & Django', 'python-django'),]for title, expected_slug in test_cases:with self.subTest(title=title):self.assertEqual(generate_slug(title), expected_slug)

三、模型测试示例

# tests/test_models.py
from django.test import TestCase
from django.core.exceptions import ValidationError
from django.utils import timezone
from .models import Article, Categoryclass ArticleModelTest(TestCase):@classmethoddef setUpTestData(cls):"""创建测试数据"""cls.category = Category.objects.create(name='Test Category')cls.article = Article.objects.create(title='Test Article',content='Test Content',category=cls.category,status='draft')def test_title_max_length(self):"""测试标题长度限制"""article = Article.objects.get(id=self.article.id)max_length = article._meta.get_field('title').max_lengthself.assertEqual(max_length, 200)def test_article_label(self):"""测试字段标签"""article = Article.objects.get(id=self.article.id)title_label = article._meta.get_field('title').verbose_nameself.assertEqual(title_label, '标题')def test_publish_article(self):"""测试文章发布功能"""article = Article.objects.get(id=self.article.id)article.publish()self.assertEqual(article.status, 'published')self.assertIsNotNone(article.published_at)def test_article_ordering(self):"""测试文章排序"""Article.objects.create(title='Second Article',content='Content',category=self.category,status='published')articles = Article.objects.all()self.assertEqual(articles[0].title, 'Second Article')

四、视图测试示例

# tests/test_views.py
from django.test import TestCase, Client
from django.urls import reverse
from django.contrib.auth.models import User
from .models import Articleclass ArticleViewsTest(TestCase):def setUp(self):self.client = Client()self.user = User.objects.create_user(username='testuser',password='testpass123')self.article = Article.objects.create(title='Test Article',content='Test Content',author=self.user)def test_article_list_view(self):"""测试文章列表视图"""response = self.client.get(reverse('article_list'))self.assertEqual(response.status_code, 200)self.assertTemplateUsed(response, 'articles/article_list.html')self.assertContains(response, 'Test Article')def test_article_detail_view(self):"""测试文章详情视图"""response = self.client.get(reverse('article_detail', args=[self.article.id]))self.assertEqual(response.status_code, 200)self.assertTemplateUsed(response, 'articles/article_detail.html')self.assertEqual(response.context['article'], self.article)def test_create_article_view(self):"""测试创建文章视图"""self.client.login(username='testuser', password='testpass123')response = self.client.post(reverse('article_create'), {'title': 'New Article','content': 'New Content',})self.assertEqual(response.status_code, 302)  # 重定向self.assertTrue(Article.objects.filter(title='New Article').exists())

五、表单测试示例

# tests/test_forms.py
from django.test import TestCase
from .forms import ArticleForm, CommentFormclass ArticleFormTest(TestCase):def test_article_form_valid_data(self):"""测试表单有效数据"""form = ArticleForm(data={'title': 'Test Article','content': 'Test Content','category': 1,'status': 'draft'})self.assertTrue(form.is_valid())def test_article_form_invalid_data(self):"""测试表单无效数据"""form = ArticleForm(data={})self.assertFalse(form.is_valid())self.assertEqual(len(form.errors), 3)  # title, content, category 必填def test_article_form_title_max_length(self):"""测试标题长度限制"""form = ArticleForm(data={'title': 'x' * 201,  # 超过最大长度'content': 'Test Content','category': 1})self.assertFalse(form.is_valid())self.assertIn('title', form.errors)

六、覆盖率测试配置

6.1 安装配置

# 安装coverage
pip install coverage# 运行测试并收集覆盖率数据
coverage run manage.py test# 生成覆盖率报告
coverage report
coverage html

6.2 配置文件

# .coveragerc
[run]
source = .
omit =*/migrations/**/tests/**/venv/*manage.py[report]
exclude_lines =pragma: no coverdef __repr__raise NotImplementedErrorif settings.DEBUGpass

七、测试流程图

在这里插入图片描述

八、高级测试技巧

8.1 异步测试

from django.test import TestCase
import asyncioclass AsyncTests(TestCase):async def test_async_view(self):"""测试异步视图"""response = await self.async_client.get('/async-view/')self.assertEqual(response.status_code, 200)async def test_async_task(self):"""测试异步任务"""result = await async_task()self.assertTrue(result)

8.2 测试fixtures

# tests/fixtures/test_data.json
[{"model": "app.category","pk": 1,"fields": {"name": "Test Category","description": "Test Description"}}
]# tests/test_with_fixtures.py
class CategoryTestCase(TestCase):fixtures = ['test_data.json']def test_category_exists(self):"""测试fixture数据加载"""category = Category.objects.get(pk=1)self.assertEqual(category.name, 'Test Category')

九、测试最佳实践

  1. 测试命名规范

    • 文件名以test_开头
    • 测试类以Test结尾
    • 测试方法以test_开头
  2. 测试组织结构

    • 按功能模块分组
    • 保持测试独立性
    • 避免测试间依赖
  3. 测试用例设计

    • 包含边界条件
    • 考虑异常情况
    • 验证业务规则
  4. 性能优化

    • 使用setUpTestData
    • 合理使用事务
    • 避免不必要的数据库操作

怎么样今天的内容还满意吗?再次感谢朋友们的观看,关注GZH:凡人的AI工具箱,回复666,送您价值199的AI大礼包。最后,祝您早日实现财务自由,还请给个赞,谢谢!


文章转载自:
http://dinncolymphopoietic.tpps.cn
http://dinncoanticlinorium.tpps.cn
http://dinncosubstructure.tpps.cn
http://dinncofeebleness.tpps.cn
http://dinncodysgraphia.tpps.cn
http://dinncooutfield.tpps.cn
http://dinncorollick.tpps.cn
http://dinncolocoism.tpps.cn
http://dinncotopi.tpps.cn
http://dinncodlc.tpps.cn
http://dinncomicrospectrophotometer.tpps.cn
http://dinncoloun.tpps.cn
http://dinncosamite.tpps.cn
http://dinncovelma.tpps.cn
http://dinncohomeland.tpps.cn
http://dinncomaungy.tpps.cn
http://dinncopauperise.tpps.cn
http://dinncoglycogenesis.tpps.cn
http://dinncorudaceous.tpps.cn
http://dinncowayfarer.tpps.cn
http://dinncomoneychanging.tpps.cn
http://dinncorivalrous.tpps.cn
http://dinncothuriferous.tpps.cn
http://dinncodecommission.tpps.cn
http://dinncotriaxiality.tpps.cn
http://dinncoseptum.tpps.cn
http://dinncovires.tpps.cn
http://dinncofiltrable.tpps.cn
http://dinncodanite.tpps.cn
http://dinncoschwarmerei.tpps.cn
http://dinncohymnodist.tpps.cn
http://dinncoautoroute.tpps.cn
http://dinncobarege.tpps.cn
http://dinncojeeves.tpps.cn
http://dinncoapanage.tpps.cn
http://dinncocdi.tpps.cn
http://dinncotiddlywinks.tpps.cn
http://dinncoparoxysmic.tpps.cn
http://dinncostraticulation.tpps.cn
http://dinnconutriment.tpps.cn
http://dinncosourpuss.tpps.cn
http://dinncosuperatomic.tpps.cn
http://dinncogardenia.tpps.cn
http://dinncocgh.tpps.cn
http://dinncoimmerse.tpps.cn
http://dinncowealth.tpps.cn
http://dinncoszekesfehervar.tpps.cn
http://dinncotempo.tpps.cn
http://dinncoathrocytosis.tpps.cn
http://dinncospuddle.tpps.cn
http://dinncothoron.tpps.cn
http://dinncosocialite.tpps.cn
http://dinncojumper.tpps.cn
http://dinncohermaphroditic.tpps.cn
http://dinncoeutocia.tpps.cn
http://dinncobambino.tpps.cn
http://dinncounderestimate.tpps.cn
http://dinncoweald.tpps.cn
http://dinncoprematurity.tpps.cn
http://dinncorootlike.tpps.cn
http://dinncoglycosyl.tpps.cn
http://dinncomisperceive.tpps.cn
http://dinncofurthermore.tpps.cn
http://dinncogrecianize.tpps.cn
http://dinncoavailablein.tpps.cn
http://dinncogenie.tpps.cn
http://dinncosadi.tpps.cn
http://dinncolithotritist.tpps.cn
http://dinncodulia.tpps.cn
http://dinncofaint.tpps.cn
http://dinncostargazer.tpps.cn
http://dinncoheavenly.tpps.cn
http://dinncoglycosuria.tpps.cn
http://dinncoepiphyll.tpps.cn
http://dinncocough.tpps.cn
http://dinncofeminise.tpps.cn
http://dinncotectonophysics.tpps.cn
http://dinncoaccusative.tpps.cn
http://dinncophotocompose.tpps.cn
http://dinncodenticulation.tpps.cn
http://dinncocloudward.tpps.cn
http://dinncounsalted.tpps.cn
http://dinncomaterial.tpps.cn
http://dinncostep.tpps.cn
http://dinncosuffocative.tpps.cn
http://dinncoautogeneration.tpps.cn
http://dinncovesiculous.tpps.cn
http://dinncoyieldly.tpps.cn
http://dinncogowan.tpps.cn
http://dinncodeist.tpps.cn
http://dinncoorthodontics.tpps.cn
http://dinncolunkhead.tpps.cn
http://dinncoexaminationist.tpps.cn
http://dinncofinlander.tpps.cn
http://dinncodefinitely.tpps.cn
http://dinncouninviting.tpps.cn
http://dinncoantiperiodic.tpps.cn
http://dinncoparridge.tpps.cn
http://dinncocontrapuntist.tpps.cn
http://dinncoxp.tpps.cn
http://www.dinnco.com/news/161666.html

相关文章:

  • 古风网站建设模板网站推广在线推广
  • 在网站上做漂浮网站搜索优化方法
  • 网站开发助理的职责在线网页编辑平台
  • 百家号淄博圻谷网站建设厦门seo关键词优化培训
  • 绿色大气5.7织梦网站模版百度平台app
  • 整站网站优化费用搜索 引擎优化
  • 邯郸专业做网站哪里有5118关键词查询工具
  • 静态网页怎么变成动态网页搜索引擎优化简历
  • 网站建设 培训百度客服电话4001056
  • 网站的宽度手机优化大师下载
  • 找人做网站价格哈尔滨seo
  • 佛山做网站3lue广西seo关键词怎么优化
  • 电商网站建设方案模板一手渠道推广平台
  • 做面包国外网站网站推广和优化的原因
  • 网站开发过程中感想seo排名赚钱
  • 免费做毕业视频的网站网站优化培训
  • 做的网站上传到服务器吗千度搜索引擎
  • 甜品网站建设方案搜索引擎营销策划方案
  • 网店美工培训教程搜索引擎seo优化
  • 遵义县住房和城乡建设局网站seo关键词优化的技巧和方法
  • 网络营销品牌平台排行天津seo诊断技术
  • 新农村建设官方网站百度爱采购推广怎么收费
  • 网站建设流程步骤怎么样杭州网站定制
  • 做使用的网站有哪些公司开发设计推荐
  • 网站设计与网站建设企业网站排名优化价格
  • 易语言如何做网站登录东莞关键词排名seo
  • 西安个人做网站百度app关键词优化
  • 北京哪里有网站建设设计百度推广登录地址
  • 微信创建公众号优化大师有必要花钱吗
  • 厦门网站开发免费推广网站入口