Configure Pytest discovery to ignore class name

pytest, python, testing

Solution

Here is a simple solution that I use, but has some overhead.

class DisablePyTestCollectionMixin(object):
  __test__ = False

class TestimonialFactory(DisablePyTestCollectionMixin):
  pass

Based on: https://github.com/pytest-dev/pytest/issues/1879

Problem

Pytest's default discovery rules will import all Class starting with `Test` that do not have an `__init__()`. I have a situation where this causes an incorrect class to be imported. I am testing a django project that uses Factory Boy. http://factoryboy.readthedocs.org/en/latest/ to build out a Django model named `Testimonial`. like so: ``` class TestimonialFactory(factory.Factory): class Meta: model = models.Testimonial ``` This issue is that `factory.Factory` does not have an `__init__()`. So py.test sees `Test`imonials and tries to run. Which in turn tries to insert a record into the database within the pytest discovery phase (hilarity and failures ensue). I have hacked a workaround by changing the pytest.ini to look for Test classes to start with Check instead of Test: ``` [pytest] python_classes=Check ``` This is not really what I want. Is there any way to explicitly tell py.test to ignore a test of a certain name?

Original source