当前位置: 首页 > article >正文

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

失败后停止

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

  1. pytest ‐x # 第一次测试失败后停止测试

  2. 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.pyTestDemo02类下的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的用例

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

 

 这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取 


http://www.kler.cn/news/309444.html

相关文章:

  • 如何在@GenericGenerator中显式指定schema
  • 友思特方案 | 搭建红外桥梁:嵌入式视觉接口助力红外热像仪传输
  • SpringBoot入门(黑马)
  • 【数据可视化】Arcgis api 4.x 专题图制作之分级色彩,采用自然间断法(使用simple-statistics JS数学统计库生成自然间断点)
  • 0072__ActiveX插件的使用
  • Linux云计算 |【第二阶段】SHELL-DAY5
  • pdf文件怎么转换成ppt?介绍几种pdf转ppt的方法
  • 传感技术的应用
  • 利用正则表达式匹配格式并且获取替换内容中数据并保留
  • VS+QT--实现二进制和十进制的转换(含分数部分)
  • 去中心化的力量:探索Web3的分布式网络
  • 工商银行银企直联接口清单
  • Java高级Day40-QQ项目全代码
  • 使用SQL数据构建问答系统的完整指南
  • Nginx泛域名 解析的匹配前缀绑定或转发到子目录
  • APP测试基本流程与APP测试要点总结
  • 什么是单元测试?怎么做?
  • C++系列-匿名对象
  • linux网络命令:使用最多最广泛的网络抓包工具tcpdump详细介绍
  • MATLAB入门教程
  • 检查一个复数C的实部a和虚部b是否都是有限数值即a和b都不是无限数值、空值cmath.isfinite(x)
  • MES管理系统在智能制造中的重要应用
  • CMU 10423 Generative AI:lec5(Encoder-only Transformers + 阅读材料Bert, ViT)
  • 如何理解BCEWithLogitsLoss()
  • 什么是期权日内交易?怎么做日内期权策略?
  • MyBatis 源码解析:Mapper 文件加载与解析
  • 导弹追踪问题:蒙特卡罗模拟+matlab代码
  • Linux7-su,exit,sudo
  • Java 中的 sleep、wait、join 怎么理解
  • linux中的kill、pkill和killall