如何进行Django单元测试

如何进行Django单元测试

Django的单元测试使用python的unittest模块,这个模块使用基于类的方法来定义测试。类名为django.test.TestCase,继承于python的unittest.TestCase。

 

执行目录下所有的测试(所有的test*.py文件):运行测试的时候,测试程序会在所有以test开头的文件中查找所有的test cases(inittest.TestCase的子类),自动建立测试集然后运行测试。

$ python manage.py test

执行animals项目下tests包里的测试:

$ python manage.py testanimals.tests

执行animals项目里的test测试:

$ python manage.py testanimals

单独执行某个test case:

$ python manage.py testanimals.tests.AnimalTestCase

单独执行某个测试方法:

$ python manage.py testanimals.tests.AnimalTestCase.test_animals_can_speak

为测试文件提供路径:

$ python manage.py testanimals/

通配测试文件名:

$ python manage.py test--pattern="tests_*.py"

启用warnings提醒:

$ python -Wall manage.py test

posted @   冰滴的眼泪  阅读(5894)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示