Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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?
AA pathlib.Path object
BA string path
CA py.path.local object
DA file descriptor integer
✗ 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?
AIt automatically uploads files to cloud storage
BIt permanently saves test files for later use
CIt speeds up test execution by caching files
DIt isolates test files from the real file system
✗ 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?
A(tmp_path / 'greet.txt').write_text('hello')
Btmp_path.write_text('hello')
Ctmp_path / 'greet.txt'.write_text('hello')
Dwrite_text(tmp_path, 'greet.txt', 'hello')
✗ 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?
AIt remains on disk for debugging
BIt is archived as a zip file
CIt is deleted automatically
DIt is moved to a backup folder
✗ Incorrect
pytest cleans up the temporary directory automatically after the test.
Which pytest fixture is recommended for new file system tests?
Atmpdir
Btmp_path
Ctmpfile
Dtempfile
✗ 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.
Practice
(1/5)
1. What is the main purpose of using tmp_path in pytest tests?
easy
A. To mock file system calls without creating real files
B. To permanently store test files for later use
C. To speed up test execution by caching files
D. To create a temporary directory unique to each test for safe file operations
Solution
Step 1: Understand tmp_path functionality
tmp_path provides a temporary directory unique to each test run.
Step 2: Identify its purpose in tests
This temporary directory allows safe creation, reading, and deletion of files without affecting the real file system.
Final Answer:
To create a temporary directory unique to each test for safe file operations -> Option D
Quick Check:
tmp_path = safe temp folder [OK]
Hint: Remember tmp_path is a safe temp folder for each test [OK]
Common Mistakes:
Thinking tmp_path stores files permanently
Confusing tmp_path with mocking file calls
Assuming tmp_path speeds up tests by caching
2. Which of the following is the correct way to create a new file named test.txt inside the tmp_path directory in pytest?
easy
A. (tmp_path / 'test.txt').write_text('Hello')
B. tmp_path / 'test.txt'.write_text('Hello')
C. tmp_path.write_text('test.txt', 'Hello')
D. tmp_path.write('test.txt', 'Hello')
Solution
Step 1: Understand tmp_path usage
tmp_path is a pathlib.Path object, so to create a file, you combine paths with / operator.
Step 2: Correct syntax for writing text
Use parentheses to combine path and then call write_text() on the file path: (tmp_path / 'test.txt').write_text('Hello').
Final Answer:
(tmp_path / 'test.txt').write_text('Hello') -> Option A
Quick Check:
Use (tmp_path / filename).write_text() [OK]
Hint: Use parentheses to combine path before write_text() [OK]
Common Mistakes:
Calling write_text directly on tmp_path
Missing parentheses around path combination
Using incorrect method names like write()
3. Given the following pytest test code using tmp_path:
A. Missing parentheses around tmp_path / 'log.txt'
B. The assertion compares bytes to string causing failure
C. Using write() instead of write_text() causes an AttributeError
D. The test will pass without errors
Solution
Step 1: Check method used to write file
Path objects do not have a write() method; the correct method to write text is write_text().
Step 2: Understand error caused
Calling write() will raise an AttributeError because it does not exist on the Path object.
Final Answer:
Using write() instead of write_text() causes an AttributeError -> Option C
Quick Check:
Use write_text() to write strings to files [OK]
Hint: Use write_text() for writing strings to files [OK]
Common Mistakes:
Confusing write() with write_text()
Ignoring method errors and expecting test pass
Assuming parentheses are missing around path
5. You want to test a function that creates multiple files inside a directory using pytest's tmp_path. Which approach correctly verifies that exactly three files named a.txt, b.txt, and c.txt exist in the temporary directory after the function runs?
hard
A. Use assert tmp_path.read_text() == 'a.txtb.txtc.txt'
B. Use assert set(f.name for f in tmp_path.iterdir()) == {'a.txt', 'b.txt', 'c.txt'}
C. Use assert tmp_path.glob('*.txt') == ['a.txt', 'b.txt', 'c.txt']
D. Use assert tmp_path.exists('a.txt') and tmp_path.exists('b.txt') and tmp_path.exists('c.txt')
Solution
Step 1: Understand how to list files in tmp_path
tmp_path.iterdir() returns an iterator of Path objects for all entries in the directory.
Step 2: Verify file names correctly
Extract file names with f.name and compare as a set to ensure exactly the three expected files exist.
Final Answer:
Use assert set(f.name for f in tmp_path.iterdir()) == {'a.txt', 'b.txt', 'c.txt'} -> Option B
Quick Check:
Use iterdir() and set comparison for exact files [OK]
Hint: Use set of filenames from iterdir() to check exact files [OK]