pytest(三)配置文件

本文介绍了pytest的配置文件,包括pytest.ini、tox.ini和setup.cfg的使用,重点讲解了配置文件的内容,如addopts、testpaths等选项,以及如何通过配置文件执行测试用例。还提供了一个配置文件执行实例,演示了如何创建测试文件和配置文件,以及如何运行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、配置文件

1. pytest配置文件主要用于定位测试文件及测试类和方法,更便捷的执行测试;

配置文件只允许使用以下三个文件名称:

pytest.ini

tox.ini

setup.cfg

2. 配置文件内容

[pytest] ------------------------------表示该文件为pytest的配置文件

addopts = -s -v---------------------执行用例的参数

testpaths = ./scripts --------------执行用例的存放目录,表示当前文件目录下的scripts文件夹

python_files = test_*.py ---------测试文件名称类型的匹配

python_classes = Test* ----------测试类名称的匹配

python_functions = test_*--------测试方法名称的匹配

二、配置文件执行实例

1. 新建一个测试用例的py文件,文件名称为test_add_001.py,并将它放入文件夹scripts中,内容如下:

import time


def add(a,b):
    return a + b
# 定义一个pytest类
class Test_add:
    # 添加类级别的初始化操作方法
    def setup_class(self):
        print("测试类的开始执行时间:" + time.strftime("%Y-%M-%D %H:%M:S"))
    # 添加类级别的销毁操作方法
    def teardown_class(self):
        print("测试类的结束执行时间:
pytest是一个Python的单元测试框架,而allure是一个生成测试报告的工具。在pytest中使用allure可以生成漂亮的测试报告,可以方便地查看测试结果和测试覆盖率等信息。 要使用allure,需要安装allure-pytest插件,并在pytest配置文件中添加allure的配置。以下是一个示例配置文件: ``` # content of pytest.ini [pytest] addopts = --alluredir=/path/to/allure/reports --clean-alluredir --disable-warnings -v -s -r a --tb=short --strict-markers --color=yes --maxfail=2 markers = smoke: Run the smoke test cases regression: Run the regression test cases # Allure configuration [allure] # The directory to store the allure report # The directory should be created before running the tests # The directory should be empty or not exist before running the tests # The directory should not be the same as the directory specified in the addopts option # The directory should not be a subdirectory of the directory specified in the addopts option # The directory should not be a parent directory of the directory specified in the addopts option # The directory should not contain spaces or special characters # The directory should not be a network drive or a symbolic link # The directory should not be a temporary directory or a system directory # The directory should have write permission allure_report_dir = /path/to/allure/report # The title of the report allure_report_title = My Test Report # The description of the report allure_report_description = This is a test report generated by pytest and allure # The environment information allure_report_environment = {'Browser': 'Chrome', 'OS': 'Windows 10'} ``` 在配置文件中,`addopts`选项用于指定pytest的命令行选项,`markers`选项用于定义测试用例的标记。`allure`选项用于配置allure的相关信息,包括报告存储路径、报告标题、报告描述和环境信息等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值