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

Ignore class name pattern #1879

Closed
JamshedVesuna opened this issue Aug 26, 2016 · 8 comments
Closed

Ignore class name pattern #1879

JamshedVesuna opened this issue Aug 26, 2016 · 8 comments
Labels
type: enhancement new feature or API change, should be merged into features branch

Comments

@JamshedVesuna
Copy link

It would be great to have an ignore_python_classes configuration pattern in conjunction with python_classes. This is especially useful when using private base test classes:

class _BaseTest():

    def test_something(self):
        assert self.var == 1  # self.var only exists in child classes.
        ...

class TestSomething(_BaseTest):
    var = 3
    # Run test_something with self.var = 3.
    ...

I couldn't find an existing issue or question about this. Is there a better way of doing this than setting an ignore pattern of _Base*?

@nicoddemus
Copy link
Member

pytest by default will only search for classes with the Test prefix. Running your own example:

============================= test session starts =============================
collected 1 items

foo.py::TestSomething::test_something FAILED

================================== FAILURES ===================================
________________________ TestSomething.test_something _________________________

self = <foo.TestSomething instance at 0x00000000038B9888>

    def test_something(self):
>       assert self.var == 1  # self.var only exists in child classes.
E       assert 3 == 1
E        +  where 3 = <foo.TestSomething instance at 0x00000000038B9888>.var

foo.py:4: AssertionError
========================== 1 failed in 0.03 seconds ===========================

You will notice it only collected TestSomething. Can you post a complete example of the problem you are having?

@JamshedVesuna
Copy link
Author

More specifically, I am switching from using nosetests to pytest, and have some _Base test classes that inherit from unittest.TestCase. These are getting picked up by pytest and pytest --collect-only. Would make the transition much faster if I could just explicitly ignore these test classes. Or, is there an alternative solution here?

@The-Compiler
Copy link
Member

I think setting __test__ = False in those classes should prevent them from being picked up - can you try that?

In the long run, those should probably be converted parametrized plain tests though 😉

@nicoddemus
Copy link
Member

I think setting test = False in those classes should prevent them from being picked up - can you try that?

Also you will need to set __test__ = True on the subclasses.

@JamshedVesuna
Copy link
Author

Darn, this might be a lot of overhead. Is there a plan to add ignore_python_classes to the config setup? Is this a reasonable PR request?

@nicoddemus
Copy link
Member

I guess so... it would have to be implemented by the unittest plugin. But that's a new feature, so it will be available in 3.1.0 only, and that might take a awhile to be released.

@nicoddemus nicoddemus added the type: enhancement new feature or API change, should be merged into features branch label Aug 29, 2016
@JamshedVesuna
Copy link
Author

Thanks for the information and quick response! Going to close this out for now.

@nicoddemus
Copy link
Member

No problem, glad to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

3 participants