File: utilities.txt

package info (click to toggle)
python-testfixtures 8.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,064 kB
  • sloc: python: 10,208; makefile: 76; sh: 9
file content (166 lines) | stat: -rw-r--r-- 3,325 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
Utilities
=========

.. currentmodule:: testfixtures

This section describes a few handy functions that didn't fit nicely in
any other section.

.. _generator:

The ``generator`` helper
------------------------

It can be handy when testing to be able to turn a simple sequence into
a generator. This can be necessary when you want to check that your
code will behave correctly when processing a generator instead of a
simple sequence, or when you're looking to make assertions about the
expected return value of a callable that returns a generator.

If you need to turn a simple sequence into a generator, the
:func:`generator` function is the way to do it:

>>> from testfixtures import generator
>>> generator(1,2,3)
<generator object ...>

Iterating over this generator will return the arguments passed to the
:func:`generator` function:

>>> for i in _:
...     print(i, end=' ')
1 2 3 

The ``wrap`` helper
-------------------

The :func:`wrap` helper is a decorator function that allows you to
wrap the call to the decorated callable with calls to other
callables. This can be useful when you want to perform setup and
teardown actions either side of a test function.

For example, take the following functions:

.. code-block:: python

  def before():
      print("before")

  def after():
      print("after")

The :func:`wrap` helper can be used to wrap a function with these:

.. code-block:: python

  from testfixtures import wrap

  @wrap(before, after)
  def a_function():
      print("a_function")

When the wrapped function is executed, the output is as follows:

>>> a_function()
before
a_function
after

The section argument to :func:`wrap` is optional:

.. code-block:: python

  from testfixtures import wrap

  @wrap(before)
  def a_function():
      print("a_function")

Now, the wrapped function gives the following output when executed:

>>> a_function()
before
a_function

Multiple wrapping functions can be provided by stacking :func:`wrap`
decorations:

.. code-block:: python

  def before1():
      print("before 1")

  def after1():
      print("after 1")

  def before2():
      print("before 2")

  def after2():
      print("after 2")

  @wrap(before2, after2)
  @wrap(before1, after1)
  def a_function():
      print("a_function")

The order of execution is illustrated below:

>>> a_function()
before 1
before 2
a_function
after 2
after 1

The results of calling the wrapping functions executed before the
wrapped function can be made available to the wrapped function
provided it accepts positional arguments for these results:

.. code-block:: python

  def before1():
      return "return 1"

  def before2():
      return "return 2"

  @wrap(before2)
  @wrap(before1)
  def a_function(r1, r2):
      print(r1)
      print(r2)

Calling the wrapped function illustrates the behaviour:

>>> a_function()
return 1
return 2

Finally, the return value of the wrapped function will always be that
of the original function:

.. code-block:: python

  def before1():
      return 1

  def after1():
      return 2

  def before2():
      return 3

  def after2():
      return 4

  @wrap(before2, after2)
  @wrap(before1, after2)
  def a_function():
      return 'original'

When the above wrapped function is executed, the original return value
is still returned:

>>> a_function()
'original'