Why is it best to have only one assertion in a test function?
Think about how you find problems quickly when something breaks.
Having one assertion per test helps quickly identify what exactly failed. If multiple assertions are in one test, you might not know which part caused the failure without extra debugging.
What will pytest report when running this test?
def test_numbers(): assert 2 + 2 == 4 assert 3 * 3 == 10 assert 5 - 2 == 3
Remember pytest stops at the first failed assertion in a test.
The first assertion passes, but the second assertion (3 * 3 == 10) is false, so pytest stops and reports failure there.
Which assertion best follows the single responsibility principle for testing a function is_even(n) that returns True if n is even?
def is_even(n): return n % 2 == 0
Focus on testing one behavior per test.
Option A tests only one case (2 is even), keeping the test focused. Option A tests two cases in one assertion, which breaks single responsibility. Option A tests a different case and should be a separate test. Option A uses 'or' which can hide failures.
What is the main issue with this pytest test function?
def test_user_profile(): assert user.name == 'Alice' assert user.age == 30 assert user.email == 'alice@example.com'
Check if all variables used are defined or initialized.
The test uses 'user' without defining or initializing it, so it will raise a NameError before assertions run. Multiple assertions are allowed but setup is required.
Which pytest structure best supports single responsibility per test for a function add(a, b)?
def add(a, b): return a + b
Think about clarity and easy failure detection.
Separate test functions with one assertion each make it clear which input case fails. Multiple assertions or loops in one test hide which case caused failure. Testing only output type is insufficient.