Skip to content

Commit 959115d

Browse files
committed
unittest: Add support for specifying custom TestRunner.
1 parent 2d61dbd commit 959115d

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

python-stdlib/unittest/unittest.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def run(self, result):
263263

264264

265265
class TestRunner:
266-
def run(self, suite):
266+
def run(self, suite: TestSuite):
267267
res = TestResult()
268268
suite.run(res)
269269

@@ -292,7 +292,8 @@ def __init__(self):
292292
self.testsRun = 0
293293
self.errors = []
294294
self.failures = []
295-
self.newFailures = 0
295+
self.skipped = []
296+
self._newFailures = 0
296297

297298
def wasSuccessful(self):
298299
return self.errorsNum == 0 and self.failuresNum == 0
@@ -326,6 +327,7 @@ def __add__(self, other):
326327
self.testsRun += other.testsRun
327328
self.errors.extend(other.errors)
328329
self.failures.extend(other.failures)
330+
self.skipped.extend(other.skipped)
329331
return self
330332

331333

@@ -354,10 +356,10 @@ def handle_test_exception(
354356
test_result.errors.append((current_test, ex_str))
355357
if verbose:
356358
print(" ERROR")
357-
test_result.newFailures += 1
359+
test_result._newFailures += 1
358360

359361

360-
def run_suite(c, test_result, suite_name=""):
362+
def run_suite(c, test_result: TestResult, suite_name=""):
361363
if isinstance(c, TestSuite):
362364
c.run(test_result)
363365
return
@@ -384,19 +386,21 @@ def run_one(test_function):
384386
test_container = f"({suite_name})"
385387
__current_test__ = (name, test_container)
386388
try:
387-
test_result.newFailures = 0
389+
test_result._newFailures = 0
388390
test_result.testsRun += 1
389391
test_globals = dict(**globals())
390392
test_globals["test_function"] = test_function
391393
exec("test_function()", test_globals, test_globals)
392394
# No exception occurred, test passed
393-
if test_result.newFailures:
395+
if test_result._newFailures:
394396
print(" FAIL")
395397
else:
396398
print(" ok")
397399
except SkipTest as e:
398-
print(" skipped:", e.args[0])
400+
reason = e.args[0]
401+
print(" skipped:", reason)
399402
test_result.skippedNum += 1
403+
test_result.skipped.append((name, c, reason))
400404
except Exception as ex:
401405
handle_test_exception(
402406
current_test=(name, c), test_result=test_result, exc_info=sys.exc_info()
@@ -477,8 +481,14 @@ def discover(runner: TestRunner):
477481
return discover(runner=runner)
478482

479483

480-
def main(module="__main__"):
481-
runner = TestRunner()
484+
def main(module="__main__", testRunner=None):
485+
if testRunner:
486+
if isinstance(testRunner, type):
487+
runner = testRunner()
488+
else:
489+
runner = testRunner
490+
else:
491+
runner = TestRunner()
482492

483493
if len(sys.argv) <= 1:
484494
result = discover(runner)

0 commit comments

Comments
 (0)