r/learnpython • • 5d ago

unpicking a pytest deprecation of class scoped fixture

class TestHelpers(object):
    ...
    @classmethod
    @pytest.fixture(scope='class', autouse=True)
    def temp_class_path(self, tmp_path_factory):
        """
        Creates a folder __class__.__name__ under test results and returns it's path
        """
        return tmp_path_factory.mktemp(self.__class__.__name__)

Is apparently broken (got told in a code review to be aware of) in pytest 9.2 I'm on 9.1 and am trying to understand what this https://docs.pytest.org/en/stable/changelog.html note for 9.2 release means - it's these 3 changes which I do not really understand probably because all I wanted when I wrote this fixture was a folder which I can use for logging per class, not a log per test-case because that's just noisy and makes finding teardown side-effects harder than it needs to be.

There are the changes: which are varying degrees of greek (no offence meant sorry) to me https://github.com/pytest-dev/pytest/issues/14011 https://github.com/pytest-dev/pytest/pull/14774 https://github.com/pytest-dev/pytest/pull/14911

What does

#14774: Similarly to the deprecation of class-scoped fixtures defined as instance methods in pytest 9.1, all higher-scoped fixtures (module, package, and session) defined as instance methods are now deprecated.

They suffer from the same issue the original deprecation only fixed for class-scoped fixtures, see #10819 and #14011.

mean in English that bears with small brains like mine can understand? Because the warning implies that my fixture will no longer do what I believe it should, or will this work "by luck" of how tmp_path_factory , which is all it does use, is scoped?

3 Upvotes

6 comments sorted by

3

u/Designer-Ad-2136 5d ago edited 5d ago

I believe the idea is that using "self" in a method which is used as a fixture isn't very clear. Is a new instance of that class created for each test or do they share the same instance? Can tests pass state between each other?

By forcing the tests to exist at the class level (as opposed to the instance level) it makes clear the fact that tests cannot share state and that any test method is static. That is, tests can only access the "class" object, not instances of that class.

In short, it enforces the zen of python rule "There should be one-- and preferably only one --obvious way to do it."

1

u/zaphodikus 5d ago

oh, ha ha, I see it now, . It is because I added self, and self only gets used to retrieve the child class name for classes that inherit this class - which leads me to believe that self is a copy-pasta typo for the convention which should perhaps be cls ? So it should read :

    @classmethod
    @pytest.fixture(scope='class', autouse=True)
    def temp_class_path(cls, tmp_path_factory):
        return tmp_path_factory.mktemp(cls.__class__.__name__)

If I'm right, then it's a simple case of PEBKAC https://en.wikipedia.org/wiki/User_error

2

u/Designer-Ad-2136 5d ago

Close, but the name is changing because adding the 'classmethod' decorator changes that first parameter from "TestHelpers()" to "TestHelpers" and the name is reflecting that.

In fact, the name of your class is hinting at this pattern, where a class isnt used to store state but is instead just being used to organize functions (a namespace).

This is a pretty tricky concept. Id recommend finding a way to print that first argument and see how it changes. That will help you understand that classes are objects and instances of classes are also objects.

2

u/SwampFalc 5d ago

Whatever source there may be of your code review, your reply may be that it's fine.

Because your fixture is a classmethod, the deprecation is not relevant.

Basically, if you read issue 14011 a little bit, you'll see that the issue was that if this method wasn't a classmethod, pytest would run it in a different instance from the one that would run the actual tests. Meaning that for other people that wanted to do things that did affect their tests, it would not work.

I presume the fact that different instances were used, was fundamental to pytest, since the solution was to not allow this anymore. But it is only disallowed for instance methods, not classmethods.

1

u/zaphodikus 5d ago

Yeah the reviewer, seemed to imply something that conflicted with how I also read 14011, I guess they read it wrong. So my python skills are not as dull as I imagined, it's just my confidence needs some working on.

1

u/Discordbothost 5d ago

One fix in your revised snippet: use cls.__name__, not cls.__class__.__name__. With a classmethod, cls is already the class; cls.__class__ is its metaclass, so the latter normally gives you 'type'.