How to Fix a PytestCollectionWarning about WebTest’s TestApp Class2020-07-28
Here’s a small warning I’ve come across a couple of times, and how to fix it with a niche pytest feature.
If you have some tests using WebTest and run them with pytest, you might see this warning at the end of your test run:
This happens because pytest tries to collect the
TestApp class as a test class, but it finds it is incompatible.
pytest supports tests in plain classes, and the default glob pattern to match such classes is
But such test classes can’t have an
The simplest solution is to mark the class as not to be collected as tests.
This can be done by setting the
__test__ = False on the class, a somewhat hidden pytest feature that was copied from the nose test runner.
This is probably best done in your
conftest.py or similar, since it only needs doing once:
With that, your tests should collect without error - but it’s up to you to make them pass :)
I’ve submitted a pull request to WebTest, so hopefully that is merged and this isn’t necessary with future versions.
I hope this helps you with using pytest,
Working on a Django project? Check out my book Speed Up Your Django Tests which covers loads of best practices so you can write faster, more accurate tests.
One summary email a week, no spam, I pinky promise.
- My Most Used Pytest Commandline Flags
- Detect if Your Tests Are Running on a CI System
- Solving Algorithmic Problems in Python with Pytest
© 2020 All rights reserved.