pytest-operator


Namepytest-operator JSON
Version 0.33.0 PyPI version JSON
download
home_pagehttps://github.com/charmed-kubernetes/pytest-operator
SummaryFixtures for Operators
upload_time2024-02-23 19:16:03
maintainer
docs_urlNone
authorCory Johns
requires_python
licenseMIT license
keywords pytest py.test operators yaml asyncio
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pytest-operator

PyTest plugin to make it easy to create integration tests for Operator Charms.

## Usage

Include `pytest-operator` in the `deps` of your `tox.ini` file:

```ini
[testenv]
deps =
  pytest
  pytest-operator
```

Then, just start using the `ops_test` fixture in your async tests.  This
module-scoped fixture provides a libjuju Model, helpers to build charms for
testing, and the ability to abort testing so that the remaining tests in the
module are automatically xfailed (you can also mark a test so that this happens
automatically if the test fails; this is typically used on the initial deployment
test, where subsequent tests depend on the deployment having succeeded).

As an additional nicety, you don't have to explicitly mark an async test with
`@pytest.mark.asyncio`; if it's a test function / method and it's async, it
will be marked automatically.

Example:

```python
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"
```

## Building/Downloading Charm Resources
Quite often, when charms are preparing for integration tests, the charms may
need to attach resources to the charm for it to function. In these cases, 
the integration code must either build the resources or pull those from external resources.

Example:

```python
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()
    ...
```



## Reference

More details can be found in [the reference docs](docs/reference.md).

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/charmed-kubernetes/pytest-operator",
    "name": "pytest-operator",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "pytest,py.test,operators,yaml,asyncio",
    "author": "Cory Johns",
    "author_email": "cory.johns@canonical.com",
    "download_url": "https://files.pythonhosted.org/packages/75/cc/93cc8b4fd7060c4d54efb174737265b39eea297bd7f971f4bf0aafd1af0e/pytest-operator-0.33.0.tar.gz",
    "platform": null,
    "description": "# pytest-operator\n\nPyTest plugin to make it easy to create integration tests for Operator Charms.\n\n## Usage\n\nInclude `pytest-operator` in the `deps` of your `tox.ini` file:\n\n```ini\n[testenv]\ndeps =\n  pytest\n  pytest-operator\n```\n\nThen, just start using the `ops_test` fixture in your async tests.  This\nmodule-scoped fixture provides a libjuju Model, helpers to build charms for\ntesting, and the ability to abort testing so that the remaining tests in the\nmodule are automatically xfailed (you can also mark a test so that this happens\nautomatically if the test fails; this is typically used on the initial deployment\ntest, where subsequent tests depend on the deployment having succeeded).\n\nAs an additional nicety, you don't have to explicitly mark an async test with\n`@pytest.mark.asyncio`; if it's a test function / method and it's async, it\nwill be marked automatically.\n\nExample:\n\n```python\nimport pytest\n\n\n@pytest.mark.abort_on_fail\nasync def test_build_and_deploy(ops_test):\n    my_charm = await ops_test.build_charm(\".\")\n    await ops_test.model.deploy(my_charm)\n    await ops_test.model.wait_for_idle()\n\n\nasync def test_status(ops_test):\n    assert ops_test.model.applications[\"my-charm\"].units[0].workload_status == \"active\"\n```\n\n## Building/Downloading Charm Resources\nQuite often, when charms are preparing for integration tests, the charms may\nneed to attach resources to the charm for it to function. In these cases, \nthe integration code must either build the resources or pull those from external resources.\n\nExample:\n\n```python\nasync def test_build_and_deploy(ops_test):\n    charm = await ops_test.build_charm(\".\")\n\n    build_script = Path.cwd() / \"build-charm-resources.sh\"\n    resources = await ops_test.build_resources(build_script)\n\n    if resources:\n        # created a dict from list of a filenames\n        resources = {rsc.stem: rsc for rsc in resources}\n    else:\n        arch_resources = ops_test.arch_specific_resources(charm)\n        resources = await ops_test.download_resources(\n            charm, resources=arch_resources\n        )\n        \n    assert resources, \"Failed to build or download charm resources.\"\n    \n    log.info(\"Build Bundle...\")\n    bundle = ops_test.render_bundle(\n        \"tests/data/bundle.yaml\", charm=charm, **resources\n    )\n\n    log.info(\"Deploy Bundle...\")\n    juju_cmd = [\"deploy\", \"-m\", ops_test.model_full_name, str(bundle)]\n    rc, stdout, stderr = await ops_test.juju(*juju_cmd)\n    assert rc == 0, f\"Bundle deploy failed: {(stderr or stdout).strip()}\"\n\n    await ops_test.model.wait_for_idle()\n    ...\n```\n\n\n\n## Reference\n\nMore details can be found in [the reference docs](docs/reference.md).\n",
    "bugtrack_url": null,
    "license": "MIT license",
    "summary": "Fixtures for Operators",
    "version": "0.33.0",
    "project_urls": {
        "Homepage": "https://github.com/charmed-kubernetes/pytest-operator"
    },
    "split_keywords": [
        "pytest",
        "py.test",
        "operators",
        "yaml",
        "asyncio"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6c546784bc854f8e45bcc27a0b864633fba36b0a44c2311541b25c3be324c655",
                "md5": "e3a235c3b09f6caffe5c621cee014ce4",
                "sha256": "b1f7bc19155a204b86c1957d56fba5e667a1782b63689ef6c9e411e83af1c018"
            },
            "downloads": -1,
            "filename": "pytest_operator-0.33.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e3a235c3b09f6caffe5c621cee014ce4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 22406,
            "upload_time": "2024-02-23T19:16:01",
            "upload_time_iso_8601": "2024-02-23T19:16:01.361946Z",
            "url": "https://files.pythonhosted.org/packages/6c/54/6784bc854f8e45bcc27a0b864633fba36b0a44c2311541b25c3be324c655/pytest_operator-0.33.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "75cc93cc8b4fd7060c4d54efb174737265b39eea297bd7f971f4bf0aafd1af0e",
                "md5": "9ee6328f945d006e394b62e8c75bc8ba",
                "sha256": "97d5c1890c3a012edde7c5bb1e8659aa9c9b9a0c4b4293af1a319885100d0696"
            },
            "downloads": -1,
            "filename": "pytest-operator-0.33.0.tar.gz",
            "has_sig": false,
            "md5_digest": "9ee6328f945d006e394b62e8c75bc8ba",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 22603,
            "upload_time": "2024-02-23T19:16:03",
            "upload_time_iso_8601": "2024-02-23T19:16:03.628167Z",
            "url": "https://files.pythonhosted.org/packages/75/cc/93cc8b4fd7060c4d54efb174737265b39eea297bd7f971f4bf0aafd1af0e/pytest-operator-0.33.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-23 19:16:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "charmed-kubernetes",
    "github_project": "pytest-operator",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pytest-operator"
}
        
Elapsed time: 0.18520s