0
0
PyTesttesting~20 mins

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

Choose your learning style9 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.