Tests created with pytest_pycollect_makeitem or non-python tests can't be used in pytest_generate_tests #13814
Unanswered
sashko1988
asked this question in
Q&A
Replies: 2 comments 5 replies
-
|
As long as definition nodes are not exposed youd need a gruesome hack Items are the output after generate tests not the input |
Beta Was this translation helpful? Give feedback.
5 replies
-
|
In case this is useful to anyone else, we got this working by making custom collectors run through pytest’s normal def _synthetic_callobj(test_name: str):
# Needed for non-python/custom tests: placeholder callable so pytest can build FunctionDefinition
def _noop():
return None
_noop.__name__ = f"legacy_{abs(hash(test_name))}"
return _noop
class TooCustomToHandle(pytest.Class):
def collect(self) -> Iterable[pytest.Item]:
for func_name, custom_stuff in self.lets_collect_functions():
# real method if present, synthetic one otherwise (e.g. yaml/tst style tests)
callobj = getattr(self.obj, func_name, None) or _synthetic_callobj(func_name)
definition = FunctionDefinition.from_parent(self, name=func_name, callobj=callobj)
fixtureinfo = definition._fixtureinfo
metafunc = Metafunc(
definition=definition,
fixtureinfo=fixtureinfo,
config=self.config,
cls=self.obj,
module=__import__(self.obj.__module__),
_ispytest=True,
)
# this is the key: run pytest_generate_tests exactly like pytest does
self.ihook.pytest_generate_tests.call_extra([], {"metafunc": metafunc})
if not metafunc._calls:
yield TheCustomTest.from_parent(
self,
name=func_name,
callobj=callobj,
fixtureinfo=fixtureinfo,
custom_attribute=custom_stuff,
)
continue
metafunc._recompute_direct_params_indices()
fixtureinfo.prune_dependency_tree()
for callspec in metafunc._calls:
subname = f"{func_name}[{callspec.id}]" if callspec._idlist else func_name
yield TheCustomTest.from_parent(
self,
name=subname,
callobj=callobj,
callspec=callspec,
fixtureinfo=fixtureinfo,
keywords={callspec.id: True},
originalname=func_name,
custom_attribute=custom_stuff,
) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello folks.
I have python tests that don't follow the pytest convention. Their classes have
__init__, other helper methods have specific execution order before the runner starts executing actual tests.pytest_pycollect_makeitemmade them work with pytest.Also, I have non-Python tests. To make them work with pytest, I followed your example with yaml files
But now I want to use the pytest-repeat plugin. It generates tests using
pytest_generate_tests.The plugin works flawlessly with pytest tests. However,
pytest_generate_testsisn't called for my custom tests.Example of my custom collector plugin:
What should I add to make those items usable with
pytest_generate_tests?Beta Was this translation helpful? Give feedback.
All reactions