【Pytest学习2】 pytest用例设计规则,terminal中使用常见命令行参数,pycharm中使用常见的命令行参数
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 通过标记表达式执行 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:\Users\CXM\PycharmProjects\LearnPytest>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:\Users\CXM\PycharmProjects\LearnPytest
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 匹配要运行的函数表达式
1 2 3 4 5 6 7 8 9 10 11 12 | (LearnPytest) C:\Users\CXM\PycharmProjects\LearnPytest>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:\Users\CXM\PycharmProjects\LearnPytest 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的自定义参数标记的这个函数
1 2 3 4 5 6 7 8 9 10 11 12 13 | (LearnPytest) C:\Users\CXM\PycharmProjects\LearnPytest>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:\Users\CXM\PycharmProjects\LearnPytest collected 5 items / 4 deselected / 1 selected testcase\test_demo.py . [100%] ======================================================== warnings summary ========================================================= testcase\test_demo.py:3 C:\Users\CXM\PycharmProjects\LearnPytest\testcase\test_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 有失败的用例就立马结束,失败就马上停止运行
1 2 3 4 5 6 7 8 9 10 | testcase\test_demo.py:3 C:\Users\CXM\PycharmProjects\LearnPytest\testcase\test_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 当用例错误个数达到指定数量时,停止测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | (LearnPytest) C:\Users\CXM\PycharmProjects\LearnPytest>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:\Users\CXM\PycharmProjects\LearnPytest collected 5 items testcase\test_1.py .F ========================================= FAILURES ========================================= __________________________________________ test_y __________________________________________ def test_y(): > assert add(3)==5 E assert 4 == 5 E + where 4 = add(3) testcase\test_1.py:6: AssertionError ===================================== warnings summary ===================================== testcase\test_demo.py:3 C:\Users\CXM\PycharmProjects\LearnPytest\testcase\test_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:\Users\CXM\PycharmProjects\LearnPytest>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:\Users\CXM\PycharmProjects\LearnPytest collected 5 items testcase\test_1.py .F testcase\test_demo.py ... ====================================================================== FAILURES ======================================================================= _______________________________________________________________________ test_y ________________________________________________________________________ def test_y(): > assert add(3)==5 E assert 4 == 5 E + where 4 = add(3) testcase\test_1.py:6: AssertionError ================================================================== warnings summary =================================================================== testcase\test_demo.py:3 C:\Users\CXM\PycharmProjects\LearnPytest\testcase\test_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 只显示失败的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | (LearnPytest) C:\Users\CXM\PycharmProjects\LearnPytest>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:\Users\CXM\PycharmProjects\LearnPytest collected 1 item run-last-failure: rerun previous 1 failure (skipped 1 file) testcase\test_1.py F [100%] ===================================================== FAILURES ====================================================== ______________________________________________________ test_y _______________________________________________________ def test_y(): > assert add(3)==5 E assert 4 == 5 E + where 4 = add(3) testcase\test_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:\Users\CXM\PycharmProjects\LearnPytest> |
--ff pass的和fail都显示出来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | (LearnPytest) C:\Users\CXM\PycharmProjects\LearnPytest>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:\Users\CXM\PycharmProjects\LearnPytest collected 5 items run-last-failure: rerun previous 1 failure first testcase\test_1.py F. [ 40%] testcase\test_demo.py ... [100%] ====================================================================== FAILURES ======================================================================= _______________________________________________________________________ test_y ________________________________________________________________________ def test_y(): > assert add(3)==5 E assert 4 == 5 E + where 4 = add(3) testcase\test_1.py:6: AssertionError ================================================================== warnings summary =================================================================== testcase\test_demo.py:3 C:\Users\CXM\PycharmProjects\LearnPytest\testcase\test_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:\Users\CXM\PycharmProjects\LearnPytest>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 files\python\learnpytest\scripts\python.exe cachedir: .pytest_cache rootdir: C:\Users\CXM\PycharmProjects\LearnPytest 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\test_demo.py:3 C:\Users\CXM\PycharmProjects\LearnPytest\testcase\test_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来举例,可以通过下图看出来,错误信息没有打印,还有用例执行的详细内容
声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/
如出现转载未声明 将追究法律责任~谢谢合作
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具