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
|
2018-05-18 16:19:46 +08:00
|
|
|
fixtures (setup and teardown test state) on a per-module/class/function basis.
|
2016-07-15 08:28:59 +08:00
|
|
|
|
2013-08-02 15:52:40 +08:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2016-07-15 08:28:59 +08:00
|
|
|
While these setup/teardown methods are simple and familiar to those
|
|
|
|
coming from a ``unittest`` or nose ``background``, you may also consider
|
|
|
|
using 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,
|
|
|
|
especially for larger projects and for functional testing. You can
|
|
|
|
mix both fixture mechanisms in the same file but
|
|
|
|
test methods of ``unittest.TestCase`` subclasses
|
|
|
|
cannot receive fixture arguments.
|
|
|
|
|
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::
|
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
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
|
|
|
"""
|
|
|
|
|
2016-07-15 08:28:59 +08:00
|
|
|
As of pytest-3.0, the ``module`` parameter is optional.
|
|
|
|
|
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::
|
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
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::
|
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
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
|
|
|
"""
|
|
|
|
|
2016-07-15 08:28:59 +08:00
|
|
|
As of pytest-3.0, the ``method`` parameter is optional.
|
|
|
|
|
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::
|
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
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
|
|
|
|
2016-07-15 08:28:59 +08:00
|
|
|
As of pytest-3.0, the ``function`` parameter is optional.
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
|
|
|
|
* It is possible for setup/teardown pairs to be invoked multiple times
|
|
|
|
per testing process.
|
2018-10-07 07:30:18 +08:00
|
|
|
|
2016-07-15 08:28:59 +08:00
|
|
|
* teardown functions are not called if the corresponding setup function existed
|
|
|
|
and failed/was skipped.
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2018-10-07 07:30:18 +08:00
|
|
|
* Prior to pytest-4.2, xunit-style functions did not obey the scope rules of fixtures, so
|
|
|
|
it was possible, for example, for a ``setup_method`` to be called before a
|
|
|
|
session-scoped autouse fixture.
|
|
|
|
|
|
|
|
Now the xunit-style functions are integrated with the fixture mechanism and obey the proper
|
|
|
|
scope rules of fixtures involved in the call.
|
|
|
|
|
2011-12-05 18:10:48 +08:00
|
|
|
.. _`unittest.py module`: http://docs.python.org/library/unittest.html
|