0
0
PyTesttesting~20 mins

Single responsibility per test in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Single Responsibility Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why keep one assertion per test?

Why is it best to have only one assertion in a test function?

AIt makes it easier to find the exact cause when a test fails.
BIt reduces the total number of tests needed.
CIt allows multiple features to be tested together efficiently.
DIt speeds up test execution by combining checks.
Attempts:
2 left
💡 Hint

Think about how you find problems quickly when something breaks.

Predict Output
intermediate
2:00remaining
Output of pytest with multiple assertions

What will pytest report when running this test?

PyTest
def test_numbers():
    assert 2 + 2 == 4
    assert 3 * 3 == 10
    assert 5 - 2 == 3
ATest fails at first assertion with AssertionError.
BTest passes because only one assertion needs to be true.
CTest fails at second assertion with AssertionError.
DTest fails at third assertion with AssertionError.
Attempts:
2 left
💡 Hint

Remember pytest stops at the first failed assertion in a test.

assertion
advanced
1:30remaining
Choose the best single responsibility test assertion

Which assertion best follows the single responsibility principle for testing a function is_even(n) that returns True if n is even?

PyTest
def is_even(n):
    return n % 2 == 0
Aassert is_even(2) is True
Bassert is_even(2) is True and is_even(3) is False
Cassert is_even(3) is False
Dassert is_even(2) == True or is_even(3) == False
Attempts:
2 left
💡 Hint

Focus on testing one behavior per test.

🔧 Debug
advanced
2:00remaining
Identify the problem with this test function

What is the main issue with this pytest test function?

PyTest
def test_user_profile():
    assert user.name == 'Alice'
    assert user.age == 30
    assert user.email == 'alice@example.com'
AThe test has multiple assertions, making it hard to know which failed.
BThe assertions use wrong syntax for equality check.
CThe test function name does not start with 'test_'.
DThe test is missing a setup for the user object.
Attempts:
2 left
💡 Hint

Check if all variables used are defined or initialized.

framework
expert
2:30remaining
Best pytest structure for single responsibility tests

Which pytest structure best supports single responsibility per test for a function add(a, b)?

PyTest
def add(a, b):
    return a + b
AOne test function with multiple assertions for all input cases.
BSeparate test functions each with one assertion for different input cases.
COne test function with a loop and assertions inside for all cases.
DOne test function that asserts the output type only.
Attempts:
2 left
💡 Hint

Think about clarity and easy failure detection.