A. The command 'false' returns a non-zero exit code, so assertion fails
B. Missing capture_output=True causes error
C. Using text=True is invalid here
D. subprocess.run requires shell=True for 'false'
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 A
Quick 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
A. assert result.returncode != 0 and 'error' in result.stderr
B. assert result.returncode == 0 and 'error' in result.stdout
C. assert result.returncode == 0 and 'error' in result.stderr
D. assert result.returncode != 0 and 'error' in result.stdout
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 A
Quick Check:
Failure means returncode != 0 and errors in stderr [OK]
Hint: Check returncode != 0 and error text in stderr [OK]