python+pytest+pytest-html+allure集成测试案例
下面是python+pytest+pytest-html+allure四个组件同时集成使用的简单案例。
1. 项目结构
project/
│
├── src/
│ ├── __init__.py
│ ├── main.py
│
├── tests/
│ ├── __init__.py
│ ├── conftest.py
│ └── test_main.py
│
├── __init__.py
├── requirements.txt
├── pytest.ini
└── README.md
安装上述目录以此创建测试文件,文件内容参考如下。
2. 依赖文件
在requirements.txt文件中添加的依赖项,内容如下:
pytest
pytest-html
allure-pytest
requests
2. pytest 配置文件
创建配置文件pytest.ini:
[pytest]
addopts = --html=report.html --self-contained-html --alluredir=allure-results
其中:
--html=report.html --self-contained-html
为pytest-html的参数,运行测试后,会在当前路径下生成report.html的报告,同时html集成了CSS样式内容。
--alluredir=allure-results
为allure-pytest的参数,指定allure结果文件的路径。
3. 被测试的模块
src/main.py文件内容如下,实现一个接口,通过requests的get方法请求测试站点的资源。
def get_post(post_id):
import requests
response = requests.get(f"https://jsonplaceholder.typicode.