Challenge - 5 Problems
tmp_path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"
Attempts:
2 left
💡 Hint
tmp_path creates a temporary directory unique to the test.
✗ Incorrect
The tmp_path fixture provides a temporary directory. Writing and reading the file works as expected, so 'hello' is printed and the assertion passes.
❓ assertion
intermediate2: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?
Attempts:
2 left
💡 Hint
Check if the file path exists on the filesystem.
✗ Incorrect
file.exists() returns True if the file exists. file.is_file() returns True only if the path points to a regular file. tmp_path.exists() checks the directory, not the file. The last option checks content, not existence.
🔧 Debug
advanced2: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"
Attempts:
2 left
💡 Hint
Check pathlib.Path methods for writing text.
✗ Incorrect
pathlib.Path objects do not have a write() method. The correct method to write text is write_text(). Using write() causes AttributeError.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Consider the type of object returned by each fixture.
✗ Incorrect
tmp_path returns pathlib.Path objects which have a rich API and are preferred in modern Python. tmpdir returns py.path.local objects which are older and less standard.
❓ framework
expert2: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?Attempts:
2 left
💡 Hint
pytest tmp_path fixture manages cleanup automatically.
✗ Incorrect
pytest tmp_path creates a temporary directory unique to the test and deletes it entirely after the test finishes, including all nested files and directories.