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 =====================
可以看出 第二個方法沒有通過測試
剛學到的,感覺太基礎?後期陸續更新。。。