O'Reilly logo
  • Chris Kang thinks this is interesting:

 @pytest.fixture(autouse=True)​ def​ add_um(doctest_namespace):​  doctest_namespace[​'um'​] = unnecessary_math


Cover of Python Testing with pytest


This fixture is used with "autouse", so it's run for all test case methods. Because this fixture has dependency on doctest_namespace fixture, we can use "doctest_namespace" to assign an entry with key "um" and value "unnecessary_math", which is the imported module. This "um" will now be available in the doctest_namespace so that the import statement is not needed for each test run.