Test Overview
This test uses pytest's tmp_path fixture to create a temporary directory. It verifies that a file can be created and read inside this temporary directory.
This test uses pytest's tmp_path fixture to create a temporary directory. It verifies that a file can be created and read inside this temporary directory.
import pytest def test_create_and_read_file(tmp_path): # Create a file path inside the temporary directory file_path = tmp_path / "testfile.txt" # Write text to the file file_path.write_text("Hello, pytest!") # Read the text back content = file_path.read_text() # Assert the content is as expected assert content == "Hello, pytest!"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | - | PASS |
| 2 | pytest provides tmp_path fixture as a temporary directory | A new empty temporary directory is created for the test | - | PASS |
| 3 | Create a file path 'testfile.txt' inside tmp_path | Temporary directory is empty, file path is prepared | - | PASS |
| 4 | Write text 'Hello, pytest!' to the file | File 'testfile.txt' now exists with content 'Hello, pytest!' | - | PASS |
| 5 | Read text content from the file | File content read as 'Hello, pytest!' | - | PASS |
| 6 | Assert that the read content equals 'Hello, pytest!' | Content matches expected string | assert content == 'Hello, pytest!' | PASS |
| 7 | Test ends and temporary directory is cleaned up | Temporary directory and files removed | - | PASS |