| 1 |
"""Run a single test method in this (sub)process and report the result. |
| 2 |
|
| 3 |
Invoked as ``python -m test.support.subprocess_runner MODULE QUALNAME OUTFILE |
| 4 |
CONFIG`` by :func:`test.support.isolation.runInSubprocess`. CONFIG is the |
| 5 |
marshalled test.support configuration of the parent test run, as a hex string. |
| 6 |
The outcome of the test (including that of each individual subtest) is |
| 7 |
marshalled to OUTFILE. This module is not meant to be imported. |
| 8 |
|
| 9 |
Import as little as possible before running the test: every module imported |
| 10 |
here is state that the test would not see in a normal test run. |
| 11 |
""" |
| 12 |
|
| 13 |
import marshal |
| 14 |
import sys |
| 15 |
import unittest |
| 16 |
from unittest.case import _SubTest |
| 17 |
|
| 18 |
if __name__ != '__main__': |
| 19 |
raise ImportError('this module cannot be directly imported') |
| 20 |
|
| 21 |
if len(sys.argv) != 5: |
| 22 |
print('usage: python -m test.support.subprocess_runner ' |
| 23 |
'MODULE QUALNAME OUTFILE CONFIG', file=sys.stderr) |
| 24 |
sys.exit(2) |
| 25 |
|
| 26 |
module, qualname, outfile, config = sys.argv[1:] |
| 27 |
|
| 28 |
# Set up the child before importing the test. |
| 29 |
from test.support.isolation import _apply_child_config |
| 30 |
_apply_child_config(config) |
| 31 |
|
| 32 |
|
| 33 |
class _Result(unittest.TestResult): |
| 34 |
# Capture per-test durations keyed by test id, so the parent can report the |
| 35 |
# subprocess timings instead of its own replay time. |
| 36 |
def __init__(self): |
| 37 |
super().__init__() |
| 38 |
self.id_durations = [] |
| 39 |
|
| 40 |
def addDuration(self, test, elapsed): |
| 41 |
super().addDuration(test, elapsed) |
| 42 |
self.id_durations.append((test.id(), elapsed)) |
| 43 |
|
| 44 |
|
| 45 |
# Resolve the qualname in the imported module, rather than letting |
| 46 |
# loadTestsFromName() guess where the module name ends: it guesses by trying |
| 47 |
# imports that fail, and a failing import pulls in importlib.resources. |
| 48 |
__import__(module) |
| 49 |
suite = unittest.TestLoader().loadTestsFromName(qualname, sys.modules[module]) |
| 50 |
result = _Result() |
| 51 |
suite.run(result) |
| 52 |
|
| 53 |
|
| 54 |
def _outcome(kind, test, detail): |
| 55 |
subtest = isinstance(test, _SubTest) |
| 56 |
real = test.test_case if subtest else test |
| 57 |
return { |
| 58 |
'kind': kind, |
| 59 |
'subtest': subtest, |
| 60 |
'desc': test._subDescription() if subtest else '', |
| 61 |
# id() groups outcomes by test method; a non-TestCase (e.g. an |
| 62 |
# _ErrorHolder) marks a setUpClass()/setUpModule() fixture failure. |
| 63 |
'id': real.id(), |
| 64 |
'fixture': not isinstance(real, unittest.TestCase), |
| 65 |
'detail': detail, |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
outcomes = [_outcome('failure', t, tb) for t, tb in result.failures] |
| 70 |
outcomes += [_outcome('error', t, tb) for t, tb in result.errors] |
| 71 |
outcomes += [_outcome('expected_failure', t, tb) |
| 72 |
for t, tb in result.expectedFailures] |
| 73 |
outcomes += [_outcome('skipped', t, reason) for t, reason in result.skipped] |
| 74 |
|
| 75 |
payload = {'outcomes': outcomes, 'durations': result.id_durations} |
| 76 |
with open(outfile, 'wb') as f: |
| 77 |
marshal.dump(payload, f) |
| 78 |
|
| 79 |
sys.exit(0) |