Pytest運行測試用例的多種方式和調試


測試用例上方使用多個fixtures疊加時,是從下往上進行fixtures調用的。如果是 @pytest.mark.usefixtures('action','a','action2')這種形式,是從左往右進行fixtures調用的.



#Below are test_pytest_markers.py # content of test_server.py import pytest @pytest.mark.webtest def test_send_http(): pass # perform some webtest test for your app def test_something_quick(): pass def test_another(): pass class TestClass: def test_method(self): pass
復制代碼

1. Pytest Marker 機制

對於Pytest的測試用例,可以在每一個測試用例加一個marker,比如pytest運行的時就只運行帶有該marker的測試用例,比如下面的@pytest.mark.webtest。

復制代碼
C:\Users\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.exe
cachedir: .cache
rootdir: C:\Users\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 ====================
復制代碼
  》》》》》pytest -v -m "not webtest" test_pytest_markers.py

2. 選擇運行特定的某個測試用例

你可以按照某個測試用例的的模塊,類或函數來選擇你要運行的case,比如下面的方式就適合一開始在調試單個測試用例的時候。

pytest -v test_pytest_markers.py::TestClass::test_method

3. 選擇運行特定的某個類

>pytest -v test_pytest_markers.py::TestClass

4 多種組合

>pytest -v test_pytest_markers.py::TestClass test_pytest_markers.py::test_send_http

5 用-k進行關鍵字匹配來運行測試用例名字子串

>pytest -v -k http test_pytest_markers.py


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM