@@ -12,12 +12,15 @@ Asserting with the ``assert`` statement
1212
1313``pytest `` allows you to use the standard python ``assert `` for verifying
1414expectations and values in Python tests. For example, you can write the
15- following::
15+ following:
16+
17+ .. code-block :: python
1618
1719 # content of test_assert1.py
1820 def f ():
1921 return 3
2022
23+
2124 def test_function ():
2225 assert f() == 4
2326
@@ -52,7 +55,9 @@ operators. (See :ref:`tbreportdemo`). This allows you to use the
5255idiomatic python constructs without boilerplate code while not losing
5356introspection information.
5457
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
5661
5762 assert a % 2 == 0 , " value was odd, should be even"
5863
@@ -67,38 +72,49 @@ Assertions about expected exceptions
6772------------------------------------------
6873
6974In 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
7178
7279 import pytest
7380
81+
7482 def test_zero_division ():
7583 with pytest.raises(ZeroDivisionError ):
7684 1 / 0
7785
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
7989
8090 def test_recursion_depth ():
8191 with pytest.raises(RuntimeError ) as excinfo:
92+
8293 def f ():
8394 f()
95+
8496 f()
85- assert ' maximum recursion' in str(excinfo.value)
97+ assert " maximum recursion" in str (excinfo.value)
8698
8799 ``excinfo `` is a ``ExceptionInfo `` instance, which is a wrapper around
88100the actual exception raised. The main attributes of interest are
89101``.type ``, ``.value `` and ``.traceback ``.
90102
91103You can pass a ``match `` keyword parameter to the context-manager to test
92104that 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
94108
95109 import pytest
96110
111+
97112 def myfunc ():
98113 raise ValueError (" Exception 123 raised" )
99114
115+
100116 def test_match ():
101- with pytest.raises(ValueError, match=r' .* 123 .*' ):
117+ with pytest.raises(ValueError , match = r " . * 123 . * " ):
102118 myfunc()
103119
104120 The regexp parameter of the ``match `` method is matched with the ``re.search ``
@@ -107,7 +123,9 @@ well.
107123
108124There's an alternate form of the ``pytest.raises `` function where you pass
109125a 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
111129
112130 pytest.raises(ExpectedException, func, * args, ** kwargs)
113131
@@ -116,7 +134,9 @@ exception* or *wrong exception*.
116134
117135Note that it is also possible to specify a "raises" argument to
118136``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
120140
121141 @pytest.mark.xfail (raises = IndexError )
122142 def test_f ():
@@ -148,10 +168,13 @@ Making use of context-sensitive comparisons
148168.. versionadded :: 2.0
149169
150170``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
152174
153175 # content of test_assert2.py
154176
177+
155178 def test_set_comparison ():
156179 set1 = set (" 1308" )
157180 set2 = set (" 8035" )
@@ -205,16 +228,21 @@ the ``pytest_assertrepr_compare`` hook.
205228 :noindex:
206229
207230As 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
209234
210235 # content of conftest.py
211236 from test_foocompare import Foo
237+
238+
212239 def pytest_assertrepr_compare (op , left , right ):
213240 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)]
216242
217- now, given this test module::
243+ now, given this test module:
244+
245+ .. code-block :: python
218246
219247 # content of test_foocompare.py
220248 class Foo (object ):
@@ -224,6 +252,7 @@ now, given this test module::
224252 def __eq__ (self , other ):
225253 return self .val == other.val
226254
255+
227256 def test_compare ():
228257 f1 = Foo(1 )
229258 f2 = Foo(2 )
0 commit comments