Created
May 29, 2019 14:45
-
-
Save anapaulagomes/838dc5634c6fb0dfd2850a6eb51cc750 to your computer and use it in GitHub Desktop.
Fixture to create files using pytest tmpdir
This file contains hidden or 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
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