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

相关推荐
神探阿航1 天前
HNUST软件测试B考前最终复习
软件测试·单元测试·hnust·期中考试
csdn_freak_dd2 天前
查看单元测试覆盖率
java·单元测试
测试开发Kevin2 天前
从投入产出、效率、上手难易度等角度综合对比 pytest 和 unittest 框架
python·pytest
sheepfagdng4 天前
软件测试——用例篇(2)
功能测试·单元测试·测试用例·安全性测试
测试开发Kevin4 天前
以pytest_addoption 为例,讲解pytest框架中钩子函数的应用
python·pytest
西洼工作室4 天前
掌握单元测试:提升软件质量的关键步骤
java·单元测试
上官箫羽5 天前
Spring Boot 单元测试使用教程(仅供参考)
java·spring boot·单元测试
oliveira-time8 天前
java单元测试代码
java·windows·单元测试
尽-欢8 天前
以太坊智能合约开发框架:Hardhat v2 核心功能从入门到基础教程
单元测试·区块链·智能合约
Allen Bright9 天前
【Java JUnit单元测试框架-60】深入理解JUnit:Java单元测试的艺术与实践
java·junit·单元测试