UI自动化测试 - 最简 pytest 自动化测试项目实现

张开发
2026/6/5 9:18:13 15 分钟阅读
UI自动化测试 - 最简 pytest 自动化测试项目实现
UIAutotest - 最简 pytest 自动化测试项目本项目是一个极简的 pytest 测试框架模板,用于快速理解 pytest 的基本组成和运行原理。可作为后续扩展为 Web/移动端自动化测试的起点。项目结构UIAutotest/ ├── README.md # 项目说明(本文件) ├── README.en.md # 英文说明(可选) ├── requirements.txt # Python 依赖列表 ├── pytest.ini # pytest 配置文件 ├── conftest.py # pytest 扩展点(fixture、钩子) └── testcases/ # 测试用例目录 ├── __init__.py # 标识 testcases 为 Python 包 └── test_simple.py # 示例测试用例文件详解1. requirements.txt作用:列出项目所需的 Python 包及其版本。pytest原理:通过pip install -r requirements.txt可一键安装所有依赖,保证环境一致性。这里只依赖 pytest 核心包,不包含任何额外插件。2. pytest.ini作用:pytest 的配置文件,用于定义全局运行行为。[pytest] testpaths = testcases python_files = test_*.py python_classes = Test* python_functions = test_* addopts = -v原理:testpaths:指定测试目录,pytest 只在此目录下搜索测试文件。python_files:指定哪些文件被视为测试文件。python_classes:指定哪些类被视为测试类。python_functions:指定哪些函数被视为测试函数。addopts:添加默认的命令行参数,如-v表示输出详细信息。3. conftest.py作用:提供 pytest 的扩展点,可以定义 fixture、钩子函数等。当前内容:importosimportpytest@pytest.fixturedefdata_dir():"""返回测试数据目录路径,便于测试用例读取数据文件"""base

更多文章