1. 安装
pip install pytest-django
2. 测试类
-
测试类必须以TestXxx形式书写,即以Test开头,不能带有init方法
-
测试文件以
test_*.py
开头(以*_test.py
结尾也可以) -
测试函数以test_开头
-
断言使用基本的 assert 即可
-
运行测试,在测试文件路径下:
pytest tests.py
案例:
class TestClass:
def test_one(self):
x = "this"
assert "h" in x
def test_two(self):
x = "hello"
assert x == "hi"
运行: pytest tests.py
输出:
__________________ TestClass.test_two ________________
self = <apps.courses.tests.TestClass object at 0x7f260a1e12e8>
def test_two(self):
x = "hello"
> assert x == "hi"
E AssertionError: assert 'hello' == 'hi'
E - hello
E + hi
tests.py:17: AssertionError
================ 1 failed, 1 passed in 0.14 seconds =====================
可以看出 第二个方法没有通过测试
刚学到的,感觉太基础?后期陆续更新。。。