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

