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

Unit tests that return generators silently fail #59756

Closed
JasonBaker mannequin opened this issue Aug 3, 2012 · 8 comments
Closed

Unit tests that return generators silently fail #59756

JasonBaker mannequin opened this issue Aug 3, 2012 · 8 comments

Comments

@JasonBaker
Copy link
Mannequin

JasonBaker mannequin commented Aug 3, 2012

BPO 15551
Nosy @ezio-melotti, @alex, @bitdancer, @voidspace

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:

assignee = None
closed_at = <Date 2012-08-03.20:25:37.176>
created_at = <Date 2012-08-03.18:08:17.923>
labels = ['invalid']
title = 'Unit tests that return generators silently fail'
updated_at = <Date 2012-08-04.21:54:38.510>
user = 'https://bugs.python.org/JasonBaker'

bugs.python.org fields:

activity = <Date 2012-08-04.21:54:38.510>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2012-08-03.20:25:37.176>
closer = 'michael.foord'
components = ['None']
creation = <Date 2012-08-03.18:08:17.923>
creator = 'Jason.Baker'
dependencies = []
files = []
hgrepos = []
issue_num = 15551
keywords = []
message_count = 8.0
messages = ['167340', '167351', '167354', '167356', '167360', '167361', '167363', '167447']
nosy_count = 5.0
nosy_names = ['ezio.melotti', 'alex', 'r.david.murray', 'michael.foord', 'Jason.Baker']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue15551'
versions = ['Python 2.7']

@JasonBaker
Copy link
Mannequin Author

JasonBaker mannequin commented Aug 3, 2012

The following test will pass silently:

    class SomeTest(unittest.TestCase):

      def testSomething(self):
        yield 1
        self.fail()

@voidspace
Copy link
Contributor

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.

@JasonBaker
Copy link
Mannequin Author

JasonBaker mannequin commented Aug 3, 2012

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.

@voidspace
Copy link
Contributor

It's not a game. Have a test method as a generator function is clearly programmer error. Doing nothing is the expected behaviour.

@JasonBaker
Copy link
Mannequin Author

JasonBaker mannequin commented Aug 3, 2012

Who expects this behavior? Can you give me an example of when someone would rely upon a generator test to do nothing?

@voidspace
Copy link
Contributor

It's the expected behaviour according to the unittest specification.

@JasonBaker
Copy link
Mannequin Author

JasonBaker mannequin commented Aug 3, 2012

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.

@bitdancer
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
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

No branches or pull requests

2 participants