pytest的inifile文件配置

pytest配置文件

可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行

pytest.ini配置文件可以改变pytest一些默认的运行方式,如:用例收集规则,标签,命令行参数等等。

pytest.ini应该放哪里?

就放在项目根目录下 ,不要乱放,不要乱起其他名字

基本格式如下:

复制代码
# 新建pytest.ini文件,一般放在项目的顶级目录下,不能随意命名

[pytest]

addopts = -v --rerun=2 --count=2
xfail_strict = true
复制代码

使用pytest -h参看帮助文档

[pytest]
addopts = -s -v -p no:warnings --html=./report/report.html --alluredir ./report/result
testpaths = ./testCase
python_files = test_*.py
python_classes = Test*
python_functions = test_*

###

参数说明:

1,addopts

作用:addopts参数可以更改默认命令行选项,这个当我们在cmd输入一堆指令去执行用例的时候,就可以用该参数代替了,省去重复性的敲命令工作

比如:想测试完生成报告,失败重跑两次,一共运行两次,通过分布式去测试,如果在cmd中写的话,命令会很长

pytest -v --rerun=2 --count=2 --html=report.html --self-contained-html -n=auto

每次都这样敲不太现实,addopts就可以完美解决这个问题

加了addopts之后,我们在cmd中只需要敲pytest就可以生效了!!

2,更改测试用例收集规则

pytest默认的测试用例收集规则

文件名以 test_*.py 文件和 *_test.py

以 test_ 开头的函数

以 test 开头的类,不能包含 __init__ 方法

以 test_ 开头的类里面的方法

我们是可以修改或者添加这个用例收集规则的;当然啦,是建议在原有的规则上添加的,如下配置


[pytest]
python_files = test_* *_test test*
python_classes = test* test*
python_functions = test_* test*

3、指定搜索测试用例目录

testpaths = apicase     #指定用例搜索目录

testpaths (args):    

directories to search for tests when no files or directories are given in the command line.

4、排除搜索目录

norecursedirs = tmp* *plugins

5、指定mark标签

markers =
    smoke: this is smoke case
    login: this is login case

 

6、xfail_strict

设置xfail_strict = True可以让那些标记为@pytest.mark.xfail但实际通过显示XPASS的测试用例被报告为失败

xfail_strict = True

###

###

原文地址:https://www.cnblogs.com/andy0816/p/15815152.html