pytest --collect-only 搜集要运行的测试用例,不运行
匹配表达式 -k
D:\tools\pycharm\autotest>pytest -k "test_create_article or test_article_edit_alias" --collect-only 匹配包含test_create_article 或 test_article_edit_alias的用例
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.8.0, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\tools\pycharm\autotest
plugins: html-3.1.1, metadata-2.0.2
collected 7 items / 3 deselected / 4 selected
============================================================================================== 4/7 tests collected (3 deselected) in 1.88s ===============================================================================================
D:\tools\pycharm\autotest>pytest -k "test_ and _alias" --collect-only 匹配包含test_ 和 _alias的用例
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.8.0, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\tools\pycharm\autotest
plugins: html-3.1.1, metadata-2.0.2
collected 7 items / 6 deselected / 1 selected
============================================================================================== 1/7 tests collected (6 deselected) in 1.75s ===============================================================================================
-m 自定义标记执行
@pytest.mark.mark1 def test_create_article():
D:\tools\pycharm\autotest>pytest -m "mark1" --collect-only
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.8.0, pytest-7.1.3, pluggy-1.0.0
rootdir: D:\tools\pycharm\autotest
plugins: html-3.1.1, metadata-2.0.2
collected 7 items / 6 deselected / 1 selected
============================================================================================== 1/7 tests collected (6 deselected) in 1.77s ===============================================================================================
-x 失败后立马结束运行
import pytest
class TestDemo:
def test_one(self):
assert True
def test_two(self):
assert False
def test_three(self):
assert True
def test_four(self):
assert True
D:\tools\pycharm\pythonProject1>pytest -x 自动化 -v
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.8.0, pytest-7.1.3, pluggy-1.0.0 -- d:\tools\python3.8\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.0', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '7.1.3', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'html': '3.1.1', 'metadata': '2.0.2'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jdk
1.8.0_111'}
rootdir: D:\tools\pycharm\pythonProject1
plugins: html-3.1.1, metadata-2.0.2
collected 4 items
自动化/测试pytest参数/test_demo.py::TestDemo::test_one PASSED [ 25%]
自动化/测试pytest参数/test_demo.py::TestDemo::test_two FAILED [ 50%]
================================================================================================================ FAILURES ================================================================================================================
___________________________________________________________________________________________________________ TestDemo.test_two ____________________________________________________________________________________________________________
self =
def test_two(self):
> assert False
E assert False
自动化\测试pytest参数\test_demo.py:8: AssertionError
======================================================================================================== short test summary info =========================================================================================================
FAILED 自动化/测试pytest参数/test_demo.py::TestDemo::test_two - assert False
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
====================================================================================================== 1 failed, 1 passed in 0.43s =======================================================================================================
-v 显示执行的详细信息
D:\tools\pycharm\pythonProject1>pytest -x 自动化 -v
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.8.0, pytest-7.1.3, pluggy-1.0.0 -- d:\tools\python3.8\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.0', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '7.1.3', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'html': '3.1.1', 'metadata': '2.0.2'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jdk
1.8.0_111'}
rootdir: D:\tools\pycharm\pythonProject1
plugins: html-3.1.1, metadata-2.0.2
collected 4 items
自动化/测试pytest参数/test_demo.py::TestDemo::test_one PASSED [ 25%]
自动化/测试pytest参数/test_demo.py::TestDemo::test_two FAILED [ 50%]
--maxfail=num 最大失败次数,当超过最大失败次数就不在往下执行
D:\tools\pycharm\pythonProject1>pytest -v --maxfail=1 自动化
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.8.0, pytest-7.1.3, pluggy-1.0.0 -- d:\tools\python3.8\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.0', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '7.1.3', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'html': '3.1.1', 'metadata': '2.0.2'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jdk
1.8.0_111'}
rootdir: D:\tools\pycharm\pythonProject1
plugins: html-3.1.1, metadata-2.0.2
collected 4 items
自动化/测试pytest参数/test_demo.py::TestDemo::test_one PASSED [ 25%]
自动化/测试pytest参数/test_demo.py::TestDemo::test_two FAILED [ 50%]
================================================================================================================ FAILURES ================================================================================================================
___________________________________________________________________________________________________________ TestDemo.test_two ____________________________________________________________________________________________________________
self =
def test_two(self):
> assert False
E assert False
自动化\测试pytest参数\test_demo.py:8: AssertionError
======================================================================================================== short test summary info =========================================================================================================
FAILED 自动化/测试pytest参数/test_demo.py::TestDemo::test_two - assert False
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
====================================================================================================== 1 failed, 1 passed in 0.46s =======================================================================================================
D:\tools\pycharm\pythonProject1>
-s 代码中的打印输出
class TestDemo:
def test_one(self):
print("Hello test Demo one")
assert True
D:\tools\pycharm\pythonProject1>pytest -v 自动化 -s
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.8.0, pytest-7.1.3, pluggy-1.0.0 -- d:\tools\python3.8\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.0', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '7.1.3', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'html': '3.1.1', 'metadata': '2.0.2'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jdk
1.8.0_111'}
rootdir: D:\tools\pycharm\pythonProject1
plugins: html-3.1.1, metadata-2.0.2
collected 4 items
自动化/测试pytest参数/test_demo.py::TestDemo::test_one Hello test Demo one
PASSED
自动化/测试pytest参数/test_demo.py::TestDemo::test_two FAILED
自动化/测试pytest参数/test_demo.py::TestDemo::test_three PASSED
自动化/测试pytest参数/test_demo.py::TestDemo::test_four FAILED
--lf 只运行失败的
D:\tools\pycharm\pythonProject1>pytest -v --lf 自动化

--ff 失败成功的都运行
D:\tools\pycharm\pythonProject1>pytest -v --ff 自动化

--tb=no 不显示具体的失败信息 pytest -v --tb=no 自动化
【或私信000】
群里的免费资料都是笔者十多年测试生涯的精华。还有同行大神一起交流技术哦。
项目实战:

大型电商平台:

全套软件测试自动化测试教学视频

300G教程资料下载【视频教程+PPT+项目源码】

全套软件测试自动化测试大厂面经

python自动化测试++全套模板+性能测试


听说关注我并三连的铁汁都已经升职加薪暴富了哦!!!!