Skip to main content

A pytest plugin to help with testing pop projects

INSTALLATION

Install with pip:

pip install pytest-pop

DEVELOPMENT INSTALLATION

Clone the pytest-pop repo and install with pip:

git clone https://gitlab.com/saltstack/pop/pytest-pop.git
pip install -e pytest-pop

Getting the Hub

Extend the hub fixture in your conftest.py so that it includes your subs:

@pytest.fixture(scope="function")
def hub(hub):
    # TODO Add dynes that will be used for your tests
    for dyne in ("exec", "states"):
        hub.pop.sub.add(dyne_name=dyne)
        if dyne in ("corn", "exec", "states"):
            hub.pop.sub.load_subdirs(getattr(hub, dyne), recurse=True)

    args = [
        # TODO patch in whatever cli args are necessary to run your test
    ]
    with mock.patch("sys.argv", ["pytest-pop"] + args):
        hub.pop.config.load(["pytest_pop"], "pytest_pop")

    yield hub

    # TODO Hub cleanup
    pass

Markers

Make use of pytest markers from pytest-salt-factoroies

root

Marks a test as needing elevated privileges. On UNIX-like systems the test will be skipped if the user running the tests is not root. On Windows systems the test will be skipped if the tests aren’t run with admin privileges.

Example:

@pytest.mark.skip_if_not_root
def test_root(hub):
    pass

expensive

Marks a test as being expensive. Run pytest with the ‘–run-expensive’ flag or set the EXPENSIVE_TESTS environment variable to “True” to run these tests. By default they will be skipped

Example:

@pytest.mark.expensive_test
def test_expensive(hub):
    pass

destructive

Marks a test as being destructive. Run pytest with the ‘–run-destructive’ flag or set the DESTRUCTIVE_TESTS environment variable to “True” to run these tests. By default they will be skipped

Example:

@pytest.mark.destructive_test
def test_destructive(hub):
    pass

Logging

You can use the hub to log without setting up a logger in every single file that uses a hub

Example:

hub.log.debug("debug message")

Be sure to run pytest with ‘–cli-log-level=10’ in order to see debug messages

Mocking

Get access to a fully mocked/autospecced version of the hub with:

mock_hub = hub.pop.testing.mock_hub()

A mock_hub fixture with common substitutions of real plugins is available as a fixture:

def test_thing(mock_hub):
    pass

Extend the mock hub in your own fixture:

# Scope the mock_hub to a function so that the autospec gets reset after each use.
@pytest.fixture(scope="function")
def mock_hub(mock_hub, hub):
    # replace mocked functions with necessary real ones
    # extend this on a per-module or per-function basis if necessary
    mock_hub.sub.func = hub.sub.func
    yield mock_hub

You can now do autospec assertions on contracted functions:

import project.sub.plugin as plugin

def test_cmd_run(mock_hub):
    plugin.func(mock_hub, "arg")
    mock_hub.sub.plugin.func.assert_called_with("arg")

ACCT

Some projects, specifically idem-cloud need credentials from idem’s ctx generator. A ctx fixture exists, but it won’t work unless you override the acct_file and acct_profile fixtures:

@pytest.fixture
def acct_subs() -> List[str]:
    return ["azurerm", "vultr"]


@pytest.fixture
def acct_profile() -> str:
    return "test_development_idem_cloud"

Once these fixtures are overridden, the ctx fixture will become available to your test:

test_cloud_instance_present(hub, ctx):
    hub.state.cloud.present(ctx, "instance_name")

Examples

Mock a hub exec function:

with patch.object(mock_hub.exec, 'dummy', return_value="some result") as mock_exec:
    pass

Set return value for mock_hub function:

mock_hub.sub.function.return_value = "Pass"

Mock hub assert called with:

mock_hub.sub.function.assert_called_with("myinput", True)

Metadata

Release files for pytest-pop 9.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-pop 9.0.0
File Size Uploaded
pytest-pop-9.0.0.tar.gz 14.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-pop 9.0.0
File Interpreter ABI Platform
pytest_pop-9.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 26.7 kB

Release files / pytest-pop-9.0.0.tar.gz

Download URL pytest-pop-9.0.0.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1effc85e599f3087f273fbb8bd7c05e108f0d27b26aee6ee3ee363b859d69e47
BLAKE2b-256 checksum
How to use checksums
f9efe2a9dbcbf747fe60b3974e3d9c39c4b88200b3ceb2740db285e4c3d1b106
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/35.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

Release files / pytest_pop-9.0.0-py3-none-any.whl

Download URL pytest_pop-9.0.0-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ffd58498261d85938ee056328dca3195c0f2c9e7ff609b5866bdd14298978caa
BLAKE2b-256 checksum
How to use checksums
31739e741cb9c7556e37a2b4bc67116c3ca725ba111fc6a1332f54a459216c6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/35.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

Release history Release notifications | RSS feed

11.0.0

2 release files

10.0.0

2 release files

This release

9.0.0 This release

2 release files

8.0.1

2 release files

8.0.0

2 release files

7.0.1

2 release files

7.0.0

2 release files

6.5.0

2 release files

6.4

2 release files

6.3

2 release files

6.2

2 release files

6.1

2 release files

6.0

2 release files

5.3

2 release files

5.2

2 release files

5.1

2 release files

5

2 release files

4.4

2 release files

4

2 release files

3

2 release files

2.5

2 release files

1.1

2 release files

1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page