pytest用例设计规则
- 测试文件以test_开头(以_test结尾也可以)
- 测试类以Test开头,并且不能带有
__init__方法 - 测试函数以test_开头
- 断言使用assert
- 所有的包pakege必须要有__init__.py文件
pytest常见的命令行参数
命令行参数 pytest -h 可以看帮助
在 terminal 使用命令行参数

运行代码如下:
第一部分
def add(a): return a+1 def test_x(): assert add(2)==3 def test_y(): assert add(3)==5
第二部分
import pytest class TestDemo: @pytest.mark.mark1 def test_one(self): assert True def test_two(self): assert True def test_three(self): assert True

通过标记表达式执行 pytest -m demo 这条命令会执行被装饰器@pytest.mark.demo装饰的所有测试用例 生成html报告: pytest -m demo --html=Report/report.html 生成xml报告: pytest -m demo --junitxml=Report/report.xml 运行指定模块: pytest -m demo --html=Report/report.html TestCases/test_pytest.py 运行指定测试目录 pytest -m demo --html=Report/report.html TestCases/ 通过节点id来运行: pytest TestCases/test_pytest.py::TestDemo::test_demo01 通过关键字表达式过滤执行 pytest -k "MyClass and not method" 这条命令会匹配文件名、类名、方法名匹配表达式的用例 获取用例执行性能数据 获取最慢的10个用例的执行耗时 pytest --durations=10
--collect only 收集要运行的测试用例,当前文件夹所有的可运行方法
(LearnPytest) C:UsersCXMPycharmProjectsLearnPytest>pytest --collect-only testcase
========================================== test session starts ==========================================
platform win32 -- Python 3.7.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:UsersCXMPycharmProjectsLearnPytest
collected 5 items
<Module testcase/test_1.py>
<Function test_x>
<Function test_y>
<Module testcase/test_demo.py>
<Class TestDemo>
<Function test_one>
<Function test_two>
<Function test_three>
-k 匹配要运行的函数表达式
(LearnPytest) C:UsersCXMPycharmProjectsLearnPytest>pytest -k "test_one or test_two" --collect-only testcase
======================================================= test session starts =======================================================
platform win32 -- Python 3.7.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:UsersCXMPycharmProjectsLearnPytest
collected 5 items / 3 deselected / 2 selected
<Module testcase/test_demo.py>
<Class TestDemo>
<Function test_one>
<Function test_two>
=========================================== 2/5 tests collected (3 deselected) in 0.02s ===========================================
-m 自定义标记执行,只执行被mark的自定义参数标记的这个函数
(LearnPytest) C:UsersCXMPycharmProjectsLearnPytest>pytest -m mark1 testcase
======================================================= test session starts =======================================================
platform win32 -- Python 3.7.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:UsersCXMPycharmProjectsLearnPytest
collected 5 items / 4 deselected / 1 selected
testcase est_demo.py . [100%]
======================================================== warnings summary =========================================================
testcase est_demo.py:3
C:UsersCXMPycharmProjectsLearnPytest estcase est_demo.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.mark1 - is this a
typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
@pytest.mark.mark1
-x 有失败的用例就立马结束,失败就马上停止运行
testcase est_demo.py:3
C:UsersCXMPycharmProjectsLearnPytest estcase est_demo.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.mark1 - is this a
typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
@pytest.mark.mark1
-- Docs: https://docs.pytest.org/en/stable/warnings.html
===================================================== short test summary info =====================================================
FAILED testcase/test_1.py::test_y - assert 4 == 5
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================= 1 failed, 1 passed, 1 warning in 0.09s ==============================================
-v 执行的详细信息,比如说哪个失败哪个成功
--maxfail=num 当用例错误个数达到指定数量时,停止测试
(LearnPytest) C:UsersCXMPycharmProjectsLearnPytest>pytest --maxfail=1
=================================== test session starts ====================================
platform win32 -- Python 3.7.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:UsersCXMPycharmProjectsLearnPytest
collected 5 items
testcase est_1.py .F
========================================= FAILURES =========================================
__________________________________________ test_y __________________________________________
def test_y():
> assert add(3)==5
E assert 4 == 5
E + where 4 = add(3)
testcase est_1.py:6: AssertionError
===================================== warnings summary =====================================
testcase est_demo.py:3
C:UsersCXMPycharmProjectsLearnPytest estcase est_demo.py:3: PytestUnknownMarkWarning:
Unknown pytest.mark.mark1 - is this a typo? You can register custom marks to avoid this war
ning - for details, see https://docs.pytest.org/en/stable/mark.html
@pytest.mark.mark1
-- Docs: https://docs.pytest.org/en/stable/warnings.html
================================= short test summary info ==================================
FAILED testcase/test_1.py::test_y - assert 4 == 5
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================== 1 failed, 1 passed, 1 warning in 0.05s ==========================
-s 代码中打印输出
(LearnPytest) C:UsersCXMPycharmProjectsLearnPytest>pytest -s testcase ================================================================= test session starts ================================================================= platform win32 -- Python 3.7.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 rootdir: C:UsersCXMPycharmProjectsLearnPytest collected 5 items testcase est_1.py .F testcase est_demo.py ... ====================================================================== FAILURES ======================================================================= _______________________________________________________________________ test_y ________________________________________________________________________ def test_y(): > assert add(3)==5 E assert 4 == 5 E + where 4 = add(3) testcase est_1.py:6: AssertionError ================================================================== warnings summary =================================================================== testcase est_demo.py:3 C:UsersCXMPycharmProjectsLearnPytest estcase est_demo.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.mark1 - is this a typo? You can regis ter custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html @pytest.mark.mark1 -- Docs: https://docs.pytest.org/en/stable/warnings.html =============================================================== short test summary info =============================================================== FAILED testcase/test_1.py::test_y - assert 4 == 5 ======================================================= 1 failed, 4 passed, 1 warning in 0.05s ========================================================
--lf 只显示失败的内容
(LearnPytest) C:UsersCXMPycharmProjectsLearnPytest>pytest --lf testcase
================================================ test session starts ================================================
platform win32 -- Python 3.7.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:UsersCXMPycharmProjectsLearnPytest
collected 1 item
run-last-failure: rerun previous 1 failure (skipped 1 file)
testcase est_1.py F [100%]
===================================================== FAILURES ======================================================
______________________________________________________ test_y _______________________________________________________
def test_y():
> assert add(3)==5
E assert 4 == 5
E + where 4 = add(3)
testcase est_1.py:6: AssertionError
============================================== short test summary info ==============================================
FAILED testcase/test_1.py::test_y - assert 4 == 5
================================================= 1 failed in 0.04s =================================================
(LearnPytest) C:UsersCXMPycharmProjectsLearnPytest>
--ff pass的和fail都显示出来
(LearnPytest) C:UsersCXMPycharmProjectsLearnPytest>pytest --ff testcase
================================================================= test session starts =================================================================
platform win32 -- Python 3.7.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:UsersCXMPycharmProjectsLearnPytest
collected 5 items
run-last-failure: rerun previous 1 failure first
testcase est_1.py F. [ 40%]
testcase est_demo.py ... [100%]
====================================================================== FAILURES =======================================================================
_______________________________________________________________________ test_y ________________________________________________________________________
def test_y():
> assert add(3)==5
E assert 4 == 5
E + where 4 = add(3)
testcase est_1.py:6: AssertionError
================================================================== warnings summary ===================================================================
testcase est_demo.py:3
C:UsersCXMPycharmProjectsLearnPytest estcase est_demo.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.mark1 - is this a typo? You can regis
ter custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
@pytest.mark.mark1
-- Docs: https://docs.pytest.org/en/stable/warnings.html
=============================================================== short test summary info ===============================================================
FAILED testcase/test_1.py::test_y - assert 4 == 5
======================================================= 1 failed, 4 passed, 1 warning in 0.05s ========================================================
-s -v --tb=no 不显示具体的失败信息,只是显示哪一条错误
(LearnPytest) C:UsersCXMPycharmProjectsLearnPytest>pytest -s -v --tb=no testcase ================================================================= test session starts ================================================================= platform win32 -- Python 3.7.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:program filespythonlearnpytestscriptspython.exe cachedir: .pytest_cache rootdir: C:UsersCXMPycharmProjectsLearnPytest collected 5 items testcase/test_1.py::test_x PASSED testcase/test_1.py::test_y FAILED testcase/test_demo.py::TestDemo::test_one PASSED testcase/test_demo.py::TestDemo::test_two PASSED testcase/test_demo.py::TestDemo::test_three PASSED ================================================================== warnings summary =================================================================== testcase est_demo.py:3 C:UsersCXMPycharmProjectsLearnPytest estcase est_demo.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.mark1 - is this a typo? You can regis ter custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html @pytest.mark.mark1 -- Docs: https://docs.pytest.org/en/stable/warnings.html =============================================================== short test summary info =============================================================== FAILED testcase/test_1.py::test_y - assert 4 == 5 ======================================================= 1 failed, 4 passed, 1 warning in 0.04s ========================================================
--duration=N:表示把最耗时间的用例展示出来,N表示最慢的N个
pycharm执行命令行参数
在前面有介绍到pycharm执行pytest的时候需要修改下项目中的执行内容,从unittests框架变成pytest框架。然后我们进行编写代码
import pytest
class Test_01:
def test_001(self):
print('Test_01下的用例001')
assert 1 == 1
def test_002(self):
print('Test_01下的用例002')
assert 1 == 2
def test_003(self):
print('Test_01下的用例003')
assert 3 == 3
if __name__ == '__main__':
pytest.main()
这个时候直接右键运行是可以的,那么如果想要加入命令行的参数应该怎么做呢?
大家可以直接通过在 pytest.main()中直接添加
举个小栗子,例如我们想要打印详细内容
import pytest
class Test_01:
def test_001(self):
print('Test_01下的用例001')
assert 1 == 1
def test_002(self):
print('Test_01下的用例002')
assert 1 == 2
def test_003(self):
print('Test_01下的用例003')
assert 3 == 3
if __name__ == '__main__':
pytest.main(['-s'])
这里发现已经把详细内容都打印出来了。

命令行多个参数
--tb=no加上-s来举例,可以通过下图看出来,错误信息没有打印,还有用例执行的详细内容

