Run ``pytest.main()`` in-process, returning a HookRecorder. Runs the :py:func:`pytest.main` function to run all of pytest inside the test process itself. This means it can return a :py:class:`HookRecorder` instance which gives more detailed results from that run tha
(
self,
*args: Union[str, "os.PathLike[str]"],
plugins=(),
no_reraise_ctrlc: bool = False,
)
| 1052 | return items, rec |
| 1053 | |
| 1054 | def inline_run( |
| 1055 | self, |
| 1056 | *args: Union[str, "os.PathLike[str]"], |
| 1057 | plugins=(), |
| 1058 | no_reraise_ctrlc: bool = False, |
| 1059 | ) -> HookRecorder: |
| 1060 | """Run ``pytest.main()`` in-process, returning a HookRecorder. |
| 1061 | |
| 1062 | Runs the :py:func:`pytest.main` function to run all of pytest inside |
| 1063 | the test process itself. This means it can return a |
| 1064 | :py:class:`HookRecorder` instance which gives more detailed results |
| 1065 | from that run than can be done by matching stdout/stderr from |
| 1066 | :py:meth:`runpytest`. |
| 1067 | |
| 1068 | :param args: |
| 1069 | Command line arguments to pass to :py:func:`pytest.main`. |
| 1070 | :param plugins: |
| 1071 | Extra plugin instances the ``pytest.main()`` instance should use. |
| 1072 | :param no_reraise_ctrlc: |
| 1073 | Typically we reraise keyboard interrupts from the child run. If |
| 1074 | True, the KeyboardInterrupt exception is captured. |
| 1075 | """ |
| 1076 | # (maybe a cpython bug?) the importlib cache sometimes isn't updated |
| 1077 | # properly between file creation and inline_run (especially if imports |
| 1078 | # are interspersed with file creation) |
| 1079 | importlib.invalidate_caches() |
| 1080 | |
| 1081 | plugins = list(plugins) |
| 1082 | finalizers = [] |
| 1083 | try: |
| 1084 | # Any sys.module or sys.path changes done while running pytest |
| 1085 | # inline should be reverted after the test run completes to avoid |
| 1086 | # clashing with later inline tests run within the same pytest test, |
| 1087 | # e.g. just because they use matching test module names. |
| 1088 | finalizers.append(self.__take_sys_modules_snapshot().restore) |
| 1089 | finalizers.append(SysPathsSnapshot().restore) |
| 1090 | |
| 1091 | # Important note: |
| 1092 | # - our tests should not leave any other references/registrations |
| 1093 | # laying around other than possibly loaded test modules |
| 1094 | # referenced from sys.modules, as nothing will clean those up |
| 1095 | # automatically |
| 1096 | |
| 1097 | rec = [] |
| 1098 | |
| 1099 | class Collect: |
| 1100 | def pytest_configure(x, config: Config) -> None: |
| 1101 | rec.append(self.make_hook_recorder(config.pluginmanager)) |
| 1102 | |
| 1103 | plugins.append(Collect()) |
| 1104 | ret = main([str(x) for x in args], plugins=plugins) |
| 1105 | if len(rec) == 1: |
| 1106 | reprec = rec.pop() |
| 1107 | else: |
| 1108 | |
| 1109 | class reprec: # type: ignore |
| 1110 | pass |
| 1111 |
no test coverage detected