0

Consider you have the following pytest pseudo-code:

@pytest.fixture
def path(self):
   return os.path.join(...)

@pytest.fixture
def module_names(self, path):
   return [......some list comprehension]

@pytest.mark.parametrize('module', module_names)
def test_foo(self, path, module):
    .....

And I get error like:

python3.7/site-packages/_pytest/mark/structures.py:99: in _parse_parametrize_parameters
    ParameterSet.extract_from(x, force_tuple=force_tuple) for x in argvalues
E   TypeError: 'function' object is not iterable

However when changing code to hardcoded like:

@pytest.mark.parametrize('module', ['foo1']) it works...

Any idea why?

JavaSa
  • 5,813
  • 16
  • 71
  • 121
  • Because `module_names` is the *function* when the decorator gets called, it's not the returned fixture until the wrapper is invoked. Think about how decorators work, and when they get called. Try `@pytest.mark.parametrize('module', module_names(path()))`. – jonrsharpe Mar 08 '20 at 16:15
  • @jonrsharpe: can you give concrete example to explain? – JavaSa Mar 08 '20 at 16:16
  • and how can we make it work? – JavaSa Mar 08 '20 at 16:17
  • 1
    You've provided a concrete example, and my comment includes how you could make it work. I'm not sure what else you want to know. Maybe read https://stackoverflow.com/a/5929165/3001761; `parametrize` is a decorator factory, which is invoked *much* earlier than the fixtures are injected. – jonrsharpe Mar 08 '20 at 16:17

0 Answers0