Skip to content

Instantly share code, notes, and snippets.

@zahlman
Created August 16, 2020 16:33
Show Gist options
  • Save zahlman/7963824f7e2a103ab83e2d7d27da9c7a to your computer and use it in GitHub Desktop.
Save zahlman/7963824f7e2a103ab83e2d7d27da9c7a to your computer and use it in GitHub Desktop.
Clean up pytest session completely after creating per-test 'environment' folders.
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