From 16cbb3196cc376014ebbf53af45928d65021cb88 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 26 Feb 2019 20:39:06 -0300 Subject: [PATCH] Document how to disable caching rewritten .pyc files to disk Also changed how the section is presented: instead of "Note" blocks, use proper sections as those contain enough information to exist on their own. Fix #1680 --- doc/en/assert.rst | 60 ++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/doc/en/assert.rst b/doc/en/assert.rst index b119adcf0..adefe89bb 100644 --- a/doc/en/assert.rst +++ b/doc/en/assert.rst @@ -252,8 +252,8 @@ the conftest file: .. _assert-details: .. _`assert introspection`: -Advanced assertion introspection ----------------------------------- +Assertion introspection details +------------------------------- .. versionadded:: 2.1 @@ -266,28 +266,46 @@ supporting modules which are not themselves test modules will not be rewritten** You can manually enable assertion rewriting for an imported module by calling `register_assert_rewrite `_ -before you import it (a good place to do that is in ``conftest.py``). - -.. note:: - - ``pytest`` rewrites test modules on import by using an import - hook to write new ``pyc`` files. Most of the time this works transparently. - However, if you are messing with import yourself, the import hook may - interfere. - - If this is the case you have two options: - - * Disable rewriting for a specific module by adding the string - ``PYTEST_DONT_REWRITE`` to its docstring. - - * Disable rewriting for all modules by using ``--assert=plain``. - - Additionally, rewriting will fail silently if it cannot write new ``.pyc`` files, - i.e. in a read-only filesystem or a zipfile. - +before you import it (a good place to do that is in your root ``conftest.py``). For further information, Benjamin Peterson wrote up `Behind the scenes of pytest's new assertion rewriting `_. +Assertion rewriting caches files on disk +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``pytest`` will write back the rewritten modules to disk for caching. You can disable +this behavior (for example to avoid leaving stable ``.pyc`` files around in projects that +move files around a lot) by adding this to the top of your ``conftest.py`` file: + +.. code-block:: python + + import sys + + sys.dont_write_bytecode = True + +Note that you still get the benefits of assertion introspection, only change is that +the ``.pyc`` files won't be cached on disk. + +Additionally, rewriting will silently skip caching if it cannot write new ``.pyc`` files, +i.e. in a read-only filesystem or a zipfile. + + +Disabling assert rewriting +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``pytest`` rewrites test modules on import by using an import +hook to write new ``pyc`` files. Most of the time this works transparently. +However, if you are messing with import yourself, the import hook may +interfere. + +If this is the case you have two options: + +* Disable rewriting for a specific module by adding the string + ``PYTEST_DONT_REWRITE`` to its docstring. + +* Disable rewriting for all modules by using ``--assert=plain``. + + .. versionadded:: 2.1 Add assert rewriting as an alternate introspection technique.