0
0
PyTesttesting~5 mins

monkeypatch.chdir in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does monkeypatch.chdir do in pytest?

monkeypatch.chdir temporarily changes the current working directory during a test. After the test, it restores the original directory automatically.

Click to reveal answer
beginner
Why use monkeypatch.chdir instead of os.chdir directly in tests?

Using monkeypatch.chdir ensures the directory change is temporary and automatically reversed after the test. This avoids side effects on other tests.

Click to reveal answer
beginner
How do you use monkeypatch.chdir in a pytest test function?

Pass monkeypatch as a test argument, then call monkeypatch.chdir("path/to/dir") inside the test to change directory temporarily.

Click to reveal answer
intermediate
What happens if you call monkeypatch.chdir with a non-existent directory?

It raises a FileNotFoundError immediately, just like os.chdir. The test will fail unless handled.

Click to reveal answer
intermediate
Can monkeypatch.chdir be used multiple times in the same test?

Yes, you can call it multiple times to change directories during a test. Each call changes the directory temporarily until the test ends.

Click to reveal answer
What is the main benefit of using monkeypatch.chdir in pytest tests?
AIt permanently changes the working directory.
BIt creates a new directory.
CIt deletes the current directory.
DIt temporarily changes the working directory and restores it after the test.
What happens if monkeypatch.chdir is called with a path that does not exist?
AIt raises a <code>FileNotFoundError</code>.
BIt creates the directory automatically.
CIt changes to the root directory.
DIt silently ignores the error.
How do you get access to monkeypatch in a pytest test function?
AAdd it as a parameter to the test function.
BImport it from pytest manually.
CCall a global function.
DUse a decorator.
Which of these is a correct use of monkeypatch.chdir?
Amonkeypatch.chdir()
Bmonkeypatch.chdir('/tmp')
Cmonkeypatch.chdir(123)
Dmonkeypatch.chdir(None)
After a test using monkeypatch.chdir finishes, what is the current working directory?
AThe home directory.
BThe directory set by monkeypatch.
CThe original directory before the test.
DThe root directory.
Explain how monkeypatch.chdir helps keep tests isolated and why that is important.
Think about what happens if tests change directories without restoring.
You got /4 concepts.
    Describe the steps to use monkeypatch.chdir in a pytest test function.
    Focus on how pytest provides monkeypatch and how you call the method.
    You got /4 concepts.