Pytest運(yùn)行測(cè)試用例的多種方式

Pytest提供了多樣化的方式來(lái)運(yùn)行和調(diào)試測(cè)試用例,本文介紹一些比較常用的方式。

pytest是如何識(shí)別測(cè)試用例的呢?它默認(rèn)使用檢查以test_.py 或test.py命名的文件名,在文件內(nèi)部查找以test打頭的方法或函數(shù),并執(zhí)行它們。

下面是Pytest的官網(wǎng)的一些測(cè)試?yán)樱疚挠盟鼇?lái)進(jìn)行不同的Pytest運(yùn)行。

首先安裝好Python和Pytest,新建一個(gè)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ī)制

對(duì)于Pytest的測(cè)試用例,可以在每一個(gè)測(cè)試用例加一個(gè)marker,比如pytest運(yùn)行的時(shí)就只運(yùn)行帶有該marker的測(cè)試用例,比如下面的@pytest.mark.webtest。

在我們的實(shí)際項(xiàng)目中,我們通常把為每個(gè)feature建立相應(yīng)的marker,用于測(cè)試時(shí)進(jìn)行不同條件的挑選。

Pytest還支持一個(gè)測(cè)試用例有多個(gè)marker,這個(gè)在后續(xù)會(huì)把相應(yīng)的實(shí)踐加入進(jìn)來(lái)。

測(cè)試結(jié)果如下,只挑選了測(cè)試用例帶有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 ====================

同樣的,也可以只挑選了測(cè)試用例中不帶有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)行特定的某個(gè)測(cè)試用例

你可以按照某個(gè)測(cè)試用例的的模塊,類(lèi)或函數(shù)來(lái)選擇你要運(yùn)行的case,比如下面的方式就適合一開(kāi)始在調(diào)試單個(gè)測(cè)試用例的時(shí)候。

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)行特定的某個(gè)類(lèi)

比如你想單獨(dú)運(yùn)行下某個(gè)類(lèi)的所有測(cè)試用例,可以按照如下操作。

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è)試用例。

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)鍵字匹配來(lái)運(yùn)行測(cè)試用例名字子串

比如用-k選擇測(cè)試用例的名字中只帶有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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容