Bird
Raised Fist0
PyTesttesting~20 mins

Comparing values (equality, inequality) in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Comparing Values
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this pytest assertion?
Consider the following pytest test function. What will be the result of running this test?
PyTest
def test_compare_values():
    a = [1, 2, 3]
    b = [1, 2, 3]
    assert a == b
ATest passes because lists have the same values in order
BTest fails because lists are different objects
CTest raises a TypeError
DTest fails because lists have different lengths
Attempts:
2 left
💡 Hint
In Python, equality (==) compares values, not object identity.
❓ assertion
intermediate
1:30remaining
Which assertion correctly checks inequality of two variables?
You want to write a pytest assertion to confirm that variable x is NOT equal to variable y. Which option is correct?
Aassert x <> y
Bassert x != y
Cassert x =! y
Dassert not x == y
Attempts:
2 left
💡 Hint
Use the standard Python operator for inequality.
🔧 Debug
advanced
2:30remaining
Why does this pytest assertion fail unexpectedly?
Examine the test below. Why does the assertion fail even though the printed values look equal?
PyTest
def test_string_compare():
    s1 = 'Test\n'
    s2 = 'Test\r\n'
    print(repr(s1))
    print(repr(s2))
    assert s1 == s2
AThe strings differ in newline characters, so they are not equal
BThe assertion fails because print changes the strings
CThe test fails due to a syntax error in the assertion
DThe strings are equal but the assertion is written incorrectly
Attempts:
2 left
💡 Hint
Look carefully at the escape characters in the strings.
🧠 Conceptual
advanced
2:00remaining
What is the difference between '==' and 'is' in Python testing?
In pytest assertions, what is the key difference between using '==' and 'is' when comparing two variables?
A'==' and 'is' are interchangeable in pytest assertions
B'==' checks object identity, 'is' checks value equality
C'==' checks value equality, 'is' checks if both variables point to the same object
D'==' checks type equality, 'is' checks value equality
Attempts:
2 left
💡 Hint
Think about comparing two different lists with the same content.
❓ framework
expert
3:00remaining
Which pytest assertion will fail when comparing floating point numbers due to precision issues?
Given the following test, which assertion will fail due to floating point precision problems?
PyTest
def test_float_comparison():
    a = 0.1 + 0.2
    b = 0.3
    # Which assertion fails?
Aassert abs(a - b) < 1e-9
Bassert round(a, 1) == round(b, 1)
Cassert a != b
Dassert a == b
Attempts:
2 left
💡 Hint
Floating point arithmetic can cause small precision errors.

Practice

(1/5)
1. Which operator in pytest assertions checks if two values are equal?
easy
A. <
B. !=
C. >
D. ==

Solution

  1. Step 1: Understand equality operator

    The operator == is used to check if two values are equal in Python and pytest assertions.
  2. Step 2: Differentiate from inequality

    The operator != checks if values are not equal, so it is not the correct choice for equality.
  3. Final Answer:

    == -> Option D
  4. Quick Check:

    Equality check = == [OK]
Hint: Use == for equality, != for inequality [OK]
Common Mistakes:
  • Confusing == with !=
  • Using > or < for equality
  • Forgetting to use assertion keyword
2. Which of the following is the correct syntax to assert that variable a is not equal to b in pytest?
easy
A. assert a != b
B. assert a =! b
C. assert a ==! b
D. assert a = b

Solution

  1. Step 1: Identify correct inequality syntax

    The correct operator for inequality is !=, so the assertion should be assert a != b.
  2. Step 2: Check syntax correctness

    Options with =! or ==! are invalid syntax and will cause errors.
  3. Final Answer:

    assert a != b -> Option A
  4. Quick Check:

    Inequality assertion = assert a != b [OK]
Hint: Remember: inequality is !=, not =! or ==! [OK]
Common Mistakes:
  • Using =! instead of !=
  • Using = instead of ==
  • Mixing equality and inequality operators
3. What will be the result of this pytest assertion?
def test_numbers():
    x = 10
    y = 5 + 5
    assert x == y
medium
A. Syntax error in assertion
B. Test fails because 10 does not equal 5
C. Test passes because 10 equals 10
D. Test fails because variables are different

Solution

  1. Step 1: Calculate values of variables

    Variable x is 10, and y is calculated as 5 + 5, which equals 10.
  2. Step 2: Evaluate assertion condition

    The assertion assert x == y checks if 10 equals 10, which is true, so the test passes.
  3. Final Answer:

    Test passes because 10 equals 10 -> Option C
  4. Quick Check:

    10 == 10 is True [OK]
Hint: Add values before comparing for equality [OK]
Common Mistakes:
  • Assuming variables differ without calculation
  • Confusing syntax errors with logic errors
  • Ignoring that 5 + 5 equals 10
4. Identify the error in this pytest assertion:
def test_strings():
    s1 = "hello"
    s2 = "Hello"
    assert s1 != s2
medium
A. No error, test passes because strings differ in case
B. Syntax error due to missing parentheses
C. Assertion should use == instead of !=
D. Variables s1 and s2 are not defined

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]
Hint: String comparison is case-sensitive by default [OK]
Common Mistakes:
  • Assuming strings are equal ignoring case
  • Expecting syntax error without parentheses in assert
  • Confusing == and != in assertions
5. You want to write a pytest test to check that a function get_status() returns a value that is not equal to None. Which assertion is the best choice?
hard
A. assert get_status() == None
B. assert get_status() is not None
C. assert get_status() != None
D. assert get_status() is None

Solution

  1. Step 1: Understand None comparison best practice

    In Python, to check if a value is not None, the recommended way is to use is not None rather than != None.
  2. Step 2: Evaluate options for correctness

    assert get_status() == None checks equality to None (wrong for this case). assert get_status() != None uses != None (works but not best practice). assert get_status() is not None uses is not None which is best practice. assert get_status() is None checks if value is None (opposite of requirement).
  3. Final Answer:

    assert get_status() is not None -> Option B
  4. Quick Check:

    Use 'is not None' for None checks [OK]
Hint: Use 'is not None' to check for non-None values [OK]
Common Mistakes:
  • Using == None instead of is None
  • Using != None instead of is not None
  • Confusing equality with identity for None
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