Test Overview
This test runs a subprocess command using Python's subprocess module and verifies that the command executes successfully and returns the expected output.
This test runs a subprocess command using Python's subprocess module and verifies that the command executes successfully and returns the expected output.
import subprocess import pytest def test_echo_subprocess(): # Run the 'echo' command to print 'hello' result = subprocess.run(['echo', 'hello'], capture_output=True, text=True) # Check that the command exited with code 0 (success) assert result.returncode == 0 # Check that the output is 'hello' followed by a newline assert result.stdout == 'hello\n'
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner is ready to execute the test function | - | PASS |
| 2 | Calls subprocess.run(['echo', 'hello'], capture_output=True, text=True) | Subprocess executes the 'echo' command in a new shell process | - | PASS |
| 3 | Subprocess completes and returns result object with returncode and stdout | Subprocess finished successfully, output is 'hello\n' | Check result.returncode == 0 | PASS |
| 4 | Assert that result.stdout == 'hello\n' | Output from subprocess is captured as 'hello\n' | Check output matches expected string | PASS |
| 5 | Test ends with all assertions passing | Test runner reports test passed | - | PASS |