pytest入门十:配置文件

  • pytest.ini:pytest的主配置文件,可以改变pytest的默认行为
  • conftest.py:测试用例的一些fixture配置

pytest.ini

复制代码
marks
mark 打标的执行 @pytest.mark.add  add需要些marks配置否则报warning
[pytest]
markers=
  add:测试打标

测试用例中添加了 @pytest.mark.webtest 装饰器,如果不添加marks选项的话,就会报warnings

pytest

markers =

smoke

end

fist
xfail_strict

设置xfail_strict = True可以让那些标记为@pytest.mark.xfail但实际通过显示XPASS的测试用例被报告为失败

addopts

addopts参数可以更改默认命令行选项,这个当我们在cmd输入一堆指令去执行用例的时候,就可以用该参数代替了,省去重复性的敲命令工作

命令行参数

addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto

加了addopts之后,我们在cmd中只需要敲pytest就可以生效了!!

log_cli

控制台实时输出日志

格式:log_cli=True 或False(默认),或者log_cli=1 或 0
norecursedirs

pytest 收集测试用例时,会递归遍历所有子目录,包括某些你明知道没必要遍历的目录,遇到这种情况,可以使用 norecursedirs 参数简化 pytest 的搜索工作
pytest默认的测试用例收集规则

;指定测试目录

;定义执行的目录

testpaths=tests

;自定义测试文件命名规则

python_files = test_* check_*

;自定义测试类

python_classes = Test* Check*

;自定义测试方法

python_functions = test_* check_*

;指定特定路径:当在上级路径时,也会默认执行sub_demo

;testpaths = sub_demo

;忽略路径

norecursedirs = result logs datas

指定pytest最低版本号

minversion=5.0

conftest.py

conftest.py

import pytest

@pytest.fixture()

def conftest_fixture():

print("fixture前置")

yield

print("fixture后置")
test_demo.py

def test_case(conftest_fixture):

print("测试用例")
运行结果

============================= test session starts =============================

collecting ... collected 1 item

test_demo.py::test_case fixture前置

PASSED [100%]测试用例

fixture后置

============================== 1 passed in 0.02s ==============================

相关推荐
我的xiaodoujiao19 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 24--数据驱动--参数化处理 Excel 文件 1
python·学习·测试工具·pytest
西游音月4 天前
(2)pytest+Selenium自动化测试-环境准备
selenium·测试工具·pytest
我的xiaodoujiao4 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 23--数据驱动--参数化处理 Yaml 文件
python·学习·测试工具·pytest
我的xiaodoujiao7 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 22--数据驱动--参数化处理 Json 文件
python·学习·测试工具·pytest
胜天半月子8 天前
Python自动化测试 | 快速认识并了解pytest的基本使用
服务器·python·pytest
北珣.12 天前
自动化框架pytest基础
自动化·pytest
程序员杰哥13 天前
Pytest之收集用例规则与运行指定用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
学习3人组13 天前
Python + requests + pytest + allure + Jenkins 构建完整的接口自动化测试框架
python·jenkins·pytest
shao91851616 天前
Gradio全解14——使用Gradio构建MCP的服务器与客户端(4)——Python包命令:uv与uvx实战
pytest·uv·1024程序员节·npx·uvx·uv pip·ruff
我的xiaodoujiao17 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 20--PO(POM) 设计模式和用例撰写
python·学习·测试工具·设计模式·pytest