Skip to content

Instantly share code, notes, and snippets.

@clbarnes
Created May 7, 2021 09:19
Show Gist options
  • Save clbarnes/d70874628a9c24df8825c07930b4760b to your computer and use it in GitHub Desktop.
Save clbarnes/d70874628a9c24df8825c07930b4760b to your computer and use it in GitHub Desktop.
Temporary directories for in-package doctests managed by pytest
"""Configuration for in-package doctests.
Should be importable (but not useful)
without development dependencies.
Where these files up and when they are deleted is documented
`here <https://pytest.org/en/stable/tmpdir.html#the-default-base-temporary-directory>`_.
"""
from pathlib import Path
try:
import pytest
decorator = pytest.fixture(autouse=True)
except ImportError:
def decorator(fn):
return fn
@decorator
def add_tmp_dir(doctest_namespace, tmpdir):
"""Give all doctests access to a ``tmp_dir`` variable.
``tmp_dir`` is a ``pathlib.Path`` to a real directory
in pytest's tmp directory which is automatically cleaned up
in later invocations of ``pytest``.
"""
doctest_namespace["tmp_dir"] = Path(tmpdir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment