0
0
PyTesttesting~20 mins

monkeypatch.chdir in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Monkeypatch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output after using monkeypatch.chdir?

Consider the following pytest test function that uses monkeypatch.chdir to change the current working directory.

What will be the printed output when this test runs?

PyTest
import os
import pytest

def test_change_dir(monkeypatch):
    original_dir = os.getcwd()
    monkeypatch.chdir("/tmp")
    current_dir = os.getcwd()
    print(current_dir)
    print(original_dir == current_dir)
A
Original directory path
True
B
/tmp
True
C
Original directory path
False
D
/tmp
False
Attempts:
2 left
💡 Hint

Think about what monkeypatch.chdir does to the current working directory during the test.

assertion
intermediate
1:30remaining
Which assertion correctly verifies directory change with monkeypatch.chdir?

You want to write a pytest test that confirms the current working directory changes to /var using monkeypatch.chdir. Which assertion is correct?

PyTest
import os
def test_dir_change(monkeypatch):
    monkeypatch.chdir("/var")
    # Which assertion below is correct?
Aassert os.getcwd() == "/tmp"
Bassert os.getcwd() != "/var"
Cassert os.getcwd() == "/var"
Dassert os.getcwd() is "/var"
Attempts:
2 left
💡 Hint

Use == to compare strings, not is.

🔧 Debug
advanced
2:00remaining
Why does this test fail when using monkeypatch.chdir?

Examine the test below. It is supposed to change the directory to /home/user and check it. However, it fails. What is the reason?

PyTest
import os
def test_fail_dir_change(monkeypatch):
    monkeypatch.chdir("/home/user")
    assert os.getcwd() == "/home/user"

# The test fails with AssertionError.
AThe directory /home/user does not exist in the test environment.
Bmonkeypatch.chdir does not actually change the directory.
CThe assertion syntax is incorrect.
Dos.getcwd() returns a relative path, not absolute.
Attempts:
2 left
💡 Hint

Check if the directory exists in the environment where the test runs.

framework
advanced
1:30remaining
How does monkeypatch.chdir affect tests in pytest?

Which statement best describes the behavior of monkeypatch.chdir in pytest tests?

AIt permanently changes the working directory for all tests in the session.
BIt changes the current working directory only for the duration of the test function and restores it afterward.
CIt changes the directory globally and does not restore it after the test.
DIt only simulates directory change without affecting os.getcwd().
Attempts:
2 left
💡 Hint

Think about test isolation and cleanup.

🧠 Conceptual
expert
2:30remaining
What is the main benefit of using monkeypatch.chdir in tests?

Why is it better to use monkeypatch.chdir instead of manually changing directories with os.chdir in pytest tests?

Amonkeypatch.chdir automatically restores the original directory after the test, preventing side effects.
Bmonkeypatch.chdir changes directories faster than os.chdir.
Cmonkeypatch.chdir changes the directory globally for all running processes.
Dmonkeypatch.chdir works outside of pytest tests as well.
Attempts:
2 left
💡 Hint

Consider test isolation and cleanup.