@@ -12,12 +12,15 @@ Asserting with the ``assert`` statement
12
12
13
13
``pytest `` allows you to use the standard python ``assert `` for verifying
14
14
expectations and values in Python tests. For example, you can write the
15
- following::
15
+ following:
16
+
17
+ .. code-block :: python
16
18
17
19
# content of test_assert1.py
18
20
def f ():
19
21
return 3
20
22
23
+
21
24
def test_function ():
22
25
assert f() == 4
23
26
@@ -52,7 +55,9 @@ operators. (See :ref:`tbreportdemo`). This allows you to use the
52
55
idiomatic python constructs without boilerplate code while not losing
53
56
introspection information.
54
57
55
- However, if you specify a message with the assertion like this::
58
+ However, if you specify a message with the assertion like this:
59
+
60
+ .. code-block :: python
56
61
57
62
assert a % 2 == 0 , " value was odd, should be even"
58
63
@@ -67,38 +72,49 @@ Assertions about expected exceptions
67
72
------------------------------------------
68
73
69
74
In order to write assertions about raised exceptions, you can use
70
- ``pytest.raises `` as a context manager like this::
75
+ ``pytest.raises `` as a context manager like this:
76
+
77
+ .. code-block :: python
71
78
72
79
import pytest
73
80
81
+
74
82
def test_zero_division ():
75
83
with pytest.raises(ZeroDivisionError ):
76
84
1 / 0
77
85
78
- and if you need to have access to the actual exception info you may use::
86
+ and if you need to have access to the actual exception info you may use:
87
+
88
+ .. code-block :: python
79
89
80
90
def test_recursion_depth ():
81
91
with pytest.raises(RuntimeError ) as excinfo:
92
+
82
93
def f ():
83
94
f()
95
+
84
96
f()
85
- assert ' maximum recursion' in str(excinfo.value)
97
+ assert " maximum recursion" in str (excinfo.value)
86
98
87
99
``excinfo `` is a ``ExceptionInfo `` instance, which is a wrapper around
88
100
the actual exception raised. The main attributes of interest are
89
101
``.type ``, ``.value `` and ``.traceback ``.
90
102
91
103
You can pass a ``match `` keyword parameter to the context-manager to test
92
104
that a regular expression matches on the string representation of an exception
93
- (similar to the ``TestCase.assertRaisesRegexp `` method from ``unittest ``)::
105
+ (similar to the ``TestCase.assertRaisesRegexp `` method from ``unittest ``):
106
+
107
+ .. code-block :: python
94
108
95
109
import pytest
96
110
111
+
97
112
def myfunc ():
98
113
raise ValueError (" Exception 123 raised" )
99
114
115
+
100
116
def test_match ():
101
- with pytest.raises(ValueError, match=r' .* 123 .*' ):
117
+ with pytest.raises(ValueError , match = r " . * 123 . * " ):
102
118
myfunc()
103
119
104
120
The regexp parameter of the ``match `` method is matched with the ``re.search ``
@@ -107,7 +123,9 @@ well.
107
123
108
124
There's an alternate form of the ``pytest.raises `` function where you pass
109
125
a function that will be executed with the given ``*args `` and ``**kwargs `` and
110
- assert that the given exception is raised::
126
+ assert that the given exception is raised:
127
+
128
+ .. code-block :: python
111
129
112
130
pytest.raises(ExpectedException, func, * args, ** kwargs)
113
131
@@ -116,7 +134,9 @@ exception* or *wrong exception*.
116
134
117
135
Note that it is also possible to specify a "raises" argument to
118
136
``pytest.mark.xfail ``, which checks that the test is failing in a more
119
- specific way than just having any exception raised::
137
+ specific way than just having any exception raised:
138
+
139
+ .. code-block :: python
120
140
121
141
@pytest.mark.xfail (raises = IndexError )
122
142
def test_f ():
@@ -148,10 +168,13 @@ Making use of context-sensitive comparisons
148
168
.. versionadded :: 2.0
149
169
150
170
``pytest `` has rich support for providing context-sensitive information
151
- when it encounters comparisons. For example::
171
+ when it encounters comparisons. For example:
172
+
173
+ .. code-block :: python
152
174
153
175
# content of test_assert2.py
154
176
177
+
155
178
def test_set_comparison ():
156
179
set1 = set (" 1308" )
157
180
set2 = set (" 8035" )
@@ -205,16 +228,21 @@ the ``pytest_assertrepr_compare`` hook.
205
228
:noindex:
206
229
207
230
As an example consider adding the following hook in a :ref: `conftest.py <conftest.py >`
208
- file which provides an alternative explanation for ``Foo `` objects::
231
+ file which provides an alternative explanation for ``Foo `` objects:
232
+
233
+ .. code-block :: python
209
234
210
235
# content of conftest.py
211
236
from test_foocompare import Foo
237
+
238
+
212
239
def pytest_assertrepr_compare (op , left , right ):
213
240
if isinstance (left, Foo) and isinstance (right, Foo) and op == " ==" :
214
- return ['Comparing Foo instances:',
215
- ' vals: %s != %s' % (left.val, right.val)]
241
+ return [" Comparing Foo instances:" , " vals: %s != %s " % (left.val, right.val)]
216
242
217
- now, given this test module::
243
+ now, given this test module:
244
+
245
+ .. code-block :: python
218
246
219
247
# content of test_foocompare.py
220
248
class Foo (object ):
@@ -224,6 +252,7 @@ now, given this test module::
224
252
def __eq__ (self , other ):
225
253
return self .val == other.val
226
254
255
+
227
256
def test_compare ():
228
257
f1 = Foo(1 )
229
258
f2 = Foo(2 )
0 commit comments