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
Unit tests that return generators silently fail #59756
Comments
The following test will pass silently: class SomeTest(unittest.TestCase):
def testSomething(self):
yield 1
self.fail() |
unittest calls test methods to execute tests. Generator functions do nothing (except create a generator) when you call them. Test methods as generators therefore do nothing in unittest. |
I can play this game too! Since generator functions do nothing when used as a unittest test method, there is no value in having a test method that is a generator function. Therefore, it is an error to give a test method that is a generator function. Since it is an error to have a generator function as a test method, the test I gave should fail. |
It's not a game. Have a test method as a generator function is clearly programmer error. Doing nothing is the expected behaviour. |
Who expects this behavior? Can you give me an example of when someone would rely upon a generator test to do nothing? |
It's the expected behaviour according to the unittest specification. |
I would like to verify this. Link the specification, and highlight where it says that the expected behavior for a test method that is a generator function is to do nothing. |
This is not something that is specific to unittest. In Python, if you call a generator function *it returns a generator-iterator*. Unless you *do* something with the the iterator, nothing else happens. This is true in *any* python code. Unittest calls whatever test method you define, and handles (reports) the exceptions that result from that call. That's the fundamental design of unittest. Your generator test method does not raise any exceptions when called, therefore the test passed. |
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: