@@ -961,6 +961,59 @@ The :mod:`!test.support` module defines the following functions:
961961 :mod: `tracemalloc ` is enabled.
962962
963963
964+ .. currentmodule :: test.support.isolation
965+
966+ .. decorator :: runInSubprocess()
967+
968+ Decorator that runs the decorated test in a fresh interpreter subprocess, in
969+ isolation, so that it does not share global or interpreter state with the
970+ rest of the test run. It can decorate a test method or a whole
971+ :class: `~unittest.TestCase ` subclass. Decorated methods must take no extra
972+ arguments. A failure, error or skip in the subprocess is reported for the
973+ corresponding test, and individual :meth: `subtests
974+ <unittest.TestCase.subTest> ` that fail or are skipped are reported
975+ individually. A reported failure or error shows the original subprocess
976+ traceback as the cause of the exception.
977+
978+ When a **method ** is decorated, only that method runs in a subprocess; all
979+ fixtures (:meth: `~unittest.TestCase.setUp ` / :meth: `~unittest.TestCase.tearDown `,
980+ :meth: `~unittest.TestCase.setUpClass ` / :meth: `~unittest.TestCase.tearDownClass `
981+ and ``setUpModule() `` / ``tearDownModule() ``) run both in the parent process
982+ (as usual) and in the subprocess around the method.
983+
984+ When a **class ** is decorated, the whole class runs in a single subprocess,
985+ and :meth: `~unittest.TestCase.setUpClass `,
986+ :meth: `~unittest.TestCase.tearDownClass `, :meth: `~unittest.TestCase.setUp `
987+ and :meth: `~unittest.TestCase.tearDown ` run once each in the subprocess and
988+ are skipped in the parent process. A failure or skip of
989+ :meth: `~unittest.TestCase.setUpClass ` in the subprocess is reported for the
990+ whole class. ``setUpModule() `` cannot be controlled by a class decorator,
991+ so it still runs in the parent process too; test it with
992+ :data: `runningInSubprocess ` if needed.
993+
994+ The subprocess inherits the enabled resources (``-u ``), memory limit
995+ (``-M ``) and verbosity (``-v ``) of the parent test run, so that
996+ :func: `~test.support.requires_resource `, :func: `~test.support.requires `,
997+ :func: `~test.support.bigmemtest ` and the like behave consistently in both
998+ processes.
999+
1000+ The test is skipped on platforms without subprocess support.
1001+
1002+
1003+ .. data :: runningInSubprocess
1004+
1005+ ``True `` while the code runs in the isolated subprocess spawned by
1006+ :func: `runInSubprocess `, and ``False `` otherwise (including in the parent
1007+ process and in a normal, non-isolated test run). Fixtures such as
1008+ :meth: `~unittest.TestCase.setUp `, :meth: `~unittest.TestCase.tearDown `,
1009+ :meth: `~unittest.TestCase.setUpClass `, :meth: `~unittest.TestCase.tearDownClass `,
1010+ ``setUpModule() `` and ``tearDownModule() `` can test it to choose which code
1011+ to run in the subprocess.
1012+
1013+
1014+ .. currentmodule :: test.support
1015+
1016+
9641017.. function :: check_free_after_iterating(test, iter, cls, args=())
9651018
9661019 Assert instances of *cls * are deallocated after iterating.
0 commit comments