Last active
April 27, 2021 13:23
-
-
Save bryder/d11537e82c3487e310ed to your computer and use it in GitHub Desktop.
How to use py.test 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
# 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