一、前言
上一篇内容介绍了用例编写的规则以及执行用例,执行用例时我们发现有些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插件多样,大家有想了解的可以参考官方文档。我后面也会有针对性的进行补充,敬请期待哈!