Challenge - 5 Problems
Temporary Directory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pytest tmp_path usage
What will be the output of this pytest test function when run?
PyTest
def test_create_file(tmp_path): file = tmp_path / "test.txt" file.write_text("hello") return file.read_text()
Attempts:
2 left
💡 Hint
tmp_path is a pathlib.Path object for a temporary directory unique to the test.
✗ Incorrect
The test writes 'hello' to a file in the temporary directory and reads it back, so the output is 'hello'.
❓ assertion
intermediate2:00remaining
Correct assertion for file existence in tmp_path
Which assertion correctly checks that a file named 'data.csv' exists in the pytest tmp_path directory?
Attempts:
2 left
💡 Hint
Use pathlib.Path methods to check file existence.
✗ Incorrect
The exists() method on a Path object returns True if the file or directory exists. The other options are invalid methods or syntax.
🔧 Debug
advanced2:00remaining
Identify the error in tmp_path usage
What error will this pytest test raise when executed?
PyTest
def test_write_file(tmp_path): file = tmp_path + '/output.txt' file.write_text('data')
Attempts:
2 left
💡 Hint
Check how to combine pathlib.Path objects with strings.
✗ Incorrect
You cannot use + to join a Path object and a string. Use the / operator instead.
❓ framework
advanced2:00remaining
Behavior of pytest tmp_path fixture scope
Which statement about the pytest tmp_path fixture is true?
Attempts:
2 left
💡 Hint
Consider the isolation pytest provides for tests using tmp_path.
✗ Incorrect
pytest tmp_path fixture creates a new temporary directory for each test function, ensuring isolation and automatic cleanup.
🧠 Conceptual
expert2:00remaining
Why use tmp_path over tempfile.TemporaryDirectory in pytest?
Which is the main advantage of using pytest's tmp_path fixture instead of Python's tempfile.TemporaryDirectory in tests?
Attempts:
2 left
💡 Hint
Think about pytest features and pathlib benefits.
✗ Incorrect
tmp_path is a pytest fixture that automatically manages temporary directories per test and returns pathlib.Path objects, making file handling simpler and integrated with pytest's lifecycle.