Challenge - 5 Problems
Assert Message Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate2:00remaining
Understanding assert messages in pytest
What will be the output message when this pytest assertion fails?
PyTest
def test_sum(): result = 2 + 2 assert result == 5, "Sum calculation failed! Expected 5 but got {result}"
Attempts:
2 left
💡 Hint
Check how the assert message string is formatted.
✗ Incorrect
The assert message is a plain string and does not use f-string formatting, so {result} is not replaced by its value.
❓ assertion
intermediate2:00remaining
Correct use of assert message with variable interpolation
Which option correctly shows how to include the variable 'result' in the assert message so it displays its value?
PyTest
def test_multiply(): result = 3 * 3 assert result == 10, ???
Attempts:
2 left
💡 Hint
Use Python f-strings for variable interpolation inside strings.
✗ Incorrect
Only option A uses an f-string, which correctly replaces {result} with its value 9.
❓ Predict Output
advanced2:00remaining
Output of pytest assertion with message
What is the output when running this pytest test?
PyTest
def test_divide(): value = 10 / 2 assert value == 6, f"Division result incorrect: got {value}"
Attempts:
2 left
💡 Hint
Check the actual value of value and the assertion condition.
✗ Incorrect
value is 5.0, not 6, so assertion fails and message shows the actual value 5.0.
🔧 Debug
advanced2:00remaining
Debugging assert message with wrong formatting
Why does this pytest assertion raise a SyntaxError?
PyTest
def test_subtract(): result = 10 - 3 assert result == 8, f"Subtraction failed: got {result"
Attempts:
2 left
💡 Hint
Check the f-string syntax carefully.
✗ Incorrect
The f-string is missing a closing brace '}', so Python raises SyntaxError before running the test.
❓ framework
expert2:00remaining
Best practice for assert messages in pytest
Which option is the best practice for writing assert messages in pytest to help debugging when tests fail?
Attempts:
2 left
💡 Hint
Think about what helps you understand test failures quickly.
✗ Incorrect
Detailed messages with actual and expected values help quickly identify what went wrong.