跳转到主要内容

操作符的固定值

项目描述

pytest-operator

PyTest 插件,用于简化 Operator Charms 的集成测试创建。

用法

在您的 tox.ini 文件的 deps 中包含 pytest-operator

[testenv]
deps =
  pytest
  pytest-operator

然后,只需在您的异步测试中使用 ops_test 通用 fixture。此模块级别的通用 fixture 提供了一个 libjuju Model、构建测试用例的 helper 工具,以及能够中止测试的能力,以便模块中剩余的测试自动标记为 xfailed(您也可以标记一个测试,以便在测试失败时自动执行此操作;这通常用于初始部署测试,后续测试依赖于部署成功)。

作为一个额外的便利功能,您无需显式使用 @pytest.mark.asyncio 标记异步测试;如果它是一个测试函数/方法,并且是异步的,它将自动标记。

示例

import pytest


@pytest.mark.abort_on_fail
async def test_build_and_deploy(ops_test):
    my_charm = await ops_test.build_charm(".")
    await ops_test.model.deploy(my_charm)
    await ops_test.model.wait_for_idle()


async def test_status(ops_test):
    assert ops_test.model.applications["my-charm"].units[0].workload_status == "active"

构建/下载 Charm 资源

在许多情况下,当 Charms 为集成测试做准备时,Charms 可能需要附加资源以便其正常运行。在这些情况下,集成代码必须构建这些资源或从外部资源拉取这些资源。

示例

async def test_build_and_deploy(ops_test):
    charm = await ops_test.build_charm(".")

    build_script = Path.cwd() / "build-charm-resources.sh"
    resources = await ops_test.build_resources(build_script)

    if resources:
        # created a dict from list of a filenames
        resources = {rsc.stem: rsc for rsc in resources}
    else:
        arch_resources = ops_test.arch_specific_resources(charm)
        resources = await ops_test.download_resources(
            charm, resources=arch_resources
        )
        
    assert resources, "Failed to build or download charm resources."
    
    log.info("Build Bundle...")
    bundle = ops_test.render_bundle(
        "tests/data/bundle.yaml", charm=charm, **resources
    )

    log.info("Deploy Bundle...")
    juju_cmd = ["deploy", "-m", ops_test.model_full_name, str(bundle)]
    rc, stdout, stderr = await ops_test.juju(*juju_cmd)
    assert rc == 0, f"Bundle deploy failed: {(stderr or stdout).strip()}"

    await ops_test.model.wait_for_idle()
    ...

参考

更多详细信息请参阅 参考文档

项目详情


发布历史 发布通知 | RSS 源

下载文件

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

源分发

pytest_operator-0.37.0.tar.gz (43.5 kB 查看哈希值)

上传时间

构建分发

pytest_operator-0.37.0-py3-none-any.whl (24.8 kB 查看哈希值)

上传时间 Python 3

由以下支持