Bird
Raised Fist0
PyTesttesting~20 mins

Subprocess testing in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Subprocess Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of subprocess.run with capture
What is the output of this pytest test when running the subprocess call?
PyTest
import subprocess

def test_echo():
    result = subprocess.run(['echo', 'hello'], capture_output=True, text=True)
    assert result.stdout == 'hello\n'
    print(result.stdout)
Ahello
Bhello\n
C['hello']
DNone
Attempts:
2 left
💡 Hint
Remember that echo adds a newline and capture_output=True captures stdout as bytes or text.
assertion
intermediate
2:00remaining
Correct assertion for subprocess error
Which assertion correctly checks that a subprocess call failed with exit code 1?
PyTest
import subprocess

def test_fail():
    result = subprocess.run(['false'])
    # Which assertion is correct here?
Aassert result.returncode == 1
Bassert result.returncode == 0
Cassert result.stdout == 'error'
Dassert result.stderr == 'failed'
Attempts:
2 left
💡 Hint
The 'false' command always exits with code 1 and produces no output.
🔧 Debug
advanced
2:00remaining
Identify the bug in subprocess test
Why does this pytest test fail unexpectedly?
PyTest
import subprocess

def test_ls():
    result = subprocess.run(['ls', '-l'], capture_output=True)
    assert b'README.md' in result.stdout
AThe 'ls' command is not available on Windows
Bcapture_output=True disables stdout
Cresult.stdout is bytes, so 'README.md' in bytes fails
DThe assertion syntax is invalid
Attempts:
2 left
💡 Hint
Check the type of result.stdout when capture_output=True is used without text=True.
framework
advanced
2:00remaining
Best pytest fixture for subprocess cleanup
Which pytest fixture is best to ensure subprocesses are terminated after tests?
A
@pytest.fixture(autouse=True)
def cleanup_subprocess():
    yield
    # terminate subprocesses here
B
@pytest.fixture(scope='session')
def cleanup_subprocess():
    # terminate subprocesses here
C
@pytest.fixture(scope='module')
def cleanup_subprocess():
    yield
    # terminate subprocesses here
D
@pytest.fixture(scope='function')
def cleanup_subprocess():
    yield
    # terminate subprocesses here
Attempts:
2 left
💡 Hint
Subprocesses should be cleaned after each test function to avoid interference.
🧠 Conceptual
expert
2:00remaining
Why use subprocess.check_output in tests?
What is the main advantage of using subprocess.check_output over subprocess.run in pytest tests?
Acheck_output raises an exception if the command fails, simplifying error detection
Bcheck_output automatically retries failed commands
Ccheck_output runs commands asynchronously
Dcheck_output captures stderr by default, unlike run
Attempts:
2 left
💡 Hint
Think about how exceptions help in test failures.

Practice

(1/5)
1. What is the main purpose of subprocess testing in pytest?
easy
A. To measure the speed of your Python functions
B. To test user interface elements like buttons and forms
C. To check commands run by your program and verify their output
D. To check database connections and queries

Solution

  1. Step 1: Understand subprocess testing

    Subprocess testing focuses on running external commands or programs from your code and checking their behavior.
  2. Step 2: Identify the correct purpose

    Among the options, only checking commands run by your program matches subprocess testing.
  3. Final Answer:

    To check commands run by your program and verify their output -> Option C
  4. Quick 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
A. subprocess.execute('ls', capture=True)
B. subprocess.run(['ls'], capture_output=True, text=True)
C. subprocess.call('ls', output=True)
D. subprocess.run('ls', capture=True)

Solution

  1. 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.
  2. Step 2: Check options for correctness

    Only subprocess.run(['ls'], capture_output=True, text=True) uses subprocess.run with correct parameters and argument format.
  3. Final Answer:

    subprocess.run(['ls'], capture_output=True, text=True) -> Option B
  4. Quick 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
A. The test raises a runtime error
B. The test fails because stdout is empty
C. The test fails because stdout contains 'hello' without newline
D. The test passes because stdout contains 'hello\n'

Solution

  1. Step 1: Understand subprocess.run output for echo

    The echo command outputs the string followed by a newline, so stdout will be 'hello\n'.
  2. Step 2: Check the assertion

    The assertion compares result.stdout to 'hello\n', which matches exactly, so it passes.
  3. Final Answer:

    The test passes because stdout contains 'hello\n' -> Option D
  4. Quick 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
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

  1. Step 1: Understand the 'false' command behavior

    The 'false' command always returns exit code 1 (failure), not 0.
  2. Step 2: Check the assertion on returncode

    The test asserts returncode == 0, which is false, so the assertion fails.
  3. Final Answer:

    The command 'false' returns a non-zero exit code, so assertion fails -> Option A
  4. 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

  1. Step 1: Understand failure and error output

    A failed command has returncode not zero and error messages appear in stderr.
  2. 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.
  3. Final Answer:

    assert result.returncode != 0 and 'error' in result.stderr -> Option A
  4. Quick 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