前言
我们在执行用例的时候,每次都在命令行中输入-v,-s等一些命令行参数的时,比较麻烦。其中pytest.ini这个配置文件可以快速的帮助我们解决这个问题。
配置文件
pytest.ini文件是pytest的主配置文件,可以改变pytest的运行方式,且是一个固定的文件pytest.ini文件,pytest.ini一般存放在项目的根目录中。我们可以打开终端,在终端中输入pytest -h来查看一些pytest.ini参数配置详情,这里的参数比较多。
参数详解
这里面这么多参数可能目前用不上,只介绍一个常用的参数信息,比如,我们每次后面跟的参数信息。
addopts
addopts:表示可以更改默认命令行选项:
[pytest]
# 表示可以更改默认命令行选项
addopts = -v -s
这样我们直接在cmd中执行pytest就会默认加上以上2个参数。将我们print的内容和用例执行结果。
testpath
testpaths:表示执行用例的目录
[pytest]
addopts = -v -s
testpaths = test_01
python_file
python_file:表示执行文件名:
[pytest]
addopts = -v -s
testpaths = test_01
python_files = test_01.py
在test_01的目录下存放多个test文件,通过加入python_files来固定确认执行那一个测试文件内容:
python_classes
python_classes:表示执行的类名:
[pytest]
addopts = -v -s
testpaths = test_01
python_files = test_01.py
python_classes = Test_
在test_01.py文件中写入两个类,不同的类名。这样就只会执行标记的类内容
import pytest
class Test_ini:
def test01(self):
print('用例01')
def test02(self):
print('用例02')
def test03(self):
print('用例03')
class Testini:
def test_01(self):
print('用例01')
def test_02(self):
print('用例02')
def test_03(self):
print('用例03')<br>
if __name__ == '__main__':
pytest.main(['-s'])
通过执行,可以看到只执行了Test_的内容:
python_functions
python_functions:表示执行方法名内容:
[pytest]
addopts = -v -s
testpaths = test_01
python_files = test_01.py
python_classes = Test
python_functions = test_
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理