自动化测试框架pytest命令参数

【软件测试面试突击班】如何逼自己一周刷完软件测试八股文教程,刷完面试就稳了,你也可以当高薪软件测试工程师(自动化测试)

失败后停止

使用下面的参数可以让测试在第1(N)次测试失败后停止:

pytest ‐x # 第一次测试失败后停止测试 
pytest ‐‐maxfail=2 # 第2次测试失败后停止测试

修改文件如下

# filename:test_02.py
import pytest
 
class TestDemo02:
    def func(self, x):
        return x + 1
 
    # 修改成断言失败
    def test_01(self):
        assert self.func(3) == 14
 
    # 修改成断言失败
    def test_02(self):
        assert self.func(3) == 5
 
 
if __name__ == '__main__':
    pytest.main(['-s', '-x', 'test_02.py'])
 

运行后观察结果

test_02.py F
 
================================== FAILURES ===================================
_____________________________ TestDemo02.test_01 ______________________________
 
...此处省略报错具体原因
 
test_02.py:10: AssertionError
=========================== short test summary info ===========================
FAILED test_02.py::TestDemo02::test_01 - assert 4 == 14
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
============================== 1 failed in 0.09s ==============================
 
Process finished with exit code 0

可以看出,由于第一条报错,2条用例只执行了第1条,使用‐‐maxfail=2可以失败两次后停止,大家自己尝试下。

指定执行范围

通过test_01.py文件执行测试

pytest test_01.py

通过文件夹执行测试

pytest testcase
创建testcase目录,然后把test_02.py移入目录下

然后执行命令,可以发现只执行了test_02.py下的用例
============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\auto-pytest
collected 3 items
testcase\test_02.py FF
================================== FAILURES ===================================
_____________________________ TestDemo02.test_01 ______________________________
...此处省略报错具体原因
testcase\test_02.py:11: AssertionError
_____________________________ TestDemo02.test_02 ______________________________
...此处省略报错具体原因
testcase\test_02.py:15: AssertionError
=========================== short test summary info ===========================
FAILED testcase/test_02.py::TestDemo02::test_01 - assert 4 == 14
FAILED testcase/test_02.py::TestDemo02::test_02 - assert 4 == 5
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 2 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
============================== 2 failed in 0.04s ==============================

通过关键字表达式来进行测试

这种方式会执行文件名,类名以及函数名与给定的字符串表达式相匹配的测试用例。

pytest ‐k "Demo02 and not 01"

上面的用例会执 行TestDemo02.test_02但是不会执行TestDemo02.test_01,就像这样,另外3个没执行

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\auto-pytest
collected 5 items / 3 deselected / 2 selected
 
testcase\test_02.py ..
 
======================= 2 passed, 3 deselected in 0.03s =======================

通过节点id来进行测试

参数化的类名、函数名和参数,用::分隔。

可以通过下面的方式运行模块中的指定的测试用例

pytest testcase/test_02.py::TestDemo02:test_01
可以从结果看出,执行了test_02.py内TestDemo02类下的test_01方法
============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\auto-pytest
collected 1 item
testcase\test_02.py .
============================== 1 passed in 0.01s ==============================

通过标记来执行

pytest ‐m tag01

这种方式会运行所有通过装饰器 @pytest.mark.tag进行装饰的测试用例,所以我们来修改test_02.py文件

# filename:test_02.py
import pytest
 
class TestDemo02():
    def func(self, x):
        return x + 1
 
    @pytest.mark.tag01
    def test_01(self):
        assert self.func(3) == 4
	
    def test_02(self):
        assert self.func(3) == 4
 
    def test_03(self):
        assert self.func(3) == 4

修改后,运行得到结果,可以看出只执行了打上标记的用例,篇幅问题,结果就不贴上来了

也可以通过pytest -m "tag01 or tag02"来执行标记为tag01和tag02的用例

下面是配套学习资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

自动化测试框架pytest命令参数_第1张图片

软件测试面试小程序

被百万人刷爆的软件测试题库!!!谁用谁知道!!!全网最全面试刷题小程序,手机就可以刷题,地铁上公交上,卷起来!

涵盖以下这些面试题板块:

1、软件测试基础理论 ,2、web,app,接口功能测试 ,3、网络 ,4、数据库 ,5、linux

6、web,app,接口自动化 ,7、性能测试 ,8、编程基础,9、hr面试题 ,10、开放性测试题,11、安全测试,12、计算机基础

 自动化测试框架pytest命令参数_第2张图片

文档获取方式:

这份文档,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

以上均可以分享,只需要你搜索vx公众号:程序员雨果,即可免费领取

你可能感兴趣的:(软件测试,技术分享,pytest,压力测试,jmeter,单元测试)