測試用例上方使用多個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