pytest-testinfra 开源项目教程
1. 项目的目录结构及介绍
pytest-testinfra 是一个用于测试基础设施的 pytest 插件。以下是其主要目录结构和介绍:
pytest-testinfra/
├── CHANGELOG.rst
├── CONTRIBUTING.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── docs/
│ ├── conf.py
│ ├── index.rst
│ └── ...
├── pytest_testinfra/
│ ├── __init__.py
│ ├── backend/
│ │ ├── __init__.py
│ │ ├── base.py
│ │ ├── ...
│ ├── plugin.py
│ └── ...
├── setup.cfg
├── setup.py
└── tox.ini
CHANGELOG.rst
: 项目变更日志。CONTRIBUTING.rst
: 贡献指南。LICENSE
: 项目许可证。MANIFEST.in
: 清单文件,用于包含或排除某些文件。README.rst
: 项目介绍和使用说明。docs/
: 项目文档目录。conf.py
: Sphinx 文档配置文件。index.rst
: 文档主页。
pytest_testinfra/
: 项目核心代码目录。__init__.py
: 模块初始化文件。backend/
: 后端实现目录。base.py
: 后端基类。
plugin.py
: pytest 插件实现。
setup.cfg
: 安装配置文件。setup.py
: 安装脚本。tox.ini
: tox 配置文件,用于自动化测试。
2. 项目的启动文件介绍
pytest-testinfra 的启动文件主要是 pytest_testinfra/plugin.py
。这个文件定义了 pytest 插件的核心功能,包括如何加载和运行测试。
# pytest_testinfra/plugin.py
import pytest
from . import backend
def pytest_addoption(parser):
group = parser.getgroup("testinfra")
group.addoption("--connection", action="store", default="ssh", help="Connection type to use")
group.addoption("--hosts", action="store", default=None, help="Hosts to connect to")
# 其他选项...
def pytest_configure(config):
config.addinivalue_line("markers", "testinfra: mark a test as a testinfra test.")
backend_class = backend.get_backend_class(config.getoption("--connection"))
hosts = config.getoption("--hosts")
# 其他配置...
# 其他功能...
pytest_addoption
: 添加命令行选项,如连接类型和主机。pytest_configure
: 配置 pytest,加载后端类和主机信息。
3. 项目的配置文件介绍
pytest-testinfra 的配置文件主要是 setup.cfg
和 tox.ini
。
setup.cfg
setup.cfg
是一个标准的 Python 包配置文件,用于定义包的元数据和安装选项。
[metadata]
name = pytest-testinfra
version = attr: pytest_testinfra.__version__
description = Testinfra test your infrastructures
long_description = file: README.rst, CHANGELOG.rst
license = MIT
author = Philippe Pepiot
author_email = phil@philpep.org
url = https://github.com/pytest-dev/pytest-testinfra
classifiers =
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
License :: OSI Approved :: MIT License
Programming Language :: Python :: 3
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
[options]
packages = find:
install_requires =
pytest>=3.0
paramiko
salt
ansible
docker
kubernetes
[options.extras_require]
doc =
sphinx
sphinx_rtd_theme
[tool:pytest]
addopts = --strict-markers
testpaths = tests