使用配置文件后可以快速的使用配置的项来选择执行哪些测试模块
使用方法:
- 项目下新建scripts模块
- 将测试脚本文件放到scripts中
- pytest的配置文件放到自动化项目目录下
- 名称为pytest.ini
- 命令行运行时会使用该配置文件中的配置
- 第一行内容为[pytest]
配置文件参数说明
[pytest] # 标识当前配置文件是pytest的配置文件
addopts = -s -v # 标识pytest执行时增加的参数
testpaths = ./scripts # 匹配搜索的目录
python_files = test_*.py # 匹配测试文件
python_classes = Test* # 匹配测试类
python_functions = test_* # 匹配测试方法
[pytest]
addopts = -s -v
testpaths = ./scripts
python_files = test_*.py
python_classes = Test*
python_functions = test_*
配置文件的命名:pytest.ini tox.ini setup.cfg
测试代码
import time
def add(x, y):
return x+y
class TestAdd:
# 添加类级别的初始化操作方法
def setup_class(self):
print("测试类开始执行时间:", time.strftime("%Y-%m-%d %H:%M:%S"))
# 添加类级别的销毁操作方法
def teardown_class(self):
print("测试类结束执行时间:", time.strftime("%Y-%m-%d %H:%M:%S"))
def setup(self):
print("测试用例开始执行时间:", time.strftime("%Y-%m-%d %H:%M:%S"))
def test_add_01(self):
result = add(1, 2)
assert result == 3
def test_add_02(self):
result = add(2, 2)
assert result == 4
def teardown(self):
print("测试用例结束时间:", time.strftime("%Y-%m-%d %H:%M:%S"))
结果