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

Deselecting class prefixed with "Test" #2007

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

Deselecting class prefixed with "Test" #2007

syre opened this issue Oct 18, 2016 · 7 comments
Labels
good first issue easy issue that is friendly to new contributor type: bug problem that needs to be addressed

Comments

@syre
Copy link

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"
@RonnyPfannschmidt RonnyPfannschmidt added the type: bug problem that needs to be addressed label Oct 18, 2016
@syre
Copy link
Author

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
Copy link
Member

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

@RonnyPfannschmidt
Copy link
Member

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

@nicoddemus
Copy link
Member

the code involved warns before that attribute is ever tested

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

@RonnyPfannschmidt RonnyPfannschmidt added the good first issue easy issue that is friendly to new contributor label Oct 18, 2016
@decentral1se
Copy link
Contributor

Oh, we forgot to close this one too ;)

@nicoddemus
Copy link
Member

@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. 😉

@decentral1se
Copy link
Contributor

Sure, will do. Thanks!

This was referenced Mar 6, 2018
blueyed added a commit to blueyed/paste that referenced this issue Oct 29, 2018
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
Fixes

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

Ref: pytest-dev/pytest#2007
cdent pushed a commit to pasteorg/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

* 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
Labels
good first issue easy issue that is friendly to new contributor type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

5 participants