This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: unittest.TestLoader().loadTestsFromTestCase(...) fails when adding test cases with the expectedFailure decorator
Type: behavior Stage: resolved
Components: Tests Versions: Python 2.7
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: Erasmus Cedernaes, ezio.melotti, iritkatriel, kwarunek, michael.foord, rbcollins
Priority: normal Keywords:

Created on 2017-09-27 08:16 by Erasmus Cedernaes, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit Erasmus Cedernaes, 2017-09-27 08:16 Minimum working example
Messages (3)
msg303112 - (view) Author: Erasmus Cedernaes (Erasmus Cedernaes) Date: 2017-09-27 08:16
How to reproduce:
* Run the attached file with Python 2.7 without any modifications. This will produce an error, as described below.
* Uncomment line 2 (the decorator @unittest.expectedFailure) and run the file with Python 2.7. This will not produce an error.

Adding the decorator @unittest.expectedFailure to a unittest.TestCase causes the TestLoader().loadTestsFromTestCase(...) to fail with the following output:

$ python2.7
Traceback (most recent call last):
  File "/usr/lib/python2.7/", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/", line 72, in _run_code
    exec code in run_globals
  File "[...]/", line 12, in <module>
    tests = unittest.TestLoader().loadTestsFromTestCase(TestClassThatFails)
  File "/usr/lib/python2.7/unittest/", line 50, in loadTestsFromTestCase
    if issubclass(testCaseClass, suite.TestSuite):
TypeError: issubclass() arg 1 must be a class

The error does only occur when adding the test manually to a test suite. Running unittest.main(), does not produce the error.
msg303282 - (view) Author: Krzysztof Warunek (kwarunek) * Date: 2017-09-28 21:53
AFAIK this helper is meant to be used with test function not the TestCases.
msg393417 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-05-10 16:21
I can't reproduce the issue on 3.10, I believe it was fixed by this:

in particular:

- def expectedFailure(func):
-     @functools.wraps(func)
-     def wrapper(*args, **kwargs):
-         try:
-             func(*args, **kwargs)
-         except Exception:
-             raise _ExpectedFailure(sys.exc_info())
-         raise _UnexpectedSuccess
-     return wrapper
+ def expectedFailure(test_item):
+     test_item.__unittest_expecting_failure__ = True
+     return test_item

2.7 won't be fixed, so closing.
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75785
2021-05-10 16:21:47iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg393417

resolution: out of date
stage: resolved
2018-07-11 06:51:31serhiy.storchakasettype: crash -> behavior
2017-11-04 09:25:27berker.peksagsetnosy: + rbcollins, ezio.melotti, michael.foord
2017-09-28 21:53:12kwaruneksetnosy: + kwarunek
messages: + msg303282
2017-09-27 08:16:44Erasmus Cedernaescreate