pytest-書寫規則及案例(一)

1. 安裝

pip install pytest-django

2. 測試類

  1. 測試類必須以TestXxx形式書寫,即以Test開頭,不能帶有init方法

  2. 測試文件以test_*.py開頭(以*_test.py結尾也可以)

  3. 測試函數以test_開頭

  4. 斷言使用基本的 assert 即可

  5. 運行測試,在測試文件路徑下:

    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 =====================

可以看出 第二個方法沒有通過測試

剛學到的,感覺太基礎?後期陸續更新。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章