Skip to content

Conversation

@AbdealiLoKo
Copy link
Contributor

test needs to be checked for methods of a class too. Earlier,
this was not done, and all methods in a class was assumed to be
a test. This commit adds the appropriate condition to ensure that
if the test is set to False, it does not collect that method.

Fixes #1558

testdir.makepyfile(test_foo="""
import unittest
class MyTestMeta(type):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test is a little more complex than it needs, although I understand it was your specific use case; IMHO we should keep things simpler in pytest's test suite though, unless we are of course testing for a specific issue with meta-classes and test cases (which is not the actual problem being tested).

I would rather see something like:

class MyTestCase(unittest.TestCase):

    def test_should_run(self):
        pass
    def test_should_not_run(self):
        pass

MyTestCase.test_should_not_run.__test__ = False

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that initially, but it isn't allowed:

>>> import unittest
>>> class MyTestCase(unittest.TestCase):
...     def test_should_run(self):
...         pass
...     def test_should_not_run(self):
...         pass
... 
>>> MyTestCase.test_should_not_run.__test__ = False
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'instancemethod' object has no attribute '__test__'

Even setattr fails:

>>> setattr(MyTestCase.test_should_not_run, '__test__', False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'instancemethod' object has no attribute '__test__'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, sorry. Please set the attribute inside the class (into the function object) instead.

import unittest

class MyTestCase(unittest.TestCase):

    def test_should_run(self):
        pass
    def test_should_not_run(self):
        pass
    test_should_not_run.__test__ = False

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍 I did not think of that

__test__ needs to be checked for methods of a class too. Earlier,
this was not done, and all methods in a class was assumed to be
a test. This commit adds the appropriate condition to ensure that
if the __test__ is set to False, it does not collect that method.

Fixes #1558
@nicoddemus
Copy link
Member

No need to worry about the py26 failure, it is unrelated to this issue.

@nicoddemus
Copy link
Member

Thanks a lot! 😁

If nobody else has any more comments, I will merge this in tomorrow.

@RonnyPfannschmidt
Copy link
Member

i think it might need to target features

@nicoddemus
Copy link
Member

Hmm you got a point... opinions?

@RonnyPfannschmidt
Copy link
Member

we should probably aim for a patch release this week and a feature release next week?

@nicoddemus
Copy link
Member

Sounds good! We have some nice features for 2.10 in place already.

@AbdealiLoKo
Copy link
Contributor Author

should I put it on features or master ?

@nicoddemus
Copy link
Member

Please put it on features, thanks! Sorry for the side discussion.

@nicoddemus
Copy link
Member

Oh, you will have to create a new PR unfortunately.

@AbdealiLoKo
Copy link
Contributor Author

@nicoddemus Yep, closing this then

@AbdealiLoKo
Copy link
Contributor Author

Moved to #1561 as based on above discussion the commit was moved to the features branch.

@nicoddemus
Copy link
Member

Thanks! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants