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

Documentation: setup and teardown aren't mentioned #9549

Closed
alfredodeza opened this issue Jan 26, 2022 · 5 comments · Fixed by #10349
Closed

Documentation: setup and teardown aren't mentioned #9549

alfredodeza opened this issue Jan 26, 2022 · 5 comments · Fixed by #10349
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@alfredodeza
Copy link
Contributor

While browsing the current release docs on "xunit-style" support, specifically the usage of setup and tearing down (cleanup) of tests, I was surprised not to see setup() and teardown() mentioned. Instead, the documentation points out that for methods, one must use setup_method() and teardown_method().

I've always used setup() and teardown() methods for setting up and cleaning up before and after each test method in a class for production code as well as teaching Pytest.

Are those methods deprecated and shouldn't be used? Supported by mistake? Or is it an omission in the docs? Either way the docs need to be rectified for correctness. I'm happy to provide a PR with the right path.

@nicoddemus
Copy link
Member

nicoddemus commented Jan 26, 2022

Hi @alfredodeza,

Fair question.

setup and teardown work because of the nose-style support.

For example, the example below works when executing pytest, but fails with pytest -p no:nose:

class Test:

    def setup(self):
        self.x = 1

    def test_foo(self):
        assert self.x == 1

So this is a nose-idiom, not a pytest-idiom.

Perhaps we need to document setup() and teardown() more explicitly in the nose page? 🤔

@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Jan 26, 2022
@alfredodeza
Copy link
Contributor Author

Good point @nicoddemus . I forgot to mention I also went through the nose reference, both for Pytest as well on the Nose project. The Nose project doesn't mention setup() or teardown() methods at all in their documentation. But perhaps I'm missing some other section where those are explained?

Thanks for pointing out that these aren't pytest-native methods and are meant to support the other test framework (TIL!)

In any case, I'm happy to provide a PR that explains this better, but would like to have something to correlate to Nose (couldn't find a reference)

@nicoddemus
Copy link
Member

nicoddemus commented Jan 26, 2022

but would like to have something to correlate to Nose (couldn't find a reference)

Not sure either, I'm not familiar with nose's docs myself, but it certainly is supported:

# test_foo.py
class Test:

    def setup(self):
        self.x = 1

    def test_foo(self):
        assert self.x == 1
λ pip list
Package    Version
---------- -------
nose       1.3.7
pip        21.3.1
setuptools 59.6.0
wheel      0.37.1

λ nosetests test_foo.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

@davidszotten
Copy link
Contributor

i just came across this in a potentially even more surprising scenario. my test module had a (top level) import of a function that happened to be called setup

(extra context: this is using django and pytest-django, that setup function happens to use the db. the test that usually calls it is marked with django_db but between pytest 6.2.5 (pytest-django 4.5.1) and 7.3.1, something's changed in the import order or otherwise causing the setup function to run before the marker is noted.

because the setup function is called by pytest, it is also the top of the stacktrace which adds to the confusion)

@davidszotten
Copy link
Contributor

davidszotten commented Oct 3, 2022

update: git bisect points to 89f0b5b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants