-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
unittest.UnitTestCase: Allow __test__ for methods #1559
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
Conversation
testing/test_unittest.py
Outdated
| testdir.makepyfile(test_foo=""" | ||
| import unittest | ||
| class MyTestMeta(type): |
There was a problem hiding this comment.
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__ = FalseWhat do you think?
There was a problem hiding this comment.
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__'
There was a problem hiding this comment.
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__ = FalseThere was a problem hiding this comment.
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
|
No need to worry about the py26 failure, it is unrelated to this issue. |
|
Thanks a lot! 😁 If nobody else has any more comments, I will merge this in tomorrow. |
|
i think it might need to target features |
|
Hmm you got a point... opinions? |
|
we should probably aim for a patch release this week and a feature release next week? |
|
Sounds good! We have some nice features for 2.10 in place already. |
|
should I put it on features or master ? |
|
Please put it on features, thanks! Sorry for the side discussion. |
|
Oh, you will have to create a new PR unfortunately. |
|
@nicoddemus Yep, closing this then |
|
Moved to #1561 as based on above discussion the commit was moved to the |
|
Thanks! 👍 |
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