Skip to content

Fixture fails with "module" scope and numpy array as value #6497

@kohr-h

Description

@kohr-h

Minimal failing example:

import numpy as np import pytest @pytest.fixture(scope="module", params=[0, np.ones(2)]) def my_fixture(request): return request.param def test_stuff(my_fixture): pass

pytest output:

=============================================== test session starts ================================================ platform linux -- Python 3.8.1, pytest-5.3.3, py-1.8.1, pluggy-0.13.1 rootdir: /home/hkohr/git collected 2 items tmp.py .E [100%] ====================================================== ERRORS ====================================================== ____________________________________ ERROR at setup of test_stuff[my_fixture1] _____________________________________ cls = <class '_pytest.runner.CallInfo'>, func = <function call_runtest_hook.<locals>.<lambda> at 0x7f8b16fb03a0> when = 'setup', reraise = (<class '_pytest.outcomes.Exit'>, <class 'KeyboardInterrupt'>) @classmethod def from_call(cls, func, when, reraise=None) -> "CallInfo": #: context of invocation: one of "setup", "call", #: "teardown", "memocollect" start = time() excinfo = None try: > result = func() ../miniconda/envs/tmp/lib/python3.8/site-packages/_pytest/runner.py:235: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ../miniconda/envs/tmp/lib/python3.8/site-packages/_pytest/runner.py:208: in <lambda> lambda: ihook(item=item, **kwds), when=when, reraise=reraise ../miniconda/envs/tmp/lib/python3.8/site-packages/pluggy/hooks.py:286: in __call__ return self._hookexec(self, self.get_hookimpls(), kwargs) ../miniconda/envs/tmp/lib/python3.8/site-packages/pluggy/manager.py:93: in _hookexec return self._inner_hookexec(hook, methods, kwargs) ../miniconda/envs/tmp/lib/python3.8/site-packages/pluggy/manager.py:84: in <lambda> self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall( ../miniconda/envs/tmp/lib/python3.8/site-packages/_pytest/runner.py:120: in pytest_runtest_setup item.session._setupstate.prepare(item) ../miniconda/envs/tmp/lib/python3.8/site-packages/_pytest/runner.py:365: in prepare col.setup() ../miniconda/envs/tmp/lib/python3.8/site-packages/_pytest/python.py:1449: in setup fixtures.fillfixtures(self) ../miniconda/envs/tmp/lib/python3.8/site-packages/_pytest/fixtures.py:294: in fillfixtures request._fillfixtures() ../miniconda/envs/tmp/lib/python3.8/site-packages/_pytest/fixtures.py:473: in _fillfixtures item.funcargs[argname] = self.getfixturevalue(argname) ../miniconda/envs/tmp/lib/python3.8/site-packages/_pytest/fixtures.py:483: in getfixturevalue return self._get_active_fixturedef(argname).cached_result[0] ../miniconda/envs/tmp/lib/python3.8/site-packages/_pytest/fixtures.py:499: in _get_active_fixturedef self._compute_fixture_value(fixturedef) ../miniconda/envs/tmp/lib/python3.8/site-packages/_pytest/fixtures.py:580: in _compute_fixture_value fixturedef.execute(request=subrequest) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <FixtureDef argname='my_fixture' scope='module' baseid='tmp.py'> request = <SubRequest 'my_fixture' for <Function test_stuff[my_fixture1]>> def execute(self, request): for argname in self._dependee_fixture_argnames(request): fixturedef = request._get_active_fixturedef(argname) if argname != "request": fixturedef.addfinalizer(functools.partial(self.finish, request=request)) my_cache_key = self.cache_key(request) cached_result = getattr(self, "cached_result", None) if cached_result is not None: result, cache_key, err = cached_result > if my_cache_key == cache_key: E ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() ../miniconda/envs/tmp/lib/python3.8/site-packages/_pytest/fixtures.py:895: ValueError ============================================ 1 passed, 1 error in 0.35s ============================================ 

I'm not sure if there's a good way to fix this. If not, perhaps the docs should mention that arrays should not be used as parameter values, or only with function scope.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions