Skip to main content

A pytest plugin to help with testing pop projects

Project description

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:

# Scoped for session because creating the hub is an expensive process
@pytest.fixture(scope="session")
def hub(hub):
    for dyne in ("exec", "states):
        hub.pop.sub.add(dyne_name=dyne)
    yield hub

The full hub you need is now available everywhere in pytest!

In fixtures:

@pytest.fixture
def my_fixture(hub):
    hub = hub

In modules:

def setup_module(module):
    hub = module.hub

def teardown_module(module):
    hub = module.hub

In functions:

def setup_function(function):
    hub = function.hub

def test_func(hub):
    hub = hub

def teardown_function(function):
    hub = function.hub

In classes:

class TestClass:
    @classmethod
    def setup_class(cls):
        hub = cls.hub

    def test_method(self):
        hub = self.hub

    @classmethod
    def teardown_class(cls):
        hub = cls.hub

Markers

Make use of pytest’s marking functionality

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.root
def test_root(hub):
    pass

expensive

Marks a test as being expensive. Run pytest with the ‘–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
def test_expensive(hub):
    pass

destructive

Marks a test as being destructive. Run pytest with the ‘–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
def test_destructive(hub):
    pass

slow

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

Example:

@pytest.mark.slow
def test_slow(hub):
    pass

Logging

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

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()

Include a fixture like this one in the conftest.py in the root of your unit test directory:

# Scope the mock_hub to a function so that the autospec gets reset after each use.
@pytest.fixture(scope="function")
def mock_hub(hub):
    mock_hub = hub.pop.testing.mock_hub()
    # replace mocked functions with necessary real ones
    # extend this on a per-module or per-function basis if necessary
    mock_hub.log = hub.log
    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")

Writing Tests

There’s some boiler plate code that may be useful to get you started in this repo’s test directory.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-pop-4.4.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

pytest_pop-4.4-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file pytest-pop-4.4.tar.gz.

File metadata

  • Download URL: pytest-pop-4.4.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for pytest-pop-4.4.tar.gz
Algorithm Hash digest
SHA256 4ac12ff1bc0e3136bad37ea6f66a5511cd72f85d350048ceccde80c758fd4dfd
MD5 140f38a4c200952083414f07e2a3a6f8
BLAKE2b-256 0431704edf0c49a40ae1925b308562e83a94bd0d4e09c957224c6755c4e90c4d

See more details on using hashes here.

File details

Details for the file pytest_pop-4.4-py3-none-any.whl.

File metadata

  • Download URL: pytest_pop-4.4-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for pytest_pop-4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3811952413ba40dff71dd4c869c0300c25429cc897b902df8db35bfa77e74c36
MD5 479fc0c80d17c039116749f49de414a9
BLAKE2b-256 4d40a6c3146b90a1636e6bc391b9cda6f611a66211e11be42785bb4ee823bd56

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page