We use tmp_path and tmp_path_factory to create temporary folders during tests. This helps keep tests clean and avoids messing with real files.
tmp_path and tmp_path_factory in PyTest
def test_example(tmp_path): # tmp_path is a pathlib.Path object for a temp directory file = tmp_path / "test.txt" file.write_text("hello") def test_factory(tmp_path_factory): # tmp_path_factory creates multiple temp directories dir1 = tmp_path_factory.mktemp("data1") dir2 = tmp_path_factory.mktemp("data2")
tmp_path gives a unique temporary directory for each test function.
tmp_path_factory lets you create multiple temporary directories if needed.
def test_write_file(tmp_path): file = tmp_path / "file.txt" file.write_text("hello world") assert file.read_text() == "hello world"
def test_multiple_dirs(tmp_path_factory): dir_a = tmp_path_factory.mktemp("a") dir_b = tmp_path_factory.mktemp("b") (dir_a / "a.txt").write_text("A") (dir_b / "b.txt").write_text("B") assert (dir_a / "a.txt").read_text() == "A" assert (dir_b / "b.txt").read_text() == "B"
This script has two tests. The first uses tmp_path to create and check a file. The second uses tmp_path_factory to create two folders and files inside them, then checks their contents.
import pytest def test_tmp_path_creates_file(tmp_path): file_path = tmp_path / "sample.txt" file_path.write_text("pytest temp file") content = file_path.read_text() assert content == "pytest temp file" def test_tmp_path_factory_creates_dirs(tmp_path_factory): dir1 = tmp_path_factory.mktemp("dir1") dir2 = tmp_path_factory.mktemp("dir2") (dir1 / "file1.txt").write_text("data1") (dir2 / "file2.txt").write_text("data2") assert (dir1 / "file1.txt").read_text() == "data1" assert (dir2 / "file2.txt").read_text() == "data2"
Temporary folders are deleted automatically after tests finish.
Use tmp_path for simple cases needing one temp folder per test.
Use tmp_path_factory when you need multiple temp folders in one test.
tmp_path gives a fresh temp folder for each test.
tmp_path_factory lets you create many temp folders as needed.
Both help keep tests clean and avoid changing real files.
Practice
tmp_path fixture in pytest?Solution
Step 1: Understand the role of
Thetmp_pathtmp_pathfixture gives each test a fresh temporary directory to use.Step 2: Compare with other options
It does not create multiple directories per test (that'stmp_path_factory), nor does it store files permanently or mock file calls.Final Answer:
To provide a unique temporary directory for each test function -> Option AQuick Check:
tmp_path= unique temp folder per test [OK]
- Confusing tmp_path with tmp_path_factory
- Thinking tmp_path stores files permanently
- Assuming tmp_path mocks file system calls
tmp_path_factory to create a new temporary directory named 'data' inside a test?Solution
Step 1: Recall the correct method name
The method to create a new temporary directory withtmp_path_factoryismktemp().Step 2: Verify method usage
Callingtmp_path_factory.mktemp('data')creates a temp directory named 'data'. Other options are not valid pytest methods.Final Answer:
new_dir = tmp_path_factory.mktemp('data') -> Option DQuick Check:
mktemp()creates temp dirs [OK]
- Using incorrect method names like create_temp or tempdir
- Trying to call make_directory which doesn't exist
- Confusing tmp_path and tmp_path_factory usage
def test_create_file(tmp_path):
file = tmp_path / 'test.txt'
file.write_text('hello')
content = file.read_text()
assert content == 'hello'
Solution
Step 1: Analyze file creation and writing
The code creates a file 'test.txt' in the temporary directory and writes the string 'hello' to it.Step 2: Check reading and assertion
Reading the file withread_text()returns the string 'hello', which matches the assertion.Final Answer:
Test passes because the file content matches 'hello' -> Option CQuick Check:
write_text/read_text with tmp_path works [OK]
- Assuming read_text returns bytes instead of string
- Thinking file is not created automatically
- Expecting exceptions from valid path operations
tmp_path_factory:
def test_dir_creation(tmp_path_factory):
dir1 = tmp_path_factory.mktemp('dir')
dir2 = tmp_path_factory.mktemp('dir')
assert dir1 != dir2
Solution
Step 1: Understand mktemp behavior
Each call tomktemp('dir')creates a unique temporary directory, even if the name is the same.Step 2: Check assertion validity
The assertion compares the two directory paths and expects them to be different, which is true.Final Answer:
No error; test passes because mktemp creates unique dirs -> Option BQuick Check:
mktemp creates unique dirs even with same name [OK]
- Thinking mktemp fails on duplicate names
- Confusing fixture names or usage
- Assuming assert compares contents, not paths
Solution
Step 1: Identify fixture roles
tmp_pathprovides a fresh temp directory unique to each test.tmp_path_factorycan create directories shared or reused across tests.Step 2: Match requirements to fixtures
For a shared directory across tests, usetmp_path_factory. For unique per-test directories, usetmp_path.Final Answer:
Use tmp_path_factory to create the shared directory and tmp_path for the unique per-test directory -> Option AQuick Check:
tmp_path unique per test, tmp_path_factory for shared [OK]
- Using tmp_path for shared directories
- Using tmp_path_factory only for unique per-test dirs
- Mixing up fixture purposes
Ready to prove it?
Pass the test and this pattern moves unknown
