Pytest提供了多樣化的方式來運(yùn)行和調(diào)試測試用例,本文介紹一些比較常用的方式。
pytest是如何識別測試用例的呢?它默認(rèn)使用檢查以test_.py 或test.py命名的文件名,在文件內(nèi)部查找以test打頭的方法或函數(shù),并執(zhí)行它們。
下面是Pytest的官網(wǎng)的一些測試?yán)樱疚挠盟鼇磉M(jìn)行不同的Pytest運(yùn)行。
首先安裝好Python和Pytest,新建一個test_pytest_markers.py,在Terminal里面可以嘗試以下不同的運(yùn)行方式。
#Below are test_pytest_markers.py# content of test_server.pyimportpytest@pytest.mark.webtestdeftest_send_http():pass# perform some webtest test for your appdeftest_something_quick():passdeftest_another():passclassTestClass:deftest_method(self):pass
1. Pytest Marker 機(jī)制
對于Pytest的測試用例,可以在每一個測試用例加一個marker,比如pytest運(yùn)行的時就只運(yùn)行帶有該marker的測試用例,比如下面的@pytest.mark.webtest。
在我們的實際項目中,我們通常把為每個feature建立相應(yīng)的marker,用于測試時進(jìn)行不同條件的挑選。
Pytest還支持一個測試用例有多個marker,這個在后續(xù)會把相應(yīng)的實踐加入進(jìn)來。
測試結(jié)果如下,只挑選了測試用例帶有webtest marker的運(yùn)行。
C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -m "webtest" test_pytest_markers.py============================= test session starts =============================platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.execachedir: .cacherootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:collected 4 items test_pytest_markers.py::test_send_http PASSED============================= 3 tests deselected ================================================= 1 passed, 3 deselected in 0.04 seconds ====================
同樣的,也可以只挑選了測試用例中不帶有webtest marker的運(yùn)行。
C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -m"not webtest"test_pytest_markers.py============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected4items test_pytest_markers.py::test_something_quick PASSEDtest_pytest_markers.py::test_another PASSEDtest_pytest_markers.py::TestClass::test_method PASSED=============================1tests deselected ==============================
2. 選擇運(yùn)行特定的某個測試用例
你可以按照某個測試用例的的模塊,類或函數(shù)來選擇你要運(yùn)行的case,比如下面的方式就適合一開始在調(diào)試單個測試用例的時候。
C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v test_pytest_markers.py::TestClass::test_method============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected5items test_pytest_markers.py::TestClass::test_method PASSED==========================1passedin0.03seconds ===========================
3. 選擇運(yùn)行特定的某個類
比如你想單獨(dú)運(yùn)行下某個類的所有測試用例,可以按照如下操作。
C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v test_pytest_markers.py::TestClass============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected4items test_pytest_markers.py::TestClass::test_method PASSED==========================1passedin0.04seconds ===========================
4 多種組合
比如下面,你可以進(jìn)行以上兩種方式的組合挑選你需要運(yùn)行的測試用例。
C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v test_pytest_markers.py::TestClass test_pytest_markers.py::test_send_http============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected8items test_pytest_markers.py::TestClass::test_method PASSEDtest_pytest_markers.py::test_send_http PASSED==========================2passedin0.06seconds ===========================
5 用-k進(jìn)行關(guān)鍵字匹配來運(yùn)行測試用例名字子串
比如用-k選擇測試用例的名字中只帶有http關(guān)鍵字的運(yùn)行。
C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -k http test_pytest_markers.py============================= test session starts =============================platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.execachedir: .cacherootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:collected 4 items test_pytest_markers.py::test_send_http PASSED============================= 3 tests deselected ================================================= 1 passed, 3 deselected in 0.12 seconds ====================
Reference