Skip to content

Instantly share code, notes, and snippets.

@bryder
Last active April 27, 2021 13:23
Show Gist options
  • Save bryder/d11537e82c3487e310ed to your computer and use it in GitHub Desktop.
Save bryder/d11537e82c3487e310ed to your computer and use it in GitHub Desktop.
How to use py.test tmpdir
# https://pytest.org/latest/tmpdir.html
# http://py.readthedocs.org/en/latest/path.html
# Using in conftest.py
@pytest.fixture(autouse=True)
def a_tmp_filename(tmpdir):
p = tmpdir.join("filename")
p.write("some stuff\n")
return str(p) # str(p) returns the full pathname you can use with normal modules
def test_thing(tmpdir):
tmp_file = tmpdir.join("thefile_name.json")
tmp_file_name = str(tmp_file)
with open(tmp_file_name, 'w') as f:
json.dump(stuff, tmp_file)
tmp_file.write(a_string_to_write)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment