Bird
0
0

Identify the error in this pytest assertion:

medium📝 Debug Q14 of 15
PyTest - Writing Assertions
Identify the error in this pytest assertion:
def test_strings():
    s1 = "hello"
    s2 = "Hello"
    assert s1 != s2
ANo error, test passes because strings differ in case
BSyntax error due to missing parentheses
CAssertion should use == instead of !=
DVariables s1 and s2 are not defined
Step-by-Step Solution
Solution:
  1. Step 1: Compare string values with case sensitivity

    Strings "hello" and "Hello" differ in case, so they are not equal.
  2. Step 2: Check assertion correctness

    The assertion assert s1 != s2 correctly checks that the strings are not equal, so the test passes without error.
  3. Final Answer:

    No error, test passes because strings differ in case -> Option A
  4. Quick Check:

    "hello" != "Hello" is True [OK]
Quick Trick: String comparison is case-sensitive by default [OK]
Common Mistakes:
MISTAKES
  • Assuming strings are equal ignoring case
  • Expecting syntax error without parentheses in assert
  • Confusing == and != in assertions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes