Pytest参数选项自由执行测试用例详解(二)

本文详细介绍了Pytest的各种参数选项,包括-k用于通过关键字匹配测试用例,-m通过标记分组执行,-V提供详细输出,-s允许调试信息显示,-x在断言失败时全局停止,--maxfail指定失败次数后停止,--lf和--ff用于重复执行失败用例,以及--tb选项调整失败信息的显示风格。这些参数能帮助开发者更灵活地管理和运行测试用例。
摘要由CSDN通过智能技术生成

  

      运行pytest可以指定目录和文件,如果不指定,pytest会搜索当前目录及其子目录中以test_开头或以_test结尾得测试函数。我们把pytest搜索测试文件和测试用例的过程称为测试搜索(test discovery)。只要遵循pytest的命名规则,pytest就能自动搜索所有待执行的测试用例。所有的包必须要有init.py文件(在使用各种编辑器时会自动生成)

1、测试文件命名规则,test_xxx.py或xxx_test.py

2、方法、测试函数命名规则,test_xxx

3、测试类命名规则,Testxxx,并且不能带有 init 方法

Pytest参数选项在脚本中和命令行用法详解(一)

-k选项  -K EXPRESSION

使用表达式指定某个关键字的测试用例,如果某测试名是唯一的或多个测试名的前缀或后缀相同,可快速匹配,匹配范围是全局相同目录或下层目录所有(包名、文件名、类名、函数名为变量),文件名、类名、函数名,必须是test_开头或_test结尾的。

pytest.main(['-k','关键字'])
关键字包含于或等于文件名、类名、函数名,多个关键字之间用and、or、not连接


测试代码如下:

chengzi\test_04.py


import pytest




class TestClass(object):
    def test_one(self):
        assert 2 == 1




    def test_two(self):
        assert 2 == 2


    def test_two2(self):
        assert 2 == 2

执行匹配包名,运行了3条用例

pytest.main(['-v','-k','chengzi'])

if __name__ == '__main__':
    pytest.main(['-v','-k','chengzi'])
    
 "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/chengzi/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Packages': {'pluggy': '0.13.1', 'py': '1.9.0', 'pytest': '6.1.1'}, 'Platform': 'Windows-10-10.0.18362-SP0', 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Python': '3.5.2', 'Plugins': {'allure-pytest': '2.8.18', 'metadata': '1.8.0', 'rerunfailures': '9.1.1', 'html': '1.22.0'}}
rootdir: C:\Users\wangli\PycharmProjects\untitled\chengzi
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
collecting ... collected 3 items


test_04.py::TestClass::test_one FAILED                                   [ 33%]
test_04.py::TestClass::test_two PASSED                                   [ 66%]
test_04.py::TestClass::test_two2 PASSED                                  [100%]


================================== FAILURES ===================================
_____________________________ TestClass.test_one ______________________________


self = <chengzi.test_04.TestClass object at 0x000001F27E537B70>


    def test_one(self):
>       assert 2 == 1
E       assert 2 == 1
E         +2
E         -1


test_04.py:5: AssertionError
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_one - assert 2 == 1
========================= 1 failed, 2 passed in 0.63s =========================


Process finished with exit code 0

执行匹配文件名,运行了3条用例

pytest.main(['-v','-k','test_04.py'])
if __name__ == '__main__':
    pytest.main(['-v','-k','test_04.py'])
    
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/chengzi/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Platform': 'Windows-10-10.0.18362-SP0', 'Python': '3.5.2', 'Packages': {'py': '1.9.0', 'pluggy': '0.13.1', 'pytest': '6.1.1'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Plugins': {'rerunfailures': '9.1.1', 'html': '1.22.0', 'allure-pytest': '2.8.18', 'metadata': '1.8.0'}}
rootdir: C:\Users\wangli\PycharmProjects\untitled\chengzi
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
collecting ... collected 3 items


test_04.py::TestClass::test_one FAILED                                   [ 33%]
test_04.py::TestClass::test_two PASSED                                   [ 66%]
test_04.py::TestClass::test_two2 PASSED                                  [100%]


================================== FAILURES ===================================
_____________________________ TestClass.test_one ______________________________


self = <chengzi.test_04.TestClass object at 0x000002031D345588>


    def test_one(self):
>       assert 2 == 1
E       assert 2 == 1
E         +2
E         -1


test_04.py:6: AssertionError
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_one - assert 2 == 1
========================= 1 failed, 2 passed in 0.48s =========================


Process finished with exit code 0

执行匹配类名,运行了3条用例

pytest.main(['-v','-k','Class'])


if __name__ == '__main__':
    pytest.main(['-v','-k','Class'])


"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/chengzi/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Plugins': {'html': '1.22.0', 'rerunfailures': '9.1.1', 'metadata': '1.8.0', 'allure-pytest': '2.8.18'}, 'Packages': {'pluggy': '0.13.1', 'py': '1.9.0', 'pytest': '6.1.1'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Python': '3.5.2', 'Platform': 'Windows-10-10.0.18362-SP0'}
rootdir: C:\Users\wangli\PycharmProjects\untitled\chengzi
plugins: allure-pytest-2.8.18, html-1.
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大力测试进阶之路

打赏博主喝瓶水吧!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值