diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py index 8b06fad0620946..9d96d993e058d4 100644 --- a/Lib/unittest/async_case.py +++ b/Lib/unittest/async_case.py @@ -79,8 +79,14 @@ async def enterAsyncContext(self, cm): return result def _callSetUp(self): - self._asyncioTestContext.run(self.setUp) - self._callAsync(self.asyncSetUp) + self._setupAsyncioRunner() + try: + self._asyncioTestContext.run(self.setUp) + self._callAsync(self.asyncSetUp) + except Exception: + # _callTearDown is not called when _callSetUp fails. + self._tearDownAsyncioRunner() + raise def _callTestMethod(self, method): if self._callMaybeAsync(method) is not None: @@ -88,8 +94,11 @@ def _callTestMethod(self, method): f'test case ({method})', DeprecationWarning, stacklevel=4) def _callTearDown(self): - self._callAsync(self.asyncTearDown) - self._asyncioTestContext.run(self.tearDown) + try: + self._callAsync(self.asyncTearDown) + self._asyncioTestContext.run(self.tearDown) + finally: + self._tearDownAsyncioRunner() def _callCleanup(self, function, *args, **kwargs): self._callMaybeAsync(function, *args, **kwargs) @@ -125,18 +134,6 @@ def _tearDownAsyncioRunner(self): runner = self._asyncioRunner runner.close() - def run(self, result=None): - self._setupAsyncioRunner() - try: - return super().run(result) - finally: - self._tearDownAsyncioRunner() - - def debug(self): - self._setupAsyncioRunner() - super().debug() - self._tearDownAsyncioRunner() - def __del__(self): if self._asyncioRunner is not None: self._tearDownAsyncioRunner() diff --git a/Misc/NEWS.d/next/Library/2022-08-16-19-49-39.gh-issue-96030.B3ZQeX.rst b/Misc/NEWS.d/next/Library/2022-08-16-19-49-39.gh-issue-96030.B3ZQeX.rst new file mode 100644 index 00000000000000..8338db0cfafa6f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-08-16-19-49-39.gh-issue-96030.B3ZQeX.rst @@ -0,0 +1,2 @@ +The ``run`` method of :class:`unittest.IsolatedAsyncioTestCase` +no longer sets up an asyncio runner if a test case or class is skipped.