Pytest(2)使用和呼叫方法
阿新 • • 發佈:2021-06-30
Pytest執行用例規則
Pytest在命令列中支援多種方式來執行和選擇測試用例
1.對某個目錄下所有的用例
pytest
2.對模組中進行測試
pytest test_mod.py
3.對資料夾進行測試
pytest testing
4.通過標記來進行測試
pytest -m slow
這種方式會執行所有通過裝飾器@pytest.mark.slow進行裝飾的測試用例。
5.通過關鍵字表達式來進行測試
pytest -k "MyClass and not method"
這種方式會執行檔名,類名以及函式名與給定的字串表示式相匹配的測試用例。 上面的用例會執行TestMyClass.test_something
6.通過節點id來測試
每個被選中的測試用例都會被分配一個唯一的nodeid,它由模組檔名和以下說明符組成:引數化的類名、函式名和引數,用::分隔。
# 測試test_1.py檔案下的TestClass類下的test_method方法 pytest test_1.py::TestClass::test_method # test1.py檔案 class TestClass(object): def test_one(self): x = "hello" assert 'h' inx def test_method(self): # 測試的就是這個方法 x = "hello" assert 'h' in x
7.從包中執行測試
pytest --pyargs pkg.testing
這將會匯入pkg.testing並使用其檔案系統位置來查詢和執行測試。
8.-q 簡單列印,只打印測試用例的執行結果
pytest -q test_1.py
9.-s 詳細列印
pytest -s test_1.py
10.-x 遇到錯誤時停止測試
pytest -x test_1.py
11. --maxfail=num,測試在第1(N)次測試失敗後停止
pytest --maxfail=2 test_1.py