Recall & Review
beginner
What is the purpose of the
tmp_path fixture in pytest?The
tmp_path fixture provides a temporary directory unique to the test function. It allows tests to create and manipulate files and directories without affecting the real file system.Click to reveal answer
intermediate
How does
tmp_path differ from tmpdir in pytest?tmp_path returns a pathlib.Path object, which is modern and more versatile, while tmpdir returns a py.path.local object. tmp_path is preferred for new tests.Click to reveal answer
beginner
How do you create a new file named
example.txt inside the tmp_path directory in a pytest test?Use
file = tmp_path / 'example.txt' to create a path object, then file.write_text('content') to write text to the file.Click to reveal answer
beginner
Why is it important to use
tmp_path for file system tests?Using
tmp_path isolates test files from the real file system, preventing accidental data loss or pollution. It also ensures tests are independent and repeatable.Click to reveal answer
beginner
What happens to the files and directories created by
tmp_path after the test finishes?The temporary directory and all its contents are automatically deleted after the test completes, keeping the environment clean.
Click to reveal answer
What type of object does pytest's
tmp_path fixture provide?✗ Incorrect
tmp_path returns a pathlib.Path object for modern path handling.Which of the following is a benefit of using
tmp_path in tests?✗ Incorrect
tmp_path creates a temporary directory isolated from the real file system.How do you write text 'hello' to a file named 'greet.txt' inside
tmp_path?✗ Incorrect
You must create the file path first with (tmp_path / 'greet.txt'), then call write_text on it.
What happens to the temporary directory created by
tmp_path after the test ends?✗ Incorrect
pytest cleans up the temporary directory automatically after the test.
Which pytest fixture is recommended for new file system tests?
✗ Incorrect
tmp_path is preferred over tmpdir for modern tests.Explain how to use the pytest
tmp_path fixture to create and write to a file in a test.Think about pathlib.Path usage and test isolation.
You got /4 concepts.
Why is using
tmp_path important for file system testing in pytest?Consider risks of testing with real files.
You got /4 concepts.