elchupanebrej / pytest-bdd-ng

BDD library for the py.test runner
https://pytest-bdd-ng.readthedocs.io/en/default/
Other
14 stars 3 forks source link

TypeError Exception when pytest fixture returns unhashable type #115

Open GamalielMasters opened 1 month ago

GamalielMasters commented 1 month ago

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 commented 1 month ago

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.