File: xunit_setup.txt

package info (click to toggle)
pytest 2.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,564 kB
  • sloc: python: 16,243; makefile: 162; sh: 68
file content (83 lines) | stat: -rw-r--r-- 2,806 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
.. _xunitsetup:

====================================
Extended xUnit style setup fixtures
====================================

.. _`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.


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.
        """

    def teardown_function(function):
        """ teardown any state that was previously setup with a setup_function
        call.
        """

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