pytest.ini配置文件

一,概述

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

  • 基本格式如下:

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

addopts = -v --rerun=2 --count=2 xfail_strict = true

二,常用配置

1、addopts

更改默认命令行选项,当我们用命令行运行时,需要输入多个参数,很不方便。比如想测试完生成报告,失败重跑两次,一共运行两次,通过分布式去测试,如果在命令行中执行的话,命令会很长,很不方便

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

这时可以在pytest.ini文件通过addopts 加上子而写参数:

# 命令行参数
addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto

在命令行中只需要输入pytest就可以默认以这些参数去执行了。

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

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

文件名以 test_*.py 文件和 *test.py
以 test
开头的函数
以 Test 开头的类,不能包含 init 方法

如果需要修改这些规则,可以在pytest.ini文件中加入以下配置:

#测试用例收集规则
python_files =  test_*.py  *_test.py    # 文件名 (多个匹配规则以空格分开)
python_classes = Test*                      # 类
python_functions = test_*                  # 函数
3、指定搜索测试用例目录

只收集apicase目录下的测试用例:

testpaths = apicase     #指定用例搜索目录
4、排除搜索目录

不搜索tmp的前缀文件和plugins的后缀文件:

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

你可能感兴趣的:(pytest.ini配置文件)