一谒出、pytest概述
官網:https://docs.pytest.org/en/latest/contents.html
二、安裝
- pip install pytest
三夕春、使用規(guī)則
文件名:必須以test*.py或者*test.py命名
測試類:必須以Test開頭,不能有__init__
測試函數:必須以test開頭蚊夫。
執(zhí)行順序通unittest一樣也是根據ASCII碼執(zhí)行的
四镣陕、用例執(zhí)行
-v: 輸出詳細的用例執(zhí)行信息
-s: 輸出更加詳細的調試信息
-m: 跟標記:執(zhí)行含有改mark的測試用例
-k: 跟關鍵字;執(zhí)行含有關鍵字的測試用例
五妙蔗、用例跳過
class Test():
@pytest.mark.skip(reason="就是不想執(zhí)行,跳過")
def test_001(self):
print("第一個測試用例")
@pytest.mark.skipif(1 == 1, reason="判斷條件為true,則跳過")
def test_002(self):
print("第二個測試用例")
def test_003(self):
pytest.xfail(reason="執(zhí)行失敗") #xfail 是在用例中用的疆瑰,注意和skip用法的區(qū)別
print("第三個測試用例")
def test_004(self):
注意:xfail 是用在測試用例中眉反,不是以裝飾器的方式使用
-
執(zhí)行結果
用例跳過執(zhí)行結果.png
六昙啄、參數化
- parameterized
官網:https://pypi.org/project/parameterized/ - 使用方式同unittest《http://www.reibang.com/p/1b5e790fdaaf》
七、測試報告
pytest-html
- 安裝:pip install pytest-html
- 執(zhí)行:pytest -v -s pytest-1.py --html=./report.html
-
執(zhí)行結果:
測試報告.png
八寸五、常用插件
- pytest-selenium(集成selenium)
- pytest-html(完美html測試報告生成)
- pytest-rerunfailures(失敗case重復執(zhí)行)
- pytest-xdist(多CPU分發(fā))
九:完整示例代碼
- 待更新.....