Test Overview
This test uses monkeypatch.chdir to change the current working directory temporarily. It verifies that the directory changes inside the test and reverts after the test ends.
This test uses monkeypatch.chdir to change the current working directory temporarily. It verifies that the directory changes inside the test and reverts after the test ends.
import os import pytest def test_change_directory(monkeypatch): original_dir = os.getcwd() new_dir = "/tmp" monkeypatch.chdir(new_dir) current_dir = os.getcwd() assert current_dir == new_dir # After test, directory should revert automatically
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test environment ready, current directory is original_dir | - | PASS |
| 2 | monkeypatch.chdir("/tmp") is called to change directory | Current working directory is changed to /tmp temporarily | - | PASS |
| 3 | os.getcwd() is called to get current directory | Current directory is /tmp | assert current_dir == new_dir | PASS |
| 4 | Test ends, monkeypatch reverts directory to original_dir | Current directory restored to original_dir | - | PASS |