Created
August 16, 2020 16:33
-
-
Save zahlman/7963824f7e2a103ab83e2d7d27da9c7a to your computer and use it in GitHub Desktop.
Clean up pytest session completely after creating per-test 'environment' folders.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, shutil | |
from pathlib import Path | |
import pytest | |
@pytest.fixture(scope='session', autouse=True) | |
def session(tmp_path_factory): | |
try: | |
yield | |
finally: | |
shutil.rmtree(str(tmp_path_factory.getbasetemp())) | |
@pytest.fixture | |
def environment(tmp_path): | |
try: | |
old_path = Path.cwd() | |
os.chdir(tmp_path) | |
# dirs_exist_ok requires Python 3.8. | |
shutil.copytree(HERE / 'environment', '.', dirs_exist_ok=True) | |
yield tmp_path | |
finally: | |
os.chdir(old_path) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment