pytest temp directory 1

pytest temp directory

def test_something_else(tmp_path):
    #create a file "myfile" in "mydir" in temp directory
    f1 = tmp_path / "mydir/myfile"
    f1.parent.mkdir() #create a directory "mydir" in temp folder (which is the parent directory of "myfile"
    f1.touch() #create a file "myfile" in "mydir"


    #write to file as normal 
    f1.write_text("text to myfile")

    assert f1.read() == "text to myfile"

Here is what the above code is Doing:
1. Create a temporary directory
2. Create a file in the temporary directory
3. Write to the file
4. Assert that the file contains the text we wrote to it

Similar Posts