Bird
Raised Fist0
PyTesttesting~10 mins

Temporary directory management in PyTest - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use pytest's temporary directory fixture.

PyTest
def test_create_file([1]):
    file_path = tmp_path / "test.txt"
    file_path.write_text("hello")
    assert file_path.read_text() == "hello"
Drag options to blanks, or click blank then click option'
Atmp_path
Btempfile
Ctemp_dir
Dtmpdir
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tmpdir' which is a different fixture returning py.path.local object.
Using undefined fixture names like 'temp_dir' or 'tempfile'.
2fill in blank
medium

Complete the code to create a file inside the pytest temporary directory.

PyTest
def test_write_file(tmp_path):
    file = tmp_path / [1]
    file.write_text("data")
    assert file.read_text() == "data"
Drag options to blanks, or click blank then click option'
Aoutput.txt
B"output.txt"
C'output.txt'
Doutput
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the file name causing syntax errors.
Using a variable name without quotes.
3fill in blank
hard

Fix the error in the test that tries to check if a directory exists in the pytest temp path.

PyTest
def test_dir_exists(tmp_path):
    dir_path = tmp_path / "subdir"
    dir_path.mkdir()
    assert dir_path.[1]()
Drag options to blanks, or click blank then click option'
Aisfile
Bexists
Cis_dir
Disdir
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exists()' which returns True for files and directories.
Using 'isdir()' which is not a valid pathlib method.
Using 'isfile()' which checks for files, not directories.
4fill in blank
hard

Fill both blanks to create a file and check its size in the pytest temporary directory.

PyTest
def test_file_size(tmp_path):
    file = tmp_path / [1]
    file.write_text("abc")
    size = file.[2]
    assert size == 3
Drag options to blanks, or click blank then click option'
A"data.txt"
Bstat
Cstat().st_size
Dstat().st_size()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'stat' which returns the stat object, not the size.
Using 'stat().st_size()' which is invalid because 'st_size' is an attribute, not a method.
Not quoting the file name.
Trying to get size directly without 'stat()'.
5fill in blank
hard

Fill all three blanks to create a file, write text, and verify the file exists in pytest temp directory.

PyTest
def test_file_creation(tmp_path):
    file = tmp_path / [1]
    file.[2]("hello world")
    assert file.[3]()
Drag options to blanks, or click blank then click option'
A"hello.txt"
Bwrite_text
Cexists
Dread_text
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'read_text()' instead of 'exists()' in the assertion.
Not quoting the file name.
Using wrong method names.

Practice

(1/5)
1. What is the main purpose of using the tmp_path fixture in pytest?
easy
A. To create a temporary directory for test files that is cleaned up automatically
B. To permanently store test results on disk
C. To speed up test execution by caching files
D. To replace the need for writing assertions in tests

Solution

  1. Step 1: Understand the role of tmp_path in pytest

    The tmp_path fixture provides a temporary directory unique to each test run.
  2. Step 2: Recognize the cleanup behavior

    Files created in this directory are removed after the test finishes, keeping tests clean and isolated.
  3. Final Answer:

    To create a temporary directory for test files that is cleaned up automatically -> Option A
  4. Quick Check:

    Temporary directory = safe test files [OK]
Hint: Remember tmp_path auto-cleans test files after each test [OK]
Common Mistakes:
  • Thinking tmp_path stores files permanently
  • Confusing tmp_path with caching mechanisms
  • Believing tmp_path replaces assertions
2. Which of the following is the correct way to use the tmp_path fixture in a pytest test function?
easy
A. def test_example():\n file = tmp_path / 'test.txt'\n file.write_text('hello')
B. def test_example(tmp_path):\n file = tmp_path / 'test.txt'\n file.write_text('hello')
C. def test_example(tmp_path):\n file = open('test.txt', 'w')\n file.write('hello')
D. def test_example():\n tmp_path = '/tmp/test'\n file = tmp_path + '/test.txt'\n write(file, 'hello')

Solution

  1. Step 1: Identify correct fixture usage

    pytest injects tmp_path as a function argument automatically.
  2. Step 2: Check file creation syntax

    Using tmp_path / 'filename' creates a Path object; write_text() writes content correctly.
  3. Final Answer:

    def test_example(tmp_path):\n file = tmp_path / 'test.txt'\n file.write_text('hello') -> Option B
  4. Quick Check:

    Fixture as argument + Path write = correct [OK]
Hint: Always accept tmp_path as a test function argument [OK]
Common Mistakes:
  • Assigning tmp_path manually instead of using fixture
  • Using open() without tmp_path directory
  • Not passing tmp_path as a parameter
3. What will be the output of the following pytest test code snippet?
def test_write_read(tmp_path):
    file = tmp_path / 'data.txt'
    file.write_text('pytest')
    content = file.read_text()
    assert content == 'pytest'
medium
A. Runtime error because file is not created
B. Test fails due to assertion error
C. Syntax error in test code
D. Test passes successfully

Solution

  1. Step 1: Analyze file writing and reading

    The code writes 'pytest' to a file in tmp_path, then reads it back correctly.
  2. Step 2: Check assertion correctness

    The read content matches the written string 'pytest', so assertion passes.
  3. Final Answer:

    Test passes successfully -> Option D
  4. Quick Check:

    Write then read same text = pass [OK]
Hint: Write and read text in tmp_path matches, so assert passes [OK]
Common Mistakes:
  • Assuming file is not created automatically
  • Confusing assertion logic
  • Thinking tmp_path is a string path, not Path object
4. Identify the error in this pytest test using tmp_path:
def test_error(tmp_path):
    file = tmp_path + '/file.txt'
    file.write_text('error')
medium
A. File write_text method does not exist
B. Missing assertion statement causes test failure
C. Using + operator to join paths causes a TypeError
D. tmp_path fixture is not passed as argument

Solution

  1. Step 1: Check path joining method

    tmp_path is a Path object; using + with string causes TypeError.
  2. Step 2: Correct path joining

    Use tmp_path / 'file.txt' to join paths properly.
  3. Final Answer:

    Using + operator to join paths causes a TypeError -> Option C
  4. Quick Check:

    Path + string = error; use / operator [OK]
Hint: Join paths with / operator, not + [OK]
Common Mistakes:
  • Using + to join Path and string
  • Forgetting to pass tmp_path as parameter
  • Assuming write_text is invalid method
5. You want to create a nested directory structure inside tmp_path and write a file in the deepest folder. Which code snippet correctly does this in pytest?
hard
A. def test_nested(tmp_path):\n nested_dir = tmp_path / 'a' / 'b' / 'c'\n nested_dir.mkdir(parents=True)\n file = nested_dir / 'file.txt'\n file.write_text('nested')
B. def test_nested(tmp_path):\n nested_dir = tmp_path / 'a' / 'b' / 'c'\n nested_dir.mkdir()\n file = nested_dir / 'file.txt'\n file.write_text('nested')
C. def test_nested(tmp_path):\n nested_dir = tmp_path / 'a' / 'b' / 'c'\n file = nested_dir / 'file.txt'\n file.write_text('nested')
D. def test_nested(tmp_path):\n nested_dir = tmp_path + '/a/b/c'\n nested_dir.mkdir(parents=True)\n file = nested_dir / 'file.txt'\n file.write_text('nested')

Solution

  1. Step 1: Understand nested directory creation

    To create nested folders, mkdir(parents=True) is needed to create all missing parents.
  2. Step 2: Check path joining and file writing

    Using / operator joins paths correctly; writing text to file is valid.
  3. Step 3: Identify errors in other options

    def test_nested(tmp_path):\n nested_dir = tmp_path / 'a' / 'b' / 'c'\n nested_dir.mkdir()\n file = nested_dir / 'file.txt'\n file.write_text('nested') misses parents=True, so mkdir fails if parents missing. def test_nested(tmp_path):\n nested_dir = tmp_path / 'a' / 'b' / 'c'\n file = nested_dir / 'file.txt'\n file.write_text('nested') does not create directories before writing file. def test_nested(tmp_path):\n nested_dir = tmp_path + '/a/b/c'\n nested_dir.mkdir(parents=True)\n file = nested_dir / 'file.txt'\n file.write_text('nested') uses + operator incorrectly for paths.
  4. Final Answer:

    def test_nested(tmp_path):\n nested_dir = tmp_path / 'a' / 'b' / 'c'\n nested_dir.mkdir(parents=True)\n file = nested_dir / 'file.txt'\n file.write_text('nested') -> Option A
  5. Quick Check:

    mkdir(parents=True) + / operator = correct [OK]
Hint: Use mkdir(parents=True) to create nested folders [OK]
Common Mistakes:
  • Not using parents=True for nested mkdir
  • Using + operator to join paths
  • Writing file before creating directories
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