Test Overview
This test creates a temporary file using pytest's tmp_path fixture, writes text to it, reads the text back, and verifies the content matches what was written.
This test creates a temporary file using pytest's tmp_path fixture, writes text to it, reads the text back, and verifies the content matches what was written.
import pytest def test_write_and_read_file(tmp_path): # Create a temporary file path file = tmp_path / "testfile.txt" # Write text to the file file.write_text("Hello, pytest!") # Read text from the file content = file.read_text() # Verify the content is correct assert content == "Hello, pytest!"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initializes with tmp_path fixture ready | - | PASS |
| 2 | tmp_path fixture provides a temporary directory path | Temporary directory created in system temp location | - | PASS |
| 3 | Create a file path 'testfile.txt' inside tmp_path | File path object points to a new file in temp directory | - | PASS |
| 4 | Write text 'Hello, pytest!' to the file | File 'testfile.txt' contains the text 'Hello, pytest!' | - | PASS |
| 5 | Read text content from the file | Content read from 'testfile.txt' is 'Hello, pytest!' | - | PASS |
| 6 | Assert that read content equals 'Hello, pytest!' | Comparison between expected and actual content | content == 'Hello, pytest!' | PASS |
| 7 | Test ends successfully | Temporary files cleaned up automatically by pytest | - | PASS |