2010-11-25 19:11:10 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
.. _`classic xunit`:
|
2012-10-07 19:06:17 +08:00
|
|
|
.. _xunitsetup:
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
classic xunit-style setup
|
2012-08-02 18:07:54 +08:00
|
|
|
========================================
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2012-10-25 19:48:31 +08:00
|
|
|
This section describes a classic and popular way how you can implement
|
|
|
|
fixtures (setup and teardown test state) on a per-module/class/function basis.
|
|
|
|
pytest started supporting these methods around 2005 and subsequently
|
|
|
|
nose and the standard library introduced them (under slightly different
|
|
|
|
names). While these setup/teardown methods are and will remain fully
|
|
|
|
supported you may also use pytest's more powerful :ref:`fixture mechanism
|
|
|
|
<fixture>` which leverages the concept of dependency injection, allowing
|
|
|
|
for a more modular and more scalable approach for managing test state,
|
2013-08-02 15:52:40 +08:00
|
|
|
especially for larger projects and for functional testing. You can
|
|
|
|
mix both fixture mechanisms in the same file but unittest-based
|
|
|
|
test methods cannot receive fixture arguments.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
As of pytest-2.4, teardownX functions are not called if
|
|
|
|
setupX existed and failed/was skipped. This harmonizes
|
|
|
|
behaviour across all major python testing tools.
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Module level setup/teardown
|
2012-08-02 18:07:54 +08:00
|
|
|
--------------------------------------
|
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):
|
2011-12-05 18:10:48 +08:00
|
|
|
""" setup any state specific to the execution of the given module."""
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
def teardown_module(module):
|
2011-12-05 18:10:48 +08:00
|
|
|
""" teardown any state that was previously setup with a setup_module
|
|
|
|
method.
|
2010-10-11 05:45:45 +08:00
|
|
|
"""
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Class level setup/teardown
|
2012-08-02 18:07:54 +08:00
|
|
|
----------------------------------
|
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):
|
2011-12-05 18:10:48 +08:00
|
|
|
""" setup any state specific to the execution of the given class (which
|
|
|
|
usually contains tests).
|
2010-10-11 05:45:45 +08:00
|
|
|
"""
|
|
|
|
|
2011-03-17 02:05:28 +08:00
|
|
|
@classmethod
|
2010-10-11 05:45:45 +08:00
|
|
|
def teardown_class(cls):
|
2011-12-05 18:10:48 +08:00
|
|
|
""" teardown any state that was previously setup with a call to
|
|
|
|
setup_class.
|
2010-10-11 05:45:45 +08:00
|
|
|
"""
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Method and function level setup/teardown
|
2012-08-02 18:07:54 +08:00
|
|
|
-----------------------------------------------
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
Similarly, the following methods are called around each method invocation::
|
|
|
|
|
|
|
|
def setup_method(self, method):
|
2011-12-05 18:10:48 +08:00
|
|
|
""" 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.
|
2010-10-11 05:45:45 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def teardown_method(self, method):
|
2011-12-05 18:10:48 +08:00
|
|
|
""" teardown any state that was previously setup 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):
|
2011-12-05 18:10:48 +08:00
|
|
|
""" setup any state tied to the execution of the given 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):
|
2011-12-05 18:10:48 +08:00
|
|
|
""" teardown any state that was previously setup with a setup_function
|
|
|
|
call.
|
2010-11-02 07:53:53 +08:00
|
|
|
"""
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2011-12-05 18:10:48 +08:00
|
|
|
Note that it is possible for setup/teardown pairs to be invoked multiple times
|
|
|
|
per testing process.
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2011-12-05 18:10:48 +08:00
|
|
|
.. _`unittest.py module`: http://docs.python.org/library/unittest.html
|