Bird
Raised Fist0
PyTesttesting~20 mins

tmp_path and tmp_path_factory in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
tmp_path Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of tmp_path usage in pytest
What is the output of this pytest test function when run?
PyTest
def test_tmp_path_usage(tmp_path):
    file = tmp_path / "testfile.txt"
    file.write_text("hello")
    content = file.read_text()
    assert content == "hello"
    print(len(list(tmp_path.iterdir())))
A0
B1
CSyntaxError
DTypeError
Attempts:
2 left
💡 Hint
tmp_path is a temporary directory unique to the test function.
❓ assertion
intermediate
1:30remaining
Correct assertion for tmp_path_factory usage
Which assertion correctly verifies that a directory created by tmp_path_factory exists?
PyTest
def test_tmp_path_factory(tmp_path_factory):
    new_dir = tmp_path_factory.mktemp("mydir")
Aassert new_dir.exists() and new_dir.is_dir()
Bassert new_dir.is_file()
Cassert not new_dir.exists()
Dassert new_dir == None
Attempts:
2 left
💡 Hint
mktemp creates a new temporary directory.
🔧 Debug
advanced
2:00remaining
Identify the error in tmp_path usage
What error will this pytest test raise?
PyTest
def test_write_file(tmp_path):
    file = tmp_path / 123
    file.write_text("data")
AFileNotFoundError
BAttributeError
CTypeError
DNo error, test passes
Attempts:
2 left
💡 Hint
Path objects expect string or Path-like objects for joining.
🧠 Conceptual
advanced
2:30remaining
Difference between tmp_path and tmp_path_factory
Which statement correctly describes the difference between tmp_path and tmp_path_factory in pytest?
Atmp_path and tmp_path_factory are identical and interchangeable
Btmp_path_factory is deprecated; tmp_path is the only recommended fixture
Ctmp_path creates temporary files; tmp_path_factory creates temporary databases
Dtmp_path provides a unique temporary directory per test function; tmp_path_factory can create multiple temporary directories shared across tests
Attempts:
2 left
💡 Hint
Think about scope and number of directories created.
❓ framework
expert
3:00remaining
Using tmp_path_factory for shared test resources
You want to create a temporary directory shared by multiple test functions in the same module using pytest fixtures. Which fixture setup correctly uses tmp_path_factory to achieve this?
PyTest
import pytest

@pytest.fixture(scope="module")
def shared_tmp_dir(tmp_path_factory):
    dir = tmp_path_factory.mktemp("shared")
    return dir

def test_one(shared_tmp_dir):
    file = shared_tmp_dir / "file1.txt"
    file.write_text("one")
    assert file.read_text() == "one"

def test_two(shared_tmp_dir):
    file = shared_tmp_dir / "file2.txt"
    file.write_text("two")
    assert file.read_text() == "two"
AThe fixture shared_tmp_dir creates a module-scoped temp directory using tmp_path_factory.mktemp, shared by tests
BThe fixture scope should be 'function' to share the directory
CThe fixture should use tmp_path instead of tmp_path_factory for sharing directories
Dtmp_path_factory cannot be used in fixtures
Attempts:
2 left
💡 Hint
Consider fixture scope and tmp_path_factory capabilities.

Practice

(1/5)
1. What is the main purpose of the tmp_path fixture in pytest?
easy
A. To provide a unique temporary directory for each test function
B. To create multiple temporary directories during a single test
C. To permanently store test files after tests finish
D. To mock file system calls without creating real files

