-
Notifications
You must be signed in to change notification settings - Fork 35
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
Add option to force the dependency to be collected #37
Comments
I'm skeptical. Until now, pytest-dependency is neat and small because it has a very clear scope and that is about skipping tests. In principle, I'd prefer to keep the scope focused and keep out of neighbouring tasks such as collecting or ordering. That does not mean that I'm totally against such an idea. But it does mean that it might take a significant effort to convince me that going that way is possible without opening a can of worms. |
Yeah, I agree with you about this, let's keep this awesome project neat and small. Just as a proof of concept I added in my def pytest_collection_modifyitems(config, items):
dependencies = list()
items_dict = dict()
for item in items:
markers = item.own_markers
items_dict[item.name] = item
for marker in markers:
depends = marker.kwargs.get("depends")
parent = item.parent
collect = marker.kwargs.get("collect")
if marker.name == "dependency" and depends and collect:
for depend in depends:
dependencies.append((depend, parent))
for dependency, parent in dependencies:
if dependency not in list(items_dict.keys()):
item = pytest.Function(name=dependency, parent=parent)
items.insert(0, item)
items_dict[dependency] = item this isn't a optimal solution but it work for me . |
Taking the first comment example, imagine the case when you need to set import pytest
@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_a():
assert False
@pytest.mark.dependency()
def test_b():
pass
@pytest.mark.dependency(depends=["test_a"])
def test_c():
pass
@pytest.mark.dependency()
def test_d():
test_b()
pass
@pytest.mark.dependency(depends=["test_b", "test_c"])
def test_e():
pass this solution force to execute the |
@RKrahl Just to add my voice, I'd also appreciate such a feature, maybe enabled with a flag that I can put in my .ini though, adding a keyword to all functions creates a lot of noise. My use case is that Pycharm gives all my tests little play-buttons that I can press to run them, which just calls the @pecalleja I tried your snippet, it didn't work for me, even after changing |
I was very surprised than selecting a test doesn't also select its dependencies and that there is no option or at least documentation on how to change that. I had to modify pecalleja's snippet as follows: @pytest.hookimpl(trylast=True)
def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None:
seen = set[str]()
new_items = list[pytest.Item]()
def dfs(item: pytest.Item) -> None:
if item.name in seen:
return
seen.add(item.name)
for marker in item.own_markers:
if marker.name == "dependency" and marker.kwargs.get("collect"):
for dependency in marker.kwargs.get("depends", []):
dfs(pytest.Function.from_parent(name=dependency, parent=item.parent))
new_items.append(item)
for item in items:
dfs(item)
old_names = set(item.name for item in items)
new_names = [item.name for item in new_items if item.name not in old_names]
if new_names:
print("Un-deselected:", *new_names)
items[:] = new_items This assumes just simple function dependencies in the same parent module; I don't know how to do it in general, but #56 has more. |
It would be nice an option in the dependency that force to collect and execute the required test. For example:
if execute only the
test_d
the collector also will select thetest_b
because the hard restriction of the dependencycollect=True
If you think this is a good idea I can work on this feature. kind regards
The text was updated successfully, but these errors were encountered: