一、前言
pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行
二、ini配置文件
pytest里面有些文件是非test文件
- pytest.ini : pytest的主配置文件,可以改变pytest的默认行为
- conftest.py : 测试用例的一些fixture配置集中管理
- _init_.py : 识别该文件夹为python的package包
- tox.ini与pytest.ini类似,用tox工具时候才有用
- setup.cfg也是ini格式文件,影响setup.py的行为
ini文件基本格式
使用pytest --help指令可以查看pytest.ini的设置选项
三、mark标记
mark标记可以让用户自定义标记,如下案例,使用了2个标签:webtest和hello,使用mark标记功能对于以后分类测试非常有用
#!encoding=utf-8
import pytest
#自定义mark
@pytest.mark.webtest
def test_send_http():
print('mark web test')
def test_something_quick():
pass
def test_another():
pass
#自定义mark
@pytest.mark.hello
class TestClass:
def test_01(self):
print('hello')
def test_02(self):
print('hello world')
if __name__=="__main__":
pytest.main(['-s','test_class021.py','-m=hello'])
运行结果
================================================== test session starts ===================================================
platform darwin -- Python 3.7.1, pytest-5.1.2, py-1.7.0, pluggy-0.13.0
rootdir: /Users/luozelin/D