Skip to content

Commit

Permalink
Treat methodName="runTest" similar to unittest.TestCase
Browse files Browse the repository at this point in the history
pytest 8.2 relies on a feature of `unittest.TestCase` where the
initialization of it with the default `methodName="runTest"` is treated
specially, allowing it to insantiate even without `runTest` method
actually existing.

See under "Changed in Python 3.2" in unittest.TestCase docs

This fixes the error with pytest 8.2:
  AttributeError: 'TestUtil' object has no attribute 'runTest'. Did you mean: 'subTest'?

Fixes: testing-cabal#372
ref: https://docs.python.org/3/library/unittest.html#unittest.TestCase
ref: https://github.com/python/cpython/blob/51aefc5bf907ddffaaf083ded0de773adcdf08c8/Lib/unittest/case.py#L419-L426
ref: pytest-dev/pytest#12263 (comment)
  • Loading branch information
ncopa committed May 13, 2024
1 parent a481b55 commit e4f0b99
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion testtools/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,17 @@ def _run_teardown(self, result):

def _get_test_method(self):
method_name = getattr(self, "_testMethodName")
return getattr(self, method_name)
try:
m = getattr(self, method_name)
except AttributeError:
if method_name != "runTest":
# We allow instantiation with no explicit method name
# but not an *incorrect* or missing method name.
raise ValueError(
"no such test method in %s: %s" % (self.__class__, method_name)
)
else:
return m

def _run_test_method(self, result):
"""Run the test method for this test.
Expand Down

0 comments on commit e4f0b99

Please sign in to comment.