Solution

  1. Step 1: Understand the role of tmp_path

    The tmp_path fixture gives each test a fresh temporary directory to use.
  2. Step 2: Compare with other options

    It does not create multiple directories per test (that's tmp_path_factory), nor does it store files permanently or mock file calls.
  3. Final Answer:

    To provide a unique temporary directory for each test function -> Option A
  4. Quick Check:

    tmp_path = unique temp folder per test [OK]
Hint: Remember: tmp_path = one temp folder per test [OK]
Common Mistakes:
  • Confusing tmp_path with tmp_path_factory
  • Thinking tmp_path stores files permanently
  • Assuming tmp_path mocks file system calls
2. Which of the following is the correct way to use tmp_path_factory to create a new temporary directory named 'data' inside a test?
easy
A. new_dir = tmp_path_factory.make_directory('data')
B. new_dir = tmp_path_factory.create_temp('data')
C. new_dir = tmp_path_factory.tempdir('data')
D. new_dir = tmp_path_factory.mktemp('data')

Solution

  1. Step 1: Recall the correct method name

    The method to create a new temporary directory with tmp_path_factory is mktemp().
  2. Step 2: Verify method usage

    Calling tmp_path_factory.mktemp('data') creates a temp directory named 'data'. Other options are not valid pytest methods.
  3. Final Answer:

    new_dir = tmp_path_factory.mktemp('data') -> Option D
  4. Quick Check:

    mktemp() creates temp dirs [OK]
Hint: Use mktemp() method to create temp dirs with tmp_path_factory [OK]
Common Mistakes:
  • 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
3. Given the following pytest test code, what will be the output of the assertion?
def test_create_file(tmp_path):
    file = tmp_path / 'test.txt'
    file.write_text('hello')
    content = file.read_text()
    assert content == 'hello'
medium
A. Test fails because read_text() returns bytes, not string
B. Test fails because the file is not created
C. Test passes because the file content matches 'hello'
D. Test raises an exception due to invalid file path

Solution

  1. 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.
  2. Step 2: Check reading and assertion

    Reading the file with read_text() returns the string 'hello', which matches the assertion.
  3. Final Answer:

    Test passes because the file content matches 'hello' -> Option C
  4. Quick Check:

    write_text/read_text with tmp_path works [OK]
Hint: write_text and read_text handle strings correctly [OK]
Common Mistakes:
  • Assuming read_text returns bytes instead of string
  • Thinking file is not created automatically
  • Expecting exceptions from valid path operations
4. Identify the error in this pytest test using 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
medium
A. Error because mktemp cannot create directories with the same name
B. No error; test passes because mktemp creates unique dirs
C. Error because tmp_path_factory is not a valid fixture
D. Error because assert compares directory contents, not paths

Solution

  1. Step 1: Understand mktemp behavior

    Each call to mktemp('dir') creates a unique temporary directory, even if the name is the same.
  2. Step 2: Check assertion validity

    The assertion compares the two directory paths and expects them to be different, which is true.
  3. Final Answer:

    No error; test passes because mktemp creates unique dirs -> Option B
  4. Quick Check:

    mktemp creates unique dirs even with same name [OK]
Hint: mktemp always creates unique dirs, names can repeat [OK]
Common Mistakes:
  • Thinking mktemp fails on duplicate names
  • Confusing fixture names or usage
  • Assuming assert compares contents, not paths
5. You want to write a pytest test that creates two temporary directories: one shared across multiple tests and one unique per test. Which combination of fixtures should you use?
hard
A. Use tmp_path_factory to create the shared directory and tmp_path for the unique per-test directory
B. Use tmp_path for both shared and unique directories
C. Use tmp_path_factory for both shared and unique directories
D. Use tmp_path to create the shared directory and tmp_path_factory for unique directories

Solution

  1. Step 1: Identify fixture roles

    tmp_path provides a fresh temp directory unique to each test. tmp_path_factory can create directories shared or reused across tests.
  2. Step 2: Match requirements to fixtures

    For a shared directory across tests, use tmp_path_factory. For unique per-test directories, use tmp_path.
  3. Final Answer:

    Use tmp_path_factory to create the shared directory and tmp_path for the unique per-test directory -> Option A
  4. Quick Check:

    tmp_path unique per test, tmp_path_factory for shared [OK]
Hint: tmp_path unique per test; tmp_path_factory for shared dirs [OK]
Common Mistakes:
  • Using tmp_path for shared directories
  • Using tmp_path_factory only for unique per-test dirs
  • Mixing up fixture purposes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard