Bird
Raised Fist0
PyTesttesting~15 mins

Assert with messages in PyTest - Build an Automation Script

Choose your learning style10 modes available
Verify sum of two numbers with assertion message
Preconditions (2)
Step 1: Write a function that adds two numbers
Step 2: Call the function with inputs 2 and 3
Step 3: Assert that the result is 5 with a clear failure message if not
✅ Expected Result: Test passes if sum is 5; if not, assertion fails with the message 'Sum of 2 and 3 should be 5'
Automation Requirements - pytest
Assertions Needed:
Assert that the sum of two numbers equals expected value
Include a custom message in the assertion to explain failure
Best Practices:
Use clear and descriptive assertion messages
Keep test functions small and focused
Use pytest's assert statement with message parameter
Automated Solution
PyTest
import pytest

def add_numbers(a: int, b: int) -> int:
    return a + b

def test_add_numbers():
    result = add_numbers(2, 3)
    assert result == 5, f"Sum of 2 and 3 should be 5 but got {result}"

This test defines a simple function add_numbers that returns the sum of two inputs.

The test function test_add_numbers calls this function with 2 and 3, then asserts the result equals 5.

The assertion includes a message that will show if the test fails, helping to quickly understand what went wrong.

This message uses an f-string to show the actual result, making debugging easier.

Common Mistakes - 3 Pitfalls
Not including an assertion message
Using print statements instead of assertions
Writing complex logic inside the assertion message
Bonus Challenge

Now add data-driven testing with 3 different input pairs and expected sums

Show Hint

Practice

(1/5)
1. What is the main purpose of adding a message to an assert statement in pytest?
easy
A. To explain why the test failed when the assertion is false
B. To make the test run faster
C. To skip the test if the assertion fails
D. To change the test result to pass automatically

Solution

  1. Step 1: Understand the role of assert messages

    Assert messages provide extra information when a test fails, helping to understand the failure reason.
  2. Step 2: Identify the purpose in pytest

    In pytest, messages after assert statements show in the test report only if the assertion fails, clarifying the problem.
  3. Final Answer:

    To explain why the test failed when the assertion is false -> Option A
  4. Quick Check:

    Assert message = failure explanation [OK]
Hint: Assert messages explain failures clearly [OK]
Common Mistakes:
  • Thinking assert messages speed up tests
  • Believing messages skip tests
  • Assuming messages change test results
2. Which of the following is the correct syntax to add a message to an assert statement in pytest?
easy
A. assert(x == 5) : "x should be 5"
B. assert x == 5, "x should be 5"
C. assert x == 5 then "x should be 5"
D. assert x == 5 => "x should be 5"

Solution

  1. Step 1: Recall Python assert syntax

    Python assert statements use a comma to separate the condition and the message: assert condition, message.
  2. Step 2: Match with pytest usage

    pytest uses the same syntax, so assert x == 5, "x should be 5" is correct.
  3. Final Answer:

    assert x == 5, "x should be 5" -> Option B
  4. Quick Check:

    Assert syntax uses comma for message [OK]
Hint: Use comma after condition for assert message [OK]
Common Mistakes:
  • Using colon or arrow instead of comma
  • Adding message without comma
  • Using parentheses incorrectly
3. What will be the output when running this pytest code?
def test_value():
    x = 3
    assert x == 5, "x is not equal to 5"
medium
A. Test passes silently
B. Test skipped automatically
C. SyntaxError due to assert message
D. Test fails with message: x is not equal to 5

Solution

  1. Step 1: Check the assertion condition

    The variable x is 3, but the assertion expects x == 5, which is false.
  2. Step 2: Understand pytest behavior on failed assert

    pytest will fail the test and show the message "x is not equal to 5" to explain the failure.
  3. Final Answer:

    Test fails with message: x is not equal to 5 -> Option D
  4. Quick Check:

    False assert triggers failure message [OK]
Hint: Failed assert shows message in pytest output [OK]
Common Mistakes:
  • Assuming test passes despite false assert
  • Expecting syntax error from message
  • Thinking test skips automatically
4. Identify the error in this pytest assertion:
def test_check():
    y = 10
    assert y > 5 "y should be greater than 5"
medium
A. Missing comma between condition and message
B. Wrong comparison operator
C. Variable y is not defined
D. Message should be inside parentheses

Solution

  1. Step 1: Review assert syntax

    Assert statements require a comma between the condition and the message string.
  2. Step 2: Locate the syntax error

    The code misses a comma after y > 5, causing a syntax error.
  3. Final Answer:

    Missing comma between condition and message -> Option A
  4. Quick Check:

    Comma separates assert condition and message [OK]
Hint: Always put a comma before assert message [OK]
Common Mistakes:
  • Forgetting comma after condition
  • Adding parentheses around message
  • Changing comparison operator unnecessarily
5. You want to test a function that returns a list of even numbers from 1 to 5. Which assert statement with a message best helps debug if the test fails?
def get_evens():
    return [2, 4]

def test_evens():
    result = get_evens()
    # Choose the best assert with message
hard
A. assert result == [2, 4], "Check the function"
B. assert result == [2, 4], "Test failed"
C. assert result == [2, 4], f"Expected [2, 4], got {result}"
D. assert result == [2, 4], f"Result is {result}"

Solution

  1. Step 1: Understand the purpose of assert messages

    Good messages explain exactly what was expected and what was received to help debugging.
  2. Step 2: Compare options for clarity

    assert result == [2, 4], f"Expected [2, 4], got {result}" uses an f-string to show both expected and actual results clearly, aiding quick diagnosis.
  3. Final Answer:

    assert result == [2, 4], f"Expected [2, 4], got {result}" -> Option C
  4. Quick Check:

    Clear message shows expected vs actual [OK]
Hint: Show expected and actual values in assert message [OK]
Common Mistakes:
  • Using vague messages without details
  • Not showing actual result in message
  • Ignoring helpful f-string formatting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard