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

Deselecting class prefixed with "Test" #2007

Closed
syre opened this Issue Oct 18, 2016 · 7 comments

Comments

Projects
None yet
5 participants
@syre

syre commented Oct 18, 2016

Is it possible to deselect a class prefixed with "Test" from collection without renaming it?

The class is imported from several test modules and thus generates several warnings:
====================================== pytest-warning summary ======================================= WC1 /vagrant/publish/marketplace/tests/test_models.py cannot collect test class 'TestPrintProduct' because it has a __init__ constructor WC1 /vagrant/publish/marketplace/tests/test_signals.py cannot collect test class 'TestPrintProduct' because it has a __init__ constructor WC1 /vagrant/publish/salechannels/tests/test_signals.py cannot collect test class 'TestPrintProduct' because it has a __init__ constructor

i've tried the following to no avail:

  • the pytest.mark.skip decorator on the class
  • setting test = False on the class as attribute
  • k flag with "not TestPrintProduct"
@syre

This comment has been minimized.

syre commented Oct 18, 2016

From correspondences on IRC, it seems the only way to prevent the warnings from triggering is a conftest.py file containing the following:

import pytest


@pytest.hookimpl(tryfirst=True)
def pytest_pycollect_makeitem(collector, name, obj):
    if name == "TestPrintProduct":
        return IgnoreCollector(name, parent=collector)


class IgnoreCollector(pytest.Collector):
    def collect(self):
        return []

    def reportinfo(self):
        return ""
@The-Compiler

This comment has been minimized.

Member

The-Compiler commented Oct 18, 2016

FWIW I'd expect setting __test__ = False to work, but it doesn't

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Oct 18, 2016

@The-Compiler the code involved warns before that attribute is ever tested

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Oct 18, 2016

the code involved warns before that attribute is ever tested

But should be fixable, right? Setting __test__ = False seems like an acceptable workaround.

@lwm

This comment has been minimized.

Contributor

lwm commented Dec 2, 2016

Oh, we forgot to close this one too ;)

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Dec 2, 2016

@lwm in the future, you can add a string like Fix #2007 to the commit message so the issue will be closed automatically when the PR gets merged. More info here. 😉

@nicoddemus nicoddemus closed this Dec 2, 2016

@lwm

This comment has been minimized.

Contributor

lwm commented Dec 2, 2016

Sure, will do. Thanks!

This was referenced Mar 6, 2018

blueyed added a commit to blueyed/paste that referenced this issue Oct 29, 2018

pytest: fix collection warnings via __test__=False
Fixes

> "cannot collect test class %r because it has a __init__ constructor

Ref: pytest-dev/pytest#2007

blueyed added a commit to blueyed/paste that referenced this issue Oct 30, 2018

pytest: fix collection warnings via __test__=False
Fixes

> "cannot collect test class %r because it has a __init__ constructor

Ref: pytest-dev/pytest#2007

cdent added a commit to cdent/paste that referenced this issue Oct 30, 2018

Pytest fixes (#9)
* pytest: fix collection warnings via __test__=False

Fixes

> "cannot collect test class %r because it has a __init__ constructor

Ref: pytest-dev/pytest#2007

* pytest: configure testpaths

This is faster with test collection.

* pytest: fix warning with doctests

Fixes

> /usr/lib/python3.7/site-packages/_pytest/python.py:764:
> RemovedInPytest4Warning: usage of Generator.Function is deprecated,
> please use pytest.Function instead

* Minor fixes around s/py.test/pytest/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment