You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reporting details about a failing assertion is achieved either by rewriting
266
-
assert statements before they are run or re-evaluating the assert expression and
267
-
recording the intermediate values. Which technique is used depends on the
268
-
location of the assert, ``pytest`` configuration, and Python version being used
269
-
to run ``pytest``.
270
-
271
-
By default, ``pytest`` rewrites assert statements in test modules.
272
-
Rewritten assert statements put introspection information into the assertion failure message.
273
-
``pytest`` only rewrites test modules directly discovered by its test collection process, so
274
-
asserts in supporting modules which are not themselves test modules will not be
275
-
rewritten.
265
+
Reporting details about a failing assertion is achieved by rewriting assert
266
+
statements before they are run. Rewritten assert statements put introspection
267
+
information into the assertion failure message. ``pytest`` only rewrites test
268
+
modules directly discovered by its test collection process, so asserts in
269
+
supporting modules which are not themselves test modules will not be rewritten.
276
270
277
271
.. note::
278
272
279
273
``pytest`` rewrites test modules on import. It does this by using an import
280
-
hook to write a new pyc files. Most of the time this works transparently.
274
+
hook to write new pyc files. Most of the time this works transparently.
281
275
However, if you are messing with import yourself, the import hook may
282
-
interfere. If this is the case, simply use ``--assert=reinterp`` or
283
-
``--assert=plain``. Additionally, rewriting will fail silently if it cannot
284
-
write new pycs, i.e. in a read-only filesystem or a zipfile.
285
-
286
-
If an assert statement has not been rewritten or the Python version is less than
287
-
2.6, ``pytest`` falls back on assert reinterpretation. In assert
288
-
reinterpretation, ``pytest`` walks the frame of the function containing the
289
-
assert statement to discover sub-expression results of the failing assert
290
-
statement. You can force ``pytest`` to always use assertion reinterpretation by
291
-
passing the ``--assert=reinterp`` option.
292
-
293
-
Assert reinterpretation has a caveat not present with assert rewriting: If
294
-
evaluating the assert expression has side effects you may get a warning that the
295
-
intermediate values could not be determined safely. A common example of this
296
-
issue is an assertion which reads from a file::
297
-
298
-
assert f.read() != '...'
299
-
300
-
If this assertion fails then the re-evaluation will probably succeed!
301
-
This is because ``f.read()`` will return an empty string when it is
302
-
called the second time during the re-evaluation. However, it is
303
-
easy to rewrite the assertion and avoid any trouble::
304
-
305
-
content = f.read()
306
-
assert content != '...'
307
-
308
-
All assert introspection can be turned off by passing ``--assert=plain``.
276
+
interfere. If this is the case, use ``--assert=plain``. Additionally,
277
+
rewriting will fail silently if it cannot write new pycs, i.e. in a read-only
278
+
filesystem or a zipfile.
309
279
310
280
For further information, Benjamin Peterson wrote up `Behind the scenes of pytest's new assertion rewriting <http://pybites.blogspot.com/2011/07/behind-scenes-of-pytests-new-assertion.html>`_.
311
281
@@ -318,3 +288,4 @@ For further information, Benjamin Peterson wrote up `Behind the scenes of pytest
318
288
319
289
.. versionchanged:: 3.0
320
290
Removes the ``--no-assert`` and``--nomagic`` options.
0 commit comments