test_ok1/doc/en/xunit_setup.txt

80 lines
2.5 KiB
Plaintext
Raw Normal View History

.. _`classic xunit`:
classic xunit-style setup
========================================
.. note::
This section describes the old way how you can implement setup and
teardown on a per-module/class/function basis. It remains fully
supported but it is recommended to rather use :ref:`fixture functions
<fixture>` or :ref:`funcargs <resources>` for implementing your
needs to prepare and fix the test state for your tests.
Module level setup/teardown
--------------------------------------
If you have multiple test functions and test classes in a single
module you can optionally implement the following fixture methods
which will usually be called once for all the functions::
def setup_module(module):
""" setup any state specific to the execution of the given module."""
def teardown_module(module):
""" teardown any state that was previously setup with a setup_module
method.
"""
Class level setup/teardown
----------------------------------
Similarly, the following methods are called at class level before
and after all test methods of the class are called::
@classmethod
def setup_class(cls):
""" setup any state specific to the execution of the given class (which
usually contains tests).
"""
@classmethod
def teardown_class(cls):
""" teardown any state that was previously setup with a call to
setup_class.
"""
Method and function level setup/teardown
-----------------------------------------------
Similarly, the following methods are called around each method invocation::
def setup_method(self, method):
""" setup any state tied to the execution of the given method in a
class. setup_method is invoked for every test method of a class.
"""
def teardown_method(self, method):
""" teardown any state that was previously setup with a setup_method
call.
"""
If you would rather define test functions directly at module level
you can also use the following functions to implement fixtures::
def setup_function(function):
""" setup any state tied to the execution of the given function.
Invoked for every test function in the module.
"""
2011-01-14 06:50:10 +08:00
def teardown_function(function):
""" teardown any state that was previously setup with a setup_function
call.
2010-11-02 07:53:53 +08:00
"""
Note that it is possible for setup/teardown pairs to be invoked multiple times
per testing process.
.. _`unittest.py module`: http://docs.python.org/library/unittest.html