跳转到主要内容

pytest插件,用于在ansible集合中运行单元测试

项目描述

pytest-ansible-units

VScode Overview VScode Debug

一个实验性的pytest插件,用于使用pytest运行ansible集合的单元测试。

描述

pytest-ansible-units是一个pytest插件,允许使用仅pytest运行ansible集合的单元测试。pytest可以从命令行或IDE中使用。

入门

依赖关系

安装pytest-ansible-units将安装以下内容

  • pytest

pytest-ansible-units需要Python 3.8或更高版本,ansible-core和pyyaml。

安装

python -m pip install pytest-ansible-units

需要安装 ansible-corepyyaml 来激活插件。

支持两种目录结构,任选其一都需要安装集合依赖。可以在默认用户位置或在选项 1 中的集合树结构中安装。

集合树

首选方法是将其正在开发的集合克隆到适当的集合树路径。这消除了对任何符号链接的需求,并且可以将在同一树结构中克隆正在开发的其它集合。

git clone <repo> collections/ansible_collections/<namespace>/<name>

注意

  • pytest 需要在集合目录的根目录中运行,紧邻集合的 galaxy.yml 文件。

浅树结构

另一种方法是允许浅目录结构。

git clone <repo>

注意

  • pytest 需要在集合目录的根目录中运行,紧邻集合的 galaxy.yml 文件。
  • 在存储库目录中创建一个集合目录,并将集合内容链接到其中。
  • /collections/ 添加到 .gitignore 中,因为不需要将其提交。
  • 使用这种方法,由于符号链接,ansible-test sanity 将失败。

执行程序

从命令行,从集合的根目录

pytest tests

帮助

可以将以下内容添加到集合的 pyproject.toml 文件中,以限制警告并设置集合测试的默认路径

[tool.pytest.ini_options]
testpaths = [
    "tests",
]
filterwarnings = [
    'ignore:AnsibleCollectionFinder has already been configured',
]

galaxy.yml 文件中的信息用于构建 collections 目录结构并链接内容。galaxy.yml 文件应反映正确的集合命名空间和名称。

在不运行测试的情况下检测问题的方法之一是运行

pytest --collect-only

可能会看到以下错误

E   ModuleNotFoundError: No module named 'ansible_collections'
  • 检查 galaxy.yml 文件以确保命名空间和名称准确
  • 确保 pytest 是从集合的根目录运行的,紧邻 galaxy.yml
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
  • 确保每个测试目录都有一个 __init__.py

作者

  • cidrblock

许可证

本项目采用 GPL-v3 许可证 - 请参阅 LICENSE 文件获取详细信息

项目详情


下载文件

下载适合您平台的文件。如果您不确定要选择哪个,请了解更多关于 安装包 的信息。

源分布

pytest-ansible-units-0.1.dev50.tar.gz (1.6 MB 查看哈希)

上传时间

支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面