Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError Exception when pytest fixture returns unhashable type #115

Open
GamalielMasters opened this issue Oct 3, 2024 · 1 comment
Open

Comments

@GamalielMasters
Copy link

GamalielMasters commented Oct 3, 2024

After adding and configuring pytest-bdd-ng I can no longer run any of my unit tests because pytest crashes with the following exception:

tests/unit/intake/test_cleaning_processors.py:None (tests/unit/intake/test_cleaning_processors.py)
../../.venv/lib/python3.12/site-packages/pytest_bdd/collector.py:17: in collect
    StepHandler.Registry.inject_registry_fixture_and_register_steps(self.obj)
../../.venv/lib/python3.12/site-packages/pytest_bdd/steps.py:419: in inject_registry_fixture_and_register_steps
    obj.step_registry.__pytest_bdd_step_registry__.register_steps(steps)
../../.venv/lib/python3.12/site-packages/pytest_bdd/steps.py:427: in register_steps
    self.register_step_definition(step_definition)
../../.venv/lib/python3.12/site-packages/pytest_bdd/steps.py:422: in register_step_definition
    self.registry.add(step_definition)
E   TypeError: unhashable type: 'dict'

The following is my configuration (pyproject.toml):

[tool.pytest.ini_options]
# recognize test function which start with it_ as well as test_
python_functions = "it_* test_*"
python_classes = "Describe*"
filterwarnings = [
    'ignore:function ham\(\) is deprecated:DeprecationWarning',
    'ignore:.*pkg_resources is deprecated as an API.*:DeprecationWarning',
    'ignore:.*Deprecated call to `pkg_resources.declare_namespace.*:DeprecationWarning',
    'ignore:.*distutils Version classes are deprecated.*:DeprecationWarning',
]
bdd_features_base_dir = "features"
disable_feature_autoload = true

The crash occurs as soon as my first unit test module is being collected. I think that the "problem" has something to do with the fact that I have fixtures in my conftest.py files (for my unit tests) which return dictionaries...

I'm not certain why pytest-bdd-ng is attempting to execute and cache all the fixture results, if that is in fact what's happening. See update and workaround below.

@GamalielMasters GamalielMasters changed the title TypeError Exception TypeError Exception when pytest fixture returns unhashable type Oct 3, 2024
@GamalielMasters
Copy link
Author

Narrowed this down quite a bit more. It has nothing to do with my original idea... in fact, it occurs because the following line appeared in the referenced test file:

from unittest.mock import call

If I do the import as:

import unitest.mock as mock
.
.
.
...mock.call...

it works fine however...

Something about injecting "call" into the namespace of the module causes the above exception. You don't even have to use it, simply adding a test file with just the import line to the project will cause collection to terminate with the exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant