0
0
PyTesttesting~20 mins

File system testing with tmp_path in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
tmp_path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pytest tmp_path file creation
What will be the output of this pytest test when run?
def test_create_file(tmp_path):
    file = tmp_path / "test.txt"
    file.write_text("hello")
    content = file.read_text()
    print(content)
    assert content == "hello"
PyTest
def test_create_file(tmp_path):
    file = tmp_path / "test.txt"
    file.write_text("hello")
    content = file.read_text()
    print(content)
    assert content == "hello"
APrints 'hello' but test fails
BPrints 'hello' and test passes
CRaises FileNotFoundError
DPrints empty string and test fails
Attempts:
2 left
💡 Hint
tmp_path creates a temporary directory unique to the test.
assertion
intermediate
2:00remaining
Correct assertion for file existence with tmp_path
Which assertion correctly checks that a file named 'data.csv' exists in the tmp_path directory?
PyTest
def test_file_exists(tmp_path):
    file = tmp_path / "data.csv"
    file.write_text("id,name\n1,Alice")
    # Which assertion is correct here?
Aassert file.read_text() == ''
Bassert file.is_file()
Cassert tmp_path.exists()
Dassert file.exists()
Attempts:
2 left
💡 Hint
Check if the file path exists on the filesystem.
🔧 Debug
advanced
2:00remaining
Debug failing test with tmp_path and file write
This test fails with an error. What is the cause?
def test_write_file(tmp_path):
    file = tmp_path / "output.txt"
    file.write("data")
    assert file.read_text() == "data"
PyTest
def test_write_file(tmp_path):
    file = tmp_path / "output.txt"
    file.write("data")
    assert file.read_text() == "data"
Aread_text() returns bytes, causing assertion failure
BFile path is incorrect, should use str(file)
Cwrite() is not a method on pathlib.Path, should use write_text()
Dtmp_path is read-only, cannot write files
Attempts:
2 left
💡 Hint
Check pathlib.Path methods for writing text.
🧠 Conceptual
advanced
2:00remaining
Why use tmp_path over tmpdir in pytest?
Which reason best explains why pytest recommends using tmp_path instead of tmpdir for file system tests?
Atmp_path provides pathlib.Path objects which are more modern and easier to use
Btmpdir creates files in the system root directory causing permission errors
Ctmp_path runs tests faster by caching files between tests
Dtmpdir does not clean up files after tests finish
Attempts:
2 left
💡 Hint
Consider the type of object returned by each fixture.
framework
expert
2:00remaining
Handling cleanup of nested directories with tmp_path
In a pytest test using tmp_path, you create nested directories and files:
def test_nested(tmp_path):
    nested_dir = tmp_path / "a" / "b" / "c"
    nested_dir.mkdir(parents=True)
    file = nested_dir / "file.txt"
    file.write_text("content")
    # After test finishes, what happens to these files and directories?
AAll files and directories under tmp_path are deleted automatically after the test
BOnly the top-level tmp_path directory is deleted, nested files remain
CFiles remain but directories are deleted, causing orphan files
DNothing is deleted automatically; manual cleanup is required
Attempts:
2 left
💡 Hint
pytest tmp_path fixture manages cleanup automatically.