【测试框架篇】单元测试框架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插件多样,大家有想了解的可以参考官方文档。我后面也会有针对性的进行补充,敬请期待哈!

相关推荐
软件检测小牛玛15 小时前
具备软件功能测试资质的机构哪家更权威?山东软件测评机构 中承信安
功能测试·单元测试·软件测试报告·软件测评机构
闻哥19 小时前
从测试坏味道到优雅实践:打造高质量单元测试
java·面试·单元测试·log4j·springboot
姚青&1 天前
Pytest 测试用例编写
测试用例·pytest
Warren981 天前
Pytest Fixture 作用域与接口测试 Token 污染问题实战解析
功能测试·面试·单元测试·集成测试·pytest·postman·模块测试
知行合一。。。1 天前
程序中的log4j、stderr、stdout日志
python·单元测试·log4j
测试秃头怪2 天前
面试大厂就靠这份软件测试八股文了【含答案】
自动化测试·软件测试·python·功能测试·面试·职场和发展·单元测试
测试大圣2 天前
软件测试基础知识总结(超全的)
软件测试·python·功能测试·测试工具·职场和发展·单元测试·测试用例
CodeCraft Studio2 天前
【Parasoft案例分享】在 DO-178C 标准下,如何实现航空嵌入式软件测试自动化
单元测试·自动化·静态分析·代码覆盖率·parasoft·do-178c·软件自动化测试
serve the people2 天前
python环境搭建 (七) pytest、pytest-asyncio、pytest-cov 试生态的核心组合
开发语言·python·pytest