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
|
|
|
|
2021-03-15 16:22:11 +08:00
|
|
|
How to implement xunit-style set-up
|
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
|
2020-08-01 22:33:03 +08:00
|
|
|
coming from a ``unittest`` or ``nose`` background, you may also consider
|
2016-07-15 08:28:59 +08:00
|
|
|
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
|
2019-08-07 07:20:06 +08:00
|
|
|
which will usually be called once for all the functions:
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
def setup_module(module):
|
2022-04-28 22:30:16 +08:00
|
|
|
"""setup any state specific to the execution of the given module."""
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2019-08-07 04:34:58 +08:00
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
def teardown_module(module):
|
2020-12-30 17:56:09 +08:00
|
|
|
"""teardown any state that was previously setup with a setup_module
|
2011-12-05 18:10:48 +08:00
|
|
|
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
|
2019-08-07 07:20:06 +08:00
|
|
|
and after all test methods of the class are called:
|
2010-10-11 05:45:45 +08:00
|
|
|
|
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):
|
2020-12-30 17:56:09 +08:00
|
|
|
"""setup any state specific to the execution of the given class (which
|
2011-12-05 18:10:48 +08:00
|
|
|
usually contains tests).
|
2010-10-11 05:45:45 +08:00
|
|
|
"""
|
|
|
|
|
2019-08-07 04:34:58 +08:00
|
|
|
|
2011-03-17 02:05:28 +08:00
|
|
|
@classmethod
|
2010-10-11 05:45:45 +08:00
|
|
|
def teardown_class(cls):
|
2020-12-30 17:56:09 +08:00
|
|
|
"""teardown any state that was previously setup with a call to
|
2011-12-05 18:10:48 +08:00
|
|
|
setup_class.
|
2010-10-11 05:45:45 +08:00
|
|
|
"""
|
|
|
|
|
2022-10-10 04:16:33 +08:00
|
|
|
.. _xunit-method-setup:
|
|
|
|
|
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
|
|
|
|
2019-08-07 07:20:06 +08:00
|
|
|
Similarly, the following methods are called around each method invocation:
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
def setup_method(self, method):
|
2020-12-30 17:56:09 +08:00
|
|
|
"""setup any state tied to the execution of the given method in a
|
2011-12-05 18:10:48 +08:00
|
|
|
class. setup_method is invoked for every test method of a class.
|
2010-10-11 05:45:45 +08:00
|
|
|
"""
|
|
|
|
|
2019-08-07 04:34:58 +08:00
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
def teardown_method(self, method):
|
2020-12-30 17:56:09 +08:00
|
|
|
"""teardown any state that was previously setup with a setup_method
|
2011-12-05 18:10:48 +08:00
|
|
|
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
|
2019-08-07 07:20:06 +08:00
|
|
|
you can also use the following functions to implement fixtures:
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
def setup_function(function):
|
2020-12-30 17:56:09 +08:00
|
|
|
"""setup any state tied to the execution of the given function.
|
2011-12-05 18:10:48 +08:00
|
|
|
Invoked for every test function in the module.
|
2010-10-11 05:45:45 +08:00
|
|
|
"""
|
|
|
|
|
2019-08-07 04:34:58 +08:00
|
|
|
|
2011-01-14 06:50:10 +08:00
|
|
|
def teardown_function(function):
|
2020-12-30 17:56:09 +08:00
|
|
|
"""teardown any state that was previously setup with a setup_function
|
2011-12-05 18:10:48 +08:00
|
|
|
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.
|