Verify equality and inequality comparisons between two values
Preconditions (2)
✅ Expected Result: All assertions pass without errors, confirming correct equality and inequality comparisons.
import pytest def test_compare_values(): a = 10 b = 20 c = 10 assert a != b, f"Expected {a} to not equal {b}" assert a < b, f"Expected {a} to be less than {b}" assert b > a, f"Expected {b} to be greater than {a}" assert a == c, f"Expected {a} to equal {c}"
This test function test_compare_values defines three variables a, b, and c with values 10, 20, and 10 respectively.
It then uses assert statements to check:
a == c confirms both have the same value.a != b confirms they differ.a < b and b > a confirm the relative order.Each assertion includes a message to help understand failure if it occurs.
Running this with pytest will show a pass if all conditions hold true.
Now add data-driven testing with 3 different pairs of values to compare equality and inequality.
== is used to check if two values are equal in Python and pytest assertions.!= checks if values are not equal, so it is not the correct choice for equality.a is not equal to b in pytest?!=, so the assertion should be assert a != b.=! or ==! are invalid syntax and will cause errors.def test_numbers():
x = 10
y = 5 + 5
assert x == yx is 10, and y is calculated as 5 + 5, which equals 10.assert x == y checks if 10 equals 10, which is true, so the test passes.def test_strings():
s1 = "hello"
s2 = "Hello"
assert s1 != s2assert s1 != s2 correctly checks that the strings are not equal, so the test passes without error.get_status() returns a value that is not equal to None. Which assertion is the best choice?is not None rather than != None.is not None which is best practice. assert get_status() is None checks if value is None (opposite of requirement).Pass the test and this pattern moves unknown