[issue41322] unittest: Generator test methods will always be marked as passed

Karthikeyan Singaravelan report at bugs.python.org
Fri Jul 17 06:17:58 EDT 2020


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

https://github.com/python/cpython/blob/8e836bb21ce73f0794fd769db5883c29680dfe47/Lib/unittest/case.py#L548 . _callTestMethod just calls the test method and doesn't check for the method to be a generator function to be iterated through. In Python 3.8 the call to test method was separated out as _callTestMethod. So something like below added in the original method should work. I guess there is an existing issue for this.


import unittest
from unittest import TestCase

class BuggyTestCase(TestCase):

    def _callTestMethod(self, method):
        import inspect

        if inspect.isgeneratorfunction(method):
            list(method())
        else:
            method()

    def test_generator(self):
        self.assertTrue(False)
        yield None

if __name__ == "__main__":
    unittest.main()


python3.8 test_foo.py
F
======================================================================
FAIL: test_generator (__main__.BuggyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_foo.py", line 10, in _callTestMethod
    list(method())
  File "test_foo.py", line 15, in test_generator
    self.assertTrue(False)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

----------
nosy: +ezio.melotti, michael.foord, rbcollins, xtreak

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41322>
_______________________________________


More information about the Python-bugs-list mailing list