Pytest Helpers Namespace
pytest-helpers-namespace is a pytest plugin that provides a convenient namespace to register helper functions in your `conftest.py` file. This allows you to use these helper functions directly within your tests without needing explicit imports, simplifying test code and promoting reusability. The current version is 2021.12.29, and releases are generally infrequent, indicating a mature and stable but not actively developed project.
Common errors
-
AttributeError: module 'pytest' has no attribute 'helpers'
cause The `pytest-helpers-namespace` plugin has not been correctly loaded by pytest. This often happens if the plugin is not installed, or if the `pytest_plugins` declaration is missing in `conftest.py`.fix1. Ensure the package is installed: `pip install pytest-helpers-namespace`. 2. Explicitly add `pytest_plugins = ['helpers_namespace']` to your `conftest.py` file. -
TypeError: 'function' object is not subscriptable
cause You are attempting to use a pytest fixture function directly as a default argument in a helper registered with `@pytest.helpers.register`, and then treating it as its resolved value within the helper.fixModify your test to pass the *resolved value* of the fixture to the helper function as a regular argument, instead of using the fixture function as a default. Example: `def my_helper(data): ...` then in test: `def test_something(my_fixture): pytest.helpers.my_helper(my_fixture)`. -
PytestUnhandledHookCallWarning: Hook 'pytest_namespace' is deprecated. Please use `pytest_configure` to add attributes to the `pytest` object.
cause This specific warning refers to `pytest`'s *own* deprecated `pytest_namespace` hook. While `pytest-helpers-namespace` *provides* a `helpers` namespace, it does not use this deprecated hook itself in recent versions. This warning indicates *your own code* or another plugin might be using the old hook.fixReview your `conftest.py` and other custom plugins for any direct implementation of `pytest_namespace`. Replace it with logic inside `pytest_configure` as suggested by the warning.
Warnings
- breaking Starting with version 2021.3.24, pytest-helpers-namespace requires pytest version 6.1.1 or newer. Older versions of the plugin may be incompatible with newer pytest versions, and upgrading the plugin will necessitate upgrading pytest.
- breaking Version 2021.3.24 introduced a switch to a 'src' layout. While this primarily affects packaging and internal structure, users who might have relied on specific internal import paths (though not recommended) could experience import errors.
- gotcha Using pytest fixtures directly as default arguments in functions registered with `@pytest.helpers.register` will pass the fixture function itself, not its resolved value. This typically leads to `TypeError: 'function' object is not subscriptable` when trying to access attributes or call the fixture within the helper.
- gotcha The `pytest_plugins = ['helpers_namespace']` line in `conftest.py` is crucial for pytest to discover and load the plugin, making `pytest.helpers` available. Without it, or if there's an issue with automatic discovery (e.g., package not installed correctly), `pytest.helpers` will not exist.
Install
-
pip install pytest-helpers-namespace
Imports
- pytest.helpers.register
from pytest_helpers_namespace import helpers
import pytest @pytest.helpers.register def my_helper_function(): ...
Quickstart
import pytest
# conftest.py
# This line ensures the plugin is loaded if not automatically discovered
pytest_plugins = ['helpers_namespace']
@pytest.helpers.register
def foo(bar):
""" This dumb helper function will just return what you pass to it """
return bar
@pytest.helpers.can.haz.register
def nested_helper(value):
return f'Nested: {value}'
# test_example.py
def test_helper_namespace():
assert pytest.helpers.foo(True) is True
assert pytest.helpers.can.haz.nested_helper('test') == 'Nested: test'