0
0
PyTesttesting~5 mins

Subprocess testing in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is subprocess testing in software testing?
Subprocess testing checks how a program runs other programs or commands outside itself. It ensures these external calls work correctly.
Click to reveal answer
beginner
Which Python module is commonly used to run subprocesses in tests?
The subprocess module lets Python code start and control other programs. It is often used in subprocess testing.
Click to reveal answer
intermediate
How can you check the output of a subprocess in pytest?
Use subprocess.run() with capture_output=True to get the output. Then assert the output matches expected results.
Click to reveal answer
intermediate
Why should subprocess calls be mocked in unit tests?
Mocking subprocess calls avoids running real commands, which can be slow or unsafe. It helps tests run fast and reliably.
Click to reveal answer
advanced
What is a good practice for locating subprocess commands in tests?
Use absolute paths or environment variables to locate commands. This avoids errors from missing or wrong commands during tests.
Click to reveal answer
Which function runs a subprocess and waits for it to finish in Python?
Asubprocess.execute()
Bsubprocess.start()
Csubprocess.run()
Dsubprocess.call_async()
In pytest, how do you capture the output of a subprocess?
AUse <code>capture_output=True</code> in <code>subprocess.run()</code>
BUse <code>print()</code> inside the subprocess
CUse <code>subprocess.output()</code>
DUse <code>subprocess.capture()</code>
Why mock subprocess calls in unit tests?
ATo skip testing subprocess behavior
BTo avoid running real commands that may be slow or unsafe
CTo make tests run slower
DTo run commands twice
What does subprocess.CompletedProcess contain?
AOnly the process ID
BThe environment variables
CThe source code of the subprocess
DReturn code, stdout, and stderr of the subprocess
Which is a best practice for subprocess command paths in tests?
AUse absolute paths or environment variables
BUse relative paths without checking
CHardcode commands without paths
DIgnore command locations
Explain how to test a subprocess call using pytest, including capturing output and making assertions.
Think about how to run the command and check what it prints or returns.
You got /4 concepts.
    Describe why and how you would mock subprocess calls in unit tests.
    Consider the risks of running real commands during tests.
    You got /4 concepts.