0
0
PyTesttesting~20 mins

Temporary directory management in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Temporary Directory 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 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()
A"hello"
BFileNotFoundError
C""
DAttributeError
Attempts:
2 left
💡 Hint
tmp_path is a pathlib.Path object for a temporary directory unique to the test.
assertion
intermediate
2: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?
Aassert tmp_path.exists('data.csv')
Bassert tmp_path.has_file('data.csv')
Cassert (tmp_path / 'data.csv').exists()
Dassert 'data.csv' in tmp_path
Attempts:
2 left
💡 Hint
Use pathlib.Path methods to check file existence.
🔧 Debug
advanced
2: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')
ATypeError: unsupported operand type(s) for +: 'Path' and 'str'
BFileNotFoundError
CAttributeError: 'str' object has no attribute 'write_text'
DNo error, test passes
Attempts:
2 left
💡 Hint
Check how to combine pathlib.Path objects with strings.
framework
advanced
2:00remaining
Behavior of pytest tmp_path fixture scope
Which statement about the pytest tmp_path fixture is true?
Atmp_path is a string path, not a pathlib.Path object
Btmp_path provides a unique temporary directory for each test function invocation
Ctmp_path requires manual cleanup of files after each test
Dtmp_path creates a single temporary directory shared across all tests in a module
Attempts:
2 left
💡 Hint
Consider the isolation pytest provides for tests using tmp_path.
🧠 Conceptual
expert
2: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?
Atmp_path only works on Windows systems
Btmp_path creates permanent directories for debugging after tests finish
Ctmp_path requires manual cleanup unlike tempfile.TemporaryDirectory
Dtmp_path integrates with pytest's test lifecycle and provides pathlib.Path objects for easier file operations
Attempts:
2 left
💡 Hint
Think about pytest features and pathlib benefits.