Skip to content
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

Inheriting from test class with dependencies doesn't work correctly #72

Open
a-recknagel opened this issue Feb 13, 2023 · 1 comment
Open

Comments

@a-recknagel
Copy link

For the following example:

test_classes.py

class TestFoo:
    @pytest.mark.dependency(name="a")
    def test_a(self):
        assert True

    @pytest.mark.dependency(name="b", depends=["a"])
    def test_b(self):
        assert True

    @pytest.mark.dependency(name="c", depends=["b"])
    def test_c(self):
        assert True


class TestBar(TestFoo):
    @pytest.mark.dependency(name="b", depends=["a"])
    def test_b(self):
        assert True

Running pytest test_classes.py -k TestBar gives me

test_classes.py::TestBar::test_c SKIPPED (test_c depends on b)                                                                                                          [ 66%]

I guess dependency names are global, and TestBar.test_b doesn't get to register itself because the name b is already taken. As a consequence, if I run both classes in the same run, TestBar::test_c is executed -- even if TestBar::test_b fails. Is there another naming pattern I should use?

If there is no solution right now, something like this would be great:

@pytest.mark.dependency_localize()
class TestFoo:
    @pytest.mark.dependency(name="a")
    def test_a(self):
        assert True

    @pytest.mark.dependency(name="b", depends=["a"])
    def test_b(self):
        assert True


@pytest.mark.dependency_localize()
class TestBar(TestFoo):
    pass

, where the dependency names for TestFoo.test_a and TestFoo.test_b would be TestFoo.a and TestFoo.b respectively, and for TestBar it would be TestBar.a and TestBar.b. It wouldn't be able to fix depends calls at runtime though. But since those can access their class dynamically, there might be a way to write them in an inheritance-cooperative manner.

Setting the scope of the dependency-call to class doesn't solve the problem, since non-overloaded functions like test_a and test_c in the original example will be called in a different class scope.

@a-recknagel
Copy link
Author

I'm working around it right now with a rather involved solution, which also implements dependency-based ordering, because overloading methods in a child will move them to the front of the order in which pytest (or any other inspection) finds them in the class.

@RKrahl, I think you rejected proposals that would make this project more complex in the past. I'll make the PR anyway since similar features were requested by others as well, and if it's fine to be pulled I can add docs as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant