2010-11-25 19:11:10 +08:00
|
|
|
.. _xunitsetup:
|
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
====================================
|
2011-09-06 17:43:42 +08:00
|
|
|
Extended xUnit style setup fixtures
|
2010-10-11 05:45:45 +08:00
|
|
|
====================================
|
|
|
|
|
|
|
|
.. _`funcargs`: funcargs.html
|
|
|
|
.. _`test parametrization`: funcargs.html#parametrizing-tests
|
|
|
|
.. _`unittest plugin`: plugin/unittest.html
|
|
|
|
.. _`xUnit`: http://en.wikipedia.org/wiki/XUnit
|
|
|
|
|
|
|
|
Python, Java and many other languages support xUnit_ style testing.
|
|
|
|
This typically involves the call of a ``setup`` ("fixture") method
|
|
|
|
before running a test function and ``teardown`` after it has finished.
|
|
|
|
``py.test`` supports a more fine-grained model of setup/teardown
|
|
|
|
handling by optionally calling per-module and per-class hooks.
|
|
|
|
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Module level setup/teardown
|
2010-10-11 05:45:45 +08:00
|
|
|
=============================================
|
|
|
|
|
|
|
|
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 up 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.
|
|
|
|
"""
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Class level setup/teardown
|
2010-10-11 05:45:45 +08:00
|
|
|
=============================================
|
|
|
|
|
|
|
|
Similarly, the following methods are called at class level before
|
|
|
|
and after all test methods of the class are called::
|
|
|
|
|
2011-03-17 02:05:28 +08:00
|
|
|
@classmethod
|
2010-10-11 05:45:45 +08:00
|
|
|
def setup_class(cls):
|
|
|
|
""" setup up any state specific to the execution
|
|
|
|
of the given class (which usually contains tests).
|
|
|
|
"""
|
|
|
|
|
2011-03-17 02:05:28 +08:00
|
|
|
@classmethod
|
2010-10-11 05:45:45 +08:00
|
|
|
def teardown_class(cls):
|
|
|
|
""" teardown any state that was previously setup
|
|
|
|
with a call to setup_class.
|
|
|
|
"""
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Method and function level setup/teardown
|
2010-10-11 05:45:45 +08:00
|
|
|
=============================================
|
|
|
|
|
|
|
|
Similarly, the following methods are called around each method invocation::
|
|
|
|
|
|
|
|
def setup_method(self, method):
|
|
|
|
""" setup up any state tied to the execution of the given
|
2010-11-02 07:53:53 +08:00
|
|
|
method in a class. setup_method is invoked for every
|
|
|
|
test method of a class.
|
2010-10-11 05:45:45 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def teardown_method(self, method):
|
|
|
|
""" teardown any state that was previously setup
|
2010-11-02 07:53:53 +08:00
|
|
|
with a setup_method call.
|
2010-10-11 05:45:45 +08:00
|
|
|
"""
|
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
If you would rather define test functions directly at module level
|
2010-10-11 05:45:45 +08:00
|
|
|
you can also use the following functions to implement fixtures::
|
|
|
|
|
|
|
|
def setup_function(function):
|
|
|
|
""" setup up any state tied to the execution of the given
|
2010-11-02 07:53:53 +08:00
|
|
|
function. Invoked for every test function in the module.
|
2010-10-11 05:45:45 +08:00
|
|
|
"""
|
|
|
|
|
2011-01-14 06:50:10 +08:00
|
|
|
def teardown_function(function):
|
2010-10-11 05:45:45 +08:00
|
|
|
""" teardown any state that was previously setup
|
2010-11-02 07:53:53 +08:00
|
|
|
with a setup_function call.
|
|
|
|
"""
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
Note that it possible that setup/teardown pairs are invoked multiple
|
|
|
|
times per testing process.
|
|
|
|
|
|
|
|
.. _`unittest.py module`: http://docs.python.org/library/unittest.html
|
|
|
|
|