-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
[unittest] raise error if @skip is used with an argument that looks like a test method #78777
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
Comments
When using @Skip decorator, class SomeTest(TestCase):
@skip
def test_method(self):
# ... The test actually passes when running with unittest, because it's equivalent to class SomeTest(TestCase):
def test_method(self):
# ...
test_method = skip(test_method) This gives the illusion that @Skip decorator worked as expected. I propose we should check the passed in In practice, this behavior has misled people using the decorator in the unintended way. For example: In this chapter of Test-Driven Development with Python (http://www.obeythetestinggoat.com/book/chapter_organising_test_files.html), the author used @Skip decorator without |
Is there a use-case for reason to be anything but a string? |
Well, I personally can not think of any. I think If that is ok, I'll be happy to submit a PR that restricts reason to be a string. |
It could be interesting to enable uncalled Do note that decorating with an uncalled $ cat test.py
from unittest import TestCase, skip class Test(TestCase):
def test_good(self):
self.assertTrue(1.0)
def test_bad(self):
self.assertFalse(1.0)
@skip
def test_bad_skip(self):
self.assertFalse(1.0)
$ ./python.exe -m unittest test.py -v
test_bad (test.Test) ... FAIL
test_bad_skip (test.Test) ... ok
test_good (test.Test) ... ok
test_good_skip (test.Test) ... skipped 'always skipped' ====================================================================== Traceback (most recent call last):
File "/.../test.py", line 10, in test_bad
self.assertFalse(1.0)
AssertionError: 1.0 is not false Ran 4 tests in 0.002s FAILED (failures=1, skipped=1) |
What would be a good default reason? How about the function name? if isinstance(reason, types.FunctionType):
reason = reason.__name__ For example, from unittest import TestCase, skip
class Test(TestCase):
@skip
def test_bad_skip(self):
self.assertFalse(1.0)
$ python -m unittest -v test.py ---------------------------------------------------------------------- OK (skipped=2) But this wouldn't be very helpful if test method is a lambda (just saying...) |
"Unconditionally" :) |
Hi @zach.ware, Just to make sure I'm getting this right (first time contributing to cpython...) Now I need to open 4 PRs at GitHub,
Looking good? |
I think we should make the same change on all branches (why would we fix it in maintenance branches just to break it in the next major release?), in which case it's just one PR to master and our backport bot (and/or the merging core dev) will take care of it from there. |
Ok, I see. Thank you. |
It doesn't look like a good design to me if allow a function be a decorator and a decorator fabric at the same time. It always lead to cumbersome and errorprone implementation. Currently there is only one example of such design in the stdlib, other propositions were rejected. Checking that the argument is a string and raising exception otherwise looks good to me for 3.8. There is no a bug that need to be fixed in maintained versions. If you use unittest.skip() improperly, it is your failure. Helping to catch such mistakes is a new feature. |
I don't agree that this change makes the implementation significantly more cumbersome. I also think there's a backward compatibility argument to be made for allowing the uncalled usage, particularly considering the OP's published example and other similar ones, such as https://stackoverflow.com/questions/2066508/disable-individual-python-unit-tests-temporarily which was for me the first Google result for "python unittest skip" that was not our own docs. |
For what its worth, I'm +1 with Serhiy that we raise an exception I don't think that @Skip with no reason was ever intended to work (it |
It would be nice to make *reason* optional. Every time I needed to use @unittest.skip() (even if I wanted to use it temporarily), I ended up passing some random string as reason or use 'raise SkipTest' directly. If we decide to keep *reason* required, I agree with Serhiy's proposal. |
Hi guys, what's our consensus on this?
If we choose to explicitly make |
I think we need Michael's ruling on which way to go here. I'm fine with either an exception or a default reason, but still lean towards the default. I don't think |
I'm in favour of a default and "Unconditionally skipped" is fine with me. Although "Skipped" would also be fine. Making @Skip work with no arguments is fine. Having to pass in arguments message arguments you don't want is a pain and there's no need to force the user. Let's add a default message and make the skipped tests report as skipped properly on 3.8 and close it. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: