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
Step 1: Recall Python assert syntax
Python assert statements use a comma to separate the condition and the message: assert condition, message.
Step 2: Match with pytest usage
pytest uses the same syntax, so assert x == 5, "x should be 5" is correct.
Final Answer:
assert x == 5, "x should be 5" -> Option B
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
Step 1: Check the assertion condition
The variable x is 3, but the assertion expects x == 5, which is false.
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.
Final Answer:
Test fails with message: x is not equal to 5 -> Option D
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
Step 1: Review assert syntax
Assert statements require a comma between the condition and the message string.
Step 2: Locate the syntax error
The code misses a comma after y > 5, causing a syntax error.
Final Answer:
Missing comma between condition and message -> Option A
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
Step 1: Understand the purpose of assert messages
Good messages explain exactly what was expected and what was received to help debugging.
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.
Final Answer:
assert result == [2, 4], f"Expected [2, 4], got {result}" -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.