pytest常用命令行参数

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

<Package testcases>
<Module test_abnormal.py>
<Function test_create_article[article_data0-None]>
<Function test_create_article[article_data1-java.sql.SQLException: Incorrect integer value: 'AAA' for column 'typeId' at row 1]>
<Module test_article_create.py>
<Function test_create_article>
<Module test_article_update.py>
<Function test_article_edit_alias>

============================================================================================== 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

<Package testcases>
<Module test_article_update.py>
<Function test_article_edit_alias>

============================================================================================== 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

<Package testcases>
<Module test_article_create.py>
<Function test_create_article>

============================================================================================== 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 = <test_demo.TestDemo object at 0x00000000037463D0>

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 = <test_demo.TestDemo object at 0x0000000003744490>

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 自动化

 

posted @ 2022-10-26 12:16  聚散浮沉  阅读(148)  评论(0)    收藏  举报