{"id":8531,"library":"pytest-helpers-namespace","title":"Pytest Helpers Namespace","description":"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.","status":"maintenance","version":"2021.12.29","language":"en","source_language":"en","source_url":"https://github.com/saltstack/pytest-helpers-namespace","tags":["pytest","plugin","helpers","testing","conftest"],"install":[{"cmd":"pip install pytest-helpers-namespace","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required by the plugin. Version 2021.3.24 and newer require pytest >= 6.1.1.","package":"pytest","optional":false}],"imports":[{"note":"The 'helpers' namespace is added to the 'pytest' object at runtime, not a direct importable module.","wrong":"from pytest_helpers_namespace import helpers","symbol":"pytest.helpers.register","correct":"import pytest\n\n@pytest.helpers.register\ndef my_helper_function():\n    ..."}],"quickstart":{"code":"import pytest\n\n# conftest.py\n# This line ensures the plugin is loaded if not automatically discovered\npytest_plugins = ['helpers_namespace']\n\n@pytest.helpers.register\ndef foo(bar):\n    \"\"\" This dumb helper function will just return what you pass to it \"\"\"\n    return bar\n\n@pytest.helpers.can.haz.register\ndef nested_helper(value):\n    return f'Nested: {value}'\n\n# test_example.py\ndef test_helper_namespace():\n    assert pytest.helpers.foo(True) is True\n    assert pytest.helpers.can.haz.nested_helper('test') == 'Nested: test'","lang":"python","description":"Define helper functions in your `conftest.py` using the `@pytest.helpers.register` decorator. These helpers, including nested ones, become available via `pytest.helpers` in your test functions without explicit imports. The `pytest_plugins = ['helpers_namespace']` declaration in `conftest.py` ensures the plugin is loaded."},"warnings":[{"fix":"Ensure your pytest installation is `pytest>=6.1.1`. Run `pip install --upgrade pytest`.","message":"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.","severity":"breaking","affected_versions":">=2021.3.24"},{"fix":"Stick to the public API (e.g., `pytest.helpers.register`). If you had custom import logic for internals, verify paths or adjust packaging.","message":"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.","severity":"breaking","affected_versions":">=2021.3.24"},{"fix":"Pass the *resolved* fixture value from the test function as an explicit argument to your helper function, or make your helper a fixture if it needs direct fixture access.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always include `pytest_plugins = ['helpers_namespace']` in your root `conftest.py` to ensure the plugin is explicitly loaded.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"1. Ensure the package is installed: `pip install pytest-helpers-namespace`. 2. Explicitly add `pytest_plugins = ['helpers_namespace']` to your `conftest.py` file.","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`.","error":"AttributeError: module 'pytest' has no attribute 'helpers'"},{"fix":"Modify 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)`.","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.","error":"TypeError: 'function' object is not subscriptable"},{"fix":"Review 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.","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.","error":"PytestUnhandledHookCallWarning: Hook 'pytest_namespace' is deprecated. Please use `pytest_configure` to add attributes to the `pytest` object."}]}