Subprocess testing helps check if programs run other programs correctly. It makes sure the commands and outputs are right.
Subprocess testing in PyTest
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PyTest
import subprocess result = subprocess.run(['command', 'arg1', 'arg2'], capture_output=True, text=True) print(result.stdout) print(result.returncode)
subprocess.run() runs a command and waits for it to finish.
capture_output=True captures the output so you can check it.
Examples
echo hello command and prints its output.PyTest
import subprocess result = subprocess.run(['echo', 'hello'], capture_output=True, text=True) print(result.stdout)
PyTest
import subprocess result = subprocess.run(['ls', '/nonexistent'], capture_output=True, text=True) print(result.stderr) print(result.returncode)
python --version.PyTest
import subprocess result = subprocess.run(['python', '--version'], capture_output=True, text=True) print(result.stdout.strip())
Sample Program
This test runs the echo hello command and checks if it returns 0 (success) and outputs 'hello'.
PyTest
import subprocess def test_echo_hello(): result = subprocess.run(['echo', 'hello'], capture_output=True, text=True) assert result.returncode == 0 assert result.stdout.strip() == 'hello' if __name__ == '__main__': test_echo_hello() print('Test passed')
Important Notes
Always use capture_output=True and text=True to get readable output.
Check returncode to know if the command succeeded (0 means success).
Use strip() to remove extra spaces or newlines from output before asserting.
Summary
Subprocess testing checks commands run by your program.
Use subprocess.run() with capture_output=True to get output.
Assert on stdout, stderr, and returncode to verify behavior.
Practice
1. What is the main purpose of subprocess testing in pytest?
easy
Solution
Step 1: Understand subprocess testing
Subprocess testing focuses on running external commands or programs from your code and checking their behavior.Step 2: Identify the correct purpose
Among the options, only checking commands run by your program matches subprocess testing.Final Answer:
To check commands run by your program and verify their output -> Option CQuick Check:
Subprocess testing = check commands run [OK]
Hint: Subprocess testing checks external commands run by your code [OK]
Common Mistakes:
- Confusing subprocess testing with UI testing
- Thinking it measures function speed
- Assuming it tests databases
2. Which of the following is the correct way to run a subprocess command in pytest and capture its output?
easy
Solution
Step 1: Recall subprocess.run syntax
The correct function is subprocess.run with a list of command arguments and capture_output=True to capture output.Step 2: Check options for correctness
Only subprocess.run(['ls'], capture_output=True, text=True) uses subprocess.run with correct parameters and argument format.Final Answer:
subprocess.run(['ls'], capture_output=True, text=True) -> Option BQuick Check:
Use subprocess.run with capture_output=True [OK]
Hint: Use subprocess.run([...], capture_output=True) to capture output [OK]
Common Mistakes:
- Using subprocess.execute which does not exist
- Passing command as string without shell=True
- Using wrong parameter names like capture or output
3. Given the following pytest test code, what will be the output of the assertion?
import subprocess
def test_echo():
result = subprocess.run(['echo', 'hello'], capture_output=True, text=True)
assert result.stdout == 'hello\n'
medium
Solution
Step 1: Understand subprocess.run output for echo
The echo command outputs the string followed by a newline, so stdout will be 'hello\n'.Step 2: Check the assertion
The assertion compares result.stdout to 'hello\n', which matches exactly, so it passes.Final Answer:
The test passes because stdout contains 'hello\n' -> Option DQuick Check:
echo adds newline, assertion matches [OK]
Hint: Remember echo adds newline to output [OK]
Common Mistakes:
- Forgetting echo adds a newline
- Expecting stdout without newline
- Confusing stdout with stderr
4. Identify the error in this pytest subprocess test code:
import subprocess
def test_fail():
result = subprocess.run(['false'], capture_output=True, text=True)
assert result.returncode == 0
medium
Solution
Step 1: Understand the 'false' command behavior
The 'false' command always returns exit code 1 (failure), not 0.Step 2: Check the assertion on returncode
The test asserts returncode == 0, which is false, so the assertion fails.Final Answer:
The command 'false' returns a non-zero exit code, so assertion fails -> Option AQuick Check:
'false' returns 1, assertion expects 0 [OK]
Hint: Check command return codes before asserting 0 [OK]
Common Mistakes:
- Assuming 'false' returns 0
- Thinking capture_output=True is mandatory for returncode
- Believing shell=True is needed for 'false'
5. You want to test a subprocess command that may output errors. Which pytest assertion correctly checks that the command failed and printed 'error' in stderr?
result = subprocess.run(['mycmd'], capture_output=True, text=True)
hard
Solution
Step 1: Understand failure and error output
A failed command has returncode not zero and error messages appear in stderr.Step 2: Match assertion to expected behavior
assert result.returncode != 0 and 'error' in result.stderr asserts returncode != 0 and 'error' in stderr, which correctly tests failure and error output.Final Answer:
assert result.returncode != 0 and 'error' in result.stderr -> Option AQuick Check:
Failure means returncode != 0 and errors in stderr [OK]
Hint: Check returncode != 0 and error text in stderr [OK]
Common Mistakes:
- Checking error in stdout instead of stderr
- Expecting returncode == 0 for failure
- Mixing stdout and stderr in assertions
