diff options
author | Lisa Roach <lisaroach14@gmail.com> | 2018-11-09 02:34:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-09 02:34:33 (GMT) |
commit | 0f221d09cad46bee38d1b7a7822772df66c53028 (patch) | |
tree | 5c121b9d4747136f74eeac45c62211a2ae32dceb /Doc/library/unittest.rst | |
parent | 49fa4a9f1ef387e16596f271414c855339eadf09 (diff) | |
download | cpython-0f221d09cad46bee38d1b7a7822772df66c53028.zip cpython-0f221d09cad46bee38d1b7a7822772df66c53028.tar.gz cpython-0f221d09cad46bee38d1b7a7822772df66c53028.tar.bz2 |
bpo-24412: Adds cleanUps for setUpClass and setUpModule. (GH-9190)
Diffstat (limited to 'Doc/library/unittest.rst')
-rw-r--r-- | Doc/library/unittest.rst | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst index 1153459..c401908 100644 --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -1448,6 +1448,39 @@ Test cases .. versionadded:: 3.1 + .. classmethod:: addClassCleanup(function, *args, **kwargs) + + Add a function to be called after :meth:`tearDownClass` to cleanup + resources used during the test class. Functions will be called in reverse + order to the order they are added (:abbr:`LIFO (last-in, first-out)`). + They are called with any arguments and keyword arguments passed into + :meth:`addClassCleanup` when they are added. + + If :meth:`setUpClass` fails, meaning that :meth:`tearDownClass` is not + called, then any cleanup functions added will still be called. + + .. versionadded:: 3.8 + + + .. classmethod:: doClassCleanups() + + This method is called unconditionally after :meth:`tearDownClass`, or + after :meth:`setUpClass` if :meth:`setUpClass` raises an exception. + + It is responsible for calling all the cleanup functions added by + :meth:`addCleanupClass`. If you need cleanup functions to be called + *prior* to :meth:`tearDownClass` then you can call + :meth:`doCleanupsClass` yourself. + + :meth:`doCleanupsClass` pops methods off the stack of cleanup + functions one at a time, so it can be called at any time. + + .. versionadded:: 3.8 + + + + + .. class:: FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None) @@ -2268,6 +2301,38 @@ module will be run and the ``tearDownModule`` will not be run. If the exception :exc:`SkipTest` exception then the module will be reported as having been skipped instead of as an error. +To add cleanup code that must be run even in the case of an exception, use +``addModuleCleanup``: + + +.. function:: addModuleCleanup(function, *args, **kwargs) + + Add a function to be called after :func:`tearDownModule` to cleanup + resources used during the test class. Functions will be called in reverse + order to the order they are added (:abbr:`LIFO (last-in, first-out)`). + They are called with any arguments and keyword arguments passed into + :meth:`addModuleCleanup` when they are added. + + If :meth:`setUpModule` fails, meaning that :func:`tearDownModule` is not + called, then any cleanup functions added will still be called. + + .. versionadded:: 3.8 + + +.. function:: doModuleCleanups() + + This function is called unconditionally after :func:`tearDownModule`, or + after :func:`setUpModule` if :func:`setUpModule` raises an exception. + + It is responsible for calling all the cleanup functions added by + :func:`addCleanupModule`. If you need cleanup functions to be called + *prior* to :func:`tearDownModule` then you can call + :func:`doModuleCleanups` yourself. + + :func:`doModuleCleanups` pops methods off the stack of cleanup + functions one at a time, so it can be called at any time. + + .. versionadded:: 3.8 Signal Handling --------------- |