pytest-html-reporter 使用教程
1. 项目的目录结构及介绍
pytest-html-reporter/
├── README.md
├── pytest_html_reporter/
│ ├── __init__.py
│ ├── plugin.py
│ └── templates/
│ └── index.html
├── setup.py
└── tests/
└── test_sample.py
- README.md: 项目说明文档。
- pytest_html_reporter/: 核心代码目录。
- init.py: 模块初始化文件。
- plugin.py: 插件主文件,包含报告生成的逻辑。
- templates/: HTML模板文件夹。
- index.html: 报告的HTML模板。
- setup.py: 项目安装文件。
- tests/: 测试用例目录。
- test_sample.py: 示例测试文件。
2. 项目的启动文件介绍
项目的启动文件是 plugin.py
,它包含了报告生成的主要逻辑。该文件定义了 pytest 插件的钩子函数,用于在测试运行结束后生成 HTML 报告。
# plugin.py
import pytest
from .reporter import generate_report
def pytest_addoption(parser):
parser.addoption("--html-report", action="store", default="report", help="Path to the HTML report")
def pytest_terminal_summary(terminalreporter, exitstatus, config):
report_path = config.getoption("--html-report")
generate_report(terminalreporter, report_path)
3. 项目的配置文件介绍
项目的配置文件是 setup.py
,它用于定义项目的元数据和依赖项,以便通过 pip
安装。
# setup.py
from setuptools import setup, find_packages
setup(
name='pytest-html-reporter',
version='0.2.9',
description='Generates a static html report based on pytest framework',
author='Prashanth Sams',
packages=find_packages(),
install_requires=[
'pytest>=3.5',
],
entry_points={
'pytest11': [
'html-reporter = pytest_html_reporter.plugin',
],
},
)
通过 setup.py
文件,可以安装项目及其依赖项:
pip install .
这样,你就可以在 pytest 测试中使用 --html-report
选项来生成 HTML 报告了。