0
0
PyTesttesting~20 mins

tmp_path and tmp_path_factory in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 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.