Skip to content

Instantly share code, notes, and snippets.

@anapaulagomes
Created May 29, 2019 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anapaulagomes/838dc5634c6fb0dfd2850a6eb51cc750 to your computer and use it in GitHub Desktop.
Save anapaulagomes/838dc5634c6fb0dfd2850a6eb51cc750 to your computer and use it in GitHub Desktop.
Fixture to create files using pytest tmpdir
from pathlib import Path
import pytest
@pytest.fixture
def file_factory(tmpdir):
def _file_factory(path):
full_path = Path(path)
file_name = full_path.name
def create_path(parts, previous_tmpdir):
if parts[0] != file_name:
current_directory = parts.pop(0)
return create_path(parts, previous_tmpdir.mkdir(current_directory))
else:
return previous_tmpdir.join(parts[0])
return create_path(list(full_path.parts), tmpdir)
return _file_factory
def test_example_using_file_factory(file_factory):
test_file = file_factory(expected_test)
# [...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment