Test Overview
This test checks if two values are equal and if two other values are not equal using simple assertions in pytest.
This test checks if two values are equal and if two other values are not equal using simple assertions in pytest.
import pytest def test_compare_values(): a = 5 b = 5 c = 10 d = 20 assert a == b, "Values a and b should be equal" assert c != d, "Values c and d should not be equal"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | — | PASS |
| 2 | Assign values a=5, b=5, c=10, d=20 | Variables set in test function | — | PASS |
| 3 | Assert that a equals b | a=5, b=5 | Check if a == b | PASS |
| 4 | Assert that c does not equal d | c=10, d=20 | Check if c != d | PASS |
| 5 | Test ends successfully | All assertions passed | — | PASS |
== 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