【测试框架篇】单元测试框架pytest(3):用例执行参数详解

一、前言

上一篇内容介绍了用例编写的规则以及执行用例,执行用例时我们发现有些print输出内容,结果没有给我们展示,这是因为什么原因呢?接下来我们会针对这些问题进行阐述。

二、参数大全

我们可以在cmd中通过输入 pytest -h 或者pytest --help 来查看帮助里面打印出来的都有哪些参数:

1、参数打印

  • general:
  • Reporting:
  • pytest-warnings:
  • collection:
  • test session debugging and configuration:
  • logging:
  • reporting:
  • pytest-metadata:
  • testreport:
  • distributed and subprocess testing:
  • pytest\] ini-options in the first pytest.ini\|tox.ini\|setup.cfg\|pyproject.toml file found:

  • Environment variables:

2、常用的参数

1)pytest - s

-s:执行用例,携带详细信息,比如打印的print内容

通过cmd进行执行,发现已经详细内容展示出来了

2)pytest -x

-x:遇到错误的用例,立即退出执行,并输出结果

从上图可以看到一共3条用例,执行了2条,其中执行到第2条的时候就已经程序就退出了。

3)pytest -k

-k:表示加需要执行的用例项,-k后面可以是测试函数名,也可以是类名,也可以是模块名,以及目录名称

也可以通过加入not来不执行一些用例,这里的not要和用例名放在一起用引号

4)pytest xxx.py

::类::用例:可以通过这种形式进行指定用例执行

5)pytest -m

-m:执行被标记的用例

标记需要用到pytest中的装饰器 @pytest.mark.标记名称

python 复制代码
import pytest
class Test_01:
    
    @pytest.mark.anjing
    def test_001(self):
        print('Test_01下的用例001')
        assert  1 == 1

    def test_002(self):
        print('Test_01下的用例002')
        assert  1 == 2
        
    @pytest.mark.anjing
    def test_003(self):
        print('Test_01下的用例003')
        assert 3 == 3

if __name__ == '__main__':
    pytest.main()

6)pytest -v

-v:表示查看详细的报告内容

7)pytest -q

-q:表示简则的测试报告,运行成功通过"."表示,运行失败通过"F"表示

8)pytest --collect-only

--collect-only:表示把待执行的用例全部展示出来。

9)pytest --maxfail=n

--maxfail=n:其中n表示失败的次数,运行失败n次后,进行停止测试

python 复制代码
# coding:utf-8
class Test:
    def test_01(self):
        print('这是用例01')
        assert 1 == 1

    def test_02(self):
        print('这是用例02')
        assert 1 == 2

    def test_03(self):
        print('这是用例03')
        assert 3 == 4

    def test_04(self):
        print('这是用例04')
        assert 4 == 4

10)pytest --lf

--lf:只执行上次失败的用例

11)pytest --tb

--tb=no:不展示用例失败的错误详情

--tb=line:展示用例失败的代码具体行数。下图结果是在11行

--tb=short:展示更加详细的错误信息

12)pytest --durations

--duration=N:表示把最耗时间的用例展示出来,N表示最慢的N个

安静的代码运行都很快,把其中一个代码加上睡眠,从图中可以看出来,test_01文件中的test_03运行最慢。

上面简单描述了常用的一些参数,pytest插件多样,大家有想了解的可以参考官方文档。我后面也会有针对性的进行补充,敬请期待哈!

相关推荐
AI+程序员在路上21 小时前
单元测试与QTestLib框架使用
开发语言·c++·单元测试
RainbowJie12 天前
Spring Boot 使用 SLF4J 实现控制台输出与分类日志文件管理
spring boot·后端·单元测试
FINE!(正在努力!)2 天前
PyTest框架学习
学习·pytest
程序员杰哥2 天前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
测试老哥3 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
水银嘻嘻3 天前
07 APP 自动化- appium+pytest+allure框架封装
python·appium·自动化·pytest
gb42152873 天前
springboot项目下面的单元测试注入的RedisConnectionFactory类redisConnectionFactory值为什么为空呢?
spring boot·后端·单元测试
天才测试猿3 天前
接口自动化测试之pytest接口关联框架封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
大熊猫侯佩4 天前
用接地气的例子趣谈 WWDC 24 全新的 Swift Testing 入门(二)
单元测试·swift·apple