目录
1. -s 表示输出调试信息,包括print打印信息
2. -v 表示输出用例执行详细信息
3. -vs 表示既输出调试信息同时输出执行详细信息
4. -n 支持多线程或者分布式运行测试用例
5. --reruns 失败用例重跑
6. -x 只要存在失败用例则停止执行
7. --maxfail 只要存在max个失败用例则停止执行
8. -k 根据测试用例的部分字符串执行用例
9. --html 生成测试报告
总结:
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'])
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'])
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'])
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'])
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.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'])
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.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'])
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.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'])
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.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'])
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 ======================================================
感谢每一个认真阅读我文章的人!!!
我个人整理了我这几年软件测试生涯整理的一些技术资料,包含:电子书,简历模块,各种工作模板,面试宝典,自学项目等。欢迎大家点击下方名片免费领取,千万不要错过哦。
Python自动化测试学习交流群:全套自动化测试面试简历学习资料获取点击链接加入群聊【python自动化测试交流】:http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=DhOSZDNS-qzT5QKbFQMsfJ7DsrFfKpOF&authKey=eBt%2BF%2FBK81lVLcsLKaFqnvDAVA8IdNsGC7J0YV73w8V%2FJpdbby66r7vJ1rsPIifg&noverify=0&group_code=198408628