pytest常用参数汇总
1. -s 表示输出调试信息,包括print打印信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
D:\demo>pytest -s ./pytest_1 =============================================================== test session starts ================================================================ platform win32 -- Python 3.7.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 rootdir: D:\demo plugins: allure-pytest-2.9.43, forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.0, xdist-2.3.0 collected 1 item pytest_1\test_login.py 登录1 . ================================================================ 1 passed in 0.07s =================================================================
或
pytest.main(['-s', './pytest_1/test_login.py'])
2. -v 表示输出用例执行详细信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
D:\demo>pytest -v ./pytest_1 =============================================================== test session starts ================================================================ platform win32 -- Python 3.7.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\program files\python\python37-32\python.exe cachedir: .pytest_cache metadata: {'Python': '3.7.4', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins' : {'allure-pytest': '2.9.43', 'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.0', 'xdist': '2.3.0'} , 'JAVA_HOME': 'D:\\Program Files\\Java\\jdk1.8.0_221'} rootdir: D:\demo plugins: allure-pytest-2.9.43, forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.0, xdist-2.3.0 collected 1 item pytest_1/test_login.py::TestLogin::test_08_loogin1 PASSED [100%] ================================================================ 1 passed in 0.03s =================================================================
或
pytest.main(['-v', './pytest_1/test_login.py'])
3. -vs 表示既输出调试信息同时输出执行详细信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
D:\demo>pytest -vs ./pytest_1 =============================================================== test session starts ================================================================ platform win32 -- Python 3.7.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\program files\python\python37-32\python.exe cachedir: .pytest_cache metadata: {'Python': '3.7.4', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins' : {'allure-pytest': '2.9.43', 'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.0', 'xdist': '2.3.0'} , 'JAVA_HOME': 'D:\\Program Files\\Java\\jdk1.8.0_221'} rootdir: D:\demo plugins: allure-pytest-2.9.43, forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.0, xdist-2.3.0 collected 1 item pytest_1/test_login.py::TestLogin::test_08_loogin1 登录1 PASSED ================================================================ 1 passed in 0.03s =================================================================
或
pytest.main(['-vs', './pytest_1/test_login.py'])
4. -n 支持多线程或者分布式运行测试用例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
D:\demo>pytest -vs ./pytest_1 -n 2
=============================================================== test session starts ================================================================
platform win32 -- Python 3.7.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\program files\python\python37-32\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.4', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins'
: {'allure-pytest': '2.9.43', 'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.0', 'xdist': '2.3.0'}
, 'JAVA_HOME': 'D:\\Program Files\\Java\\jdk1.8.0_221'}
rootdir: D:\demo
plugins: allure-pytest-2.9.43, forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.0, xdist-2.3.0
[gw0] win32 Python 3.7.4 cwd: D:\demo
[gw1] win32 Python 3.7.4 cwd: D:\demo
[gw0] Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)]
[gw1] Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)]
gw0 [4] / gw1 [4]
scheduling tests via LoadScheduling
pytest_1/test_login.py::TestLogin::test_07_login2
pytest_1/test_login.py::TestLogin::test_08_loogin1
[gw0] PASSED pytest_1/test_login.py::TestLogin::test_07_login2
pytest_1/test_login.py::TestLogin::test_09_login3
[gw1] PASSED pytest_1/test_login.py::TestLogin::test_08_loogin1
[gw0] PASSED pytest_1/test_login.py::TestLogin::test_09_login3
pytest_1/test_login.py::TestLogin::test_01_login4
[gw1] PASSED pytest_1/test_login.py::TestLogin::test_01_login4
================================================================ 4 passed in 1.61s =================================================================
或
pytest.main(['-vs', './pytest_1/test_login.py', '-n=2'])
5. --reruns 失败用例重跑
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
D:\demo>pytest -vs ./pytest_1/test_login.py --reruns 2 =============================================================== test session starts ================================================================ platform win32 -- Python 3.7.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\program files\python\python37-32\python.exe cachedir: .pytest_cache metadata: {'Python': '3.7.4', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins' : {'allure-pytest': '2.9.43', 'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.0', 'xdist': '2.3.0'} , 'JAVA_HOME': 'D:\\Program Files\\Java\\jdk1.8.0_221'} rootdir: D:\demo plugins: allure-pytest-2.9.43, forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.0, xdist-2.3.0 collected 1 item pytest_1/test_login.py::TestLogin::test_08_loogin1 登录1 RERUN pytest_1/test_login.py::TestLogin::test_08_loogin1 登录1 RERUN pytest_1/test_login.py::TestLogin::test_08_loogin1 登录1 FAILED ===================================================================== FAILURES ===================================================================== ____________________________________________________________ TestLogin.test_08_loogin1 _____________________________________________________________ self = <pytest_1.test_login.TestLogin object at 0x04271DF0> @pytest.mark.run(order=3) def test_08_loogin1(self): print("登录1") > assert 1 == 2 E assert 1 == 2 E +1 E -2 pytest_1\test_login.py:10: AssertionError ============================================================= short test summary info ============================================================== FAILED pytest_1/test_login.py::TestLogin::test_08_loogin1 - assert 1 == 2 ============================================================ 1 failed, 2 rerun in 0.11s ============================================================
或
pytest.main(['-vs', './pytest_1/test_login.py', '--reruns=2'])
6. -x 只要存在失败用例则停止执行
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
D:\demo>pytest -vs ./pytest_1/test_login.py -x =============================================================== test session starts ================================================================ platform win32 -- Python 3.7.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\program files\python\python37-32\python.exe cachedir: .pytest_cache metadata: {'Python': '3.7.4', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins' : {'allure-pytest': '2.9.43', 'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.0', 'xdist': '2.3.0'} , 'JAVA_HOME': 'D:\\Program Files\\Java\\jdk1.8.0_221'} rootdir: D:\demo plugins: allure-pytest-2.9.43, forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.0, xdist-2.3.0 collected 4 items pytest_1/test_login.py::TestLogin::test_07_login2 登录2 PASSED pytest_1/test_login.py::TestLogin::test_08_loogin1 登录1 FAILED ===================================================================== FAILURES ===================================================================== ____________________________________________________________ TestLogin.test_08_loogin1 _____________________________________________________________ self = <pytest_1.test_login.TestLogin object at 0x0423D3D0> @pytest.mark.run(order=3) def test_08_loogin1(self): print("登录1") > assert 1 == 2 E assert 1 == 2 E +1 E -2 pytest_1\test_login.py:10: AssertionError ============================================================= short test summary info ============================================================== FAILED pytest_1/test_login.py::TestLogin::test_08_loogin1 - assert 1 == 2 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! =========================================================== 1 failed, 1 passed in 0.11s ============================================================
或
pytest.main(['-vs', './pytest_1/test_login.py', '-x'])
7. --maxfail 只要存在max个失败用例则停止执行
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
D:\demo>pytest -vs ./pytest_1/test_login.py --maxfail 2 =============================================================== test session starts ================================================================ platform win32 -- Python 3.7.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\program files\python\python37-32\python.exe cachedir: .pytest_cache metadata: {'Python': '3.7.4', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins' : {'allure-pytest': '2.9.43', 'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.0', 'xdist': '2.3.0'} , 'JAVA_HOME': 'D:\\Program Files\\Java\\jdk1.8.0_221'} rootdir: D:\demo plugins: allure-pytest-2.9.43, forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.0, xdist-2.3.0 collected 4 items pytest_1/test_login.py::TestLogin::test_07_login2 登录2 PASSED pytest_1/test_login.py::TestLogin::test_08_loogin1 登录1 FAILED pytest_1/test_login.py::TestLogin::test_09_login3 登录3 PASSED pytest_1/test_login.py::TestLogin::test_01_login4 登录4 PASSED ===================================================================== FAILURES ===================================================================== ____________________________________________________________ TestLogin.test_08_loogin1 _____________________________________________________________ self = <pytest_1.test_login.TestLogin object at 0x04CAEFB0> @pytest.mark.run(order=3) def test_08_loogin1(self): print("登录1") > assert 1 == 2 E assert 1 == 2 E +1 E -2 pytest_1\test_login.py:10: AssertionError ============================================================= short test summary info ============================================================== FAILED pytest_1/test_login.py::TestLogin::test_08_loogin1 - assert 1 == 2 =========================================================== 1 failed, 3 passed in 0.08s ============================================================
或
pytest.main(['-vs', './pytest_1/test_login.py', '--maxfail=2'])
8. -k 根据测试用例的部分字符串执行用例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
D:\demo>pytest -vs ./pytest_1/test_login.py -k "oo"
=============================================================== test session starts ================================================================
platform win32 -- Python 3.7.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\program files\python\python37-32\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.4', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins'
: {'allure-pytest': '2.9.43', 'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.0', 'xdist': '2.3.0'}
, 'JAVA_HOME': 'D:\\Program Files\\Java\\jdk1.8.0_221'}
rootdir: D:\demo
plugins: allure-pytest-2.9.43, forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.0, xdist-2.3.0
collected 4 items / 3 deselected / 1 selected
pytest_1/test_login.py::TestLogin::test_08_loogin1 登录1
FAILED
===================================================================== FAILURES =====================================================================
____________________________________________________________ TestLogin.test_08_loogin1 _____________________________________________________________
self = <pytest_1.test_login.TestLogin object at 0x045420B0>
@pytest.mark.run(order=3)
def test_08_loogin1(self):
print("登录1")
> assert 1 == 2
E assert 1 == 2
E +1
E -2
pytest_1\test_login.py:10: AssertionError
============================================================= short test summary info ==============================================================
FAILED pytest_1/test_login.py::TestLogin::test_08_loogin1 - assert 1 == 2
========================================================= 1 failed, 3 deselected in 0.07s ==========================================================
或
pytest.main(['-vs', './pytest_1/test_login.py', '-k=oo'])
9. --html 生成测试报告
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
D:\demo>pytest --html ./reports/report.html
=============================================================== test session starts ================================================================
platform win32 -- Python 3.7.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\program files\python\python37-32\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.4', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins'
: {'allure-pytest': '2.9.43', 'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.0', 'xdist': '2.3.0'}
, 'JAVA_HOME': 'D:\\Program Files\\Java\\jdk1.8.0_221'}
rootdir: D:\demo, configfile: pytest.ini, testpaths: ./pytest_1
plugins: allure-pytest-2.9.43, forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.0, xdist-2.3.0
collected 4 items
pytest_1/test_login.py::TestLogin::test_07_login2 登录2
PASSED
pytest_1/test_login.py::TestLogin::test_08_loogin1 登录1
PASSED
pytest_1/test_login.py::TestLogin::test_09_login3 SKIPPED (大于18跳过)
pytest_1/test_login.py::TestLogin::test_01_login4 登录4
PASSED
================================================================= warnings summary =================================================================
pytest_1\test_login.py:22
D:\demo\pytest_1\test_login.py:22: PytestUnknownMarkWarning: Unknown pytest.mark.usermanager - is this a typo? You can register custom marks to av
oid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
@pytest.mark.usermanager
-- Docs: https://docs.pytest.org/en/stable/warnings.html
--------------------------------------------- generated html file: file://D:\demo\reports\report.html ----------------------------------------------
===================================================== 3 passed, 1 skipped, 1 warning in 0.05s ======================================================