diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-05-08 14:49:09 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-08 14:49:09 (GMT) |
commit | 086c6b1b0fe8d47ebd15512d7bdcb64c60a360f0 (patch) | |
tree | a7b1eaf75879c3fded1b946b2331f6a45dfc8fc7 /Doc | |
parent | 8f293180791f2836570bdfc29aadba04a538d435 (diff) | |
download | cpython-086c6b1b0fe8d47ebd15512d7bdcb64c60a360f0.zip cpython-086c6b1b0fe8d47ebd15512d7bdcb64c60a360f0.tar.gz cpython-086c6b1b0fe8d47ebd15512d7bdcb64c60a360f0.tar.bz2 |
bpo-45046: Support context managers in unittest (GH-28045)
Add methods enterContext() and enterClassContext() in TestCase.
Add method enterAsyncContext() in IsolatedAsyncioTestCase.
Add function enterModuleContext().
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/unittest.rst | 42 | ||||
-rw-r--r-- | Doc/whatsnew/3.11.rst | 12 |
2 files changed, 54 insertions, 0 deletions
diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst index 9b8b75a..f6bcba0 100644 --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -1495,6 +1495,16 @@ Test cases .. versionadded:: 3.1 + .. method:: enterContext(cm) + + Enter the supplied :term:`context manager`. If successful, also + add its :meth:`~object.__exit__` method as a cleanup function by + :meth:`addCleanup` and return the result of the + :meth:`~object.__enter__` method. + + .. versionadded:: 3.11 + + .. method:: doCleanups() This method is called unconditionally after :meth:`tearDown`, or @@ -1510,6 +1520,7 @@ Test cases .. versionadded:: 3.1 + .. classmethod:: addClassCleanup(function, /, *args, **kwargs) Add a function to be called after :meth:`tearDownClass` to cleanup @@ -1524,6 +1535,16 @@ Test cases .. versionadded:: 3.8 + .. classmethod:: enterClassContext(cm) + + Enter the supplied :term:`context manager`. If successful, also + add its :meth:`~object.__exit__` method as a cleanup function by + :meth:`addClassCleanup` and return the result of the + :meth:`~object.__enter__` method. + + .. versionadded:: 3.11 + + .. classmethod:: doClassCleanups() This method is called unconditionally after :meth:`tearDownClass`, or @@ -1571,6 +1592,16 @@ Test cases This method accepts a coroutine that can be used as a cleanup function. + .. coroutinemethod:: enterAsyncContext(cm) + + Enter the supplied :term:`asynchronous context manager`. If successful, + also add its :meth:`~object.__aexit__` method as a cleanup function by + :meth:`addAsyncCleanup` and return the result of the + :meth:`~object.__aenter__` method. + + .. versionadded:: 3.11 + + .. method:: run(result=None) Sets up a new event loop to run the test, collecting the result into @@ -2465,6 +2496,16 @@ To add cleanup code that must be run even in the case of an exception, use .. versionadded:: 3.8 +.. classmethod:: enterModuleContext(cm) + + Enter the supplied :term:`context manager`. If successful, also + add its :meth:`~object.__exit__` method as a cleanup function by + :func:`addModuleCleanup` and return the result of the + :meth:`~object.__enter__` method. + + .. versionadded:: 3.11 + + .. function:: doModuleCleanups() This function is called unconditionally after :func:`tearDownModule`, or @@ -2480,6 +2521,7 @@ To add cleanup code that must be run even in the case of an exception, use .. versionadded:: 3.8 + Signal Handling --------------- diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index c4e8e6f..defaeeb 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -758,6 +758,18 @@ unicodedata * The Unicode database has been updated to version 14.0.0. (:issue:`45190`). +unittest +-------- + +* Added methods :meth:`~unittest.TestCase.enterContext` and + :meth:`~unittest.TestCase.enterClassContext` of class + :class:`~unittest.TestCase`, method + :meth:`~unittest.IsolatedAsyncioTestCase.enterAsyncContext` of + class :class:`~unittest.IsolatedAsyncioTestCase` and function + :func:`unittest.enterModuleContext`. + (Contributed by Serhiy Storchaka in :issue:`45046`.) + + venv ---- |