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:UsersPycharmProjectspytest_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:Python27python.exe
cachedir: .cache
rootdir: C:UsersPycharmProjectspytest_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

原文地址:https://www.cnblogs.com/shengs/p/9830112.html