0
0
PytestHow-ToBeginner ยท 3 min read

How to Assert Not Equal in pytest: Simple Guide

In pytest, you can assert that two values are not equal using the assert statement combined with the != operator, like assert a != b. This will pass if a and b are different and fail if they are the same.
๐Ÿ“

Syntax

To check that two values are not equal in pytest, use the assert keyword followed by the expression a != b. Here:

  • assert triggers the test check.
  • a and b are the values you want to compare.
  • != means 'not equal'.

If a is not equal to b, the test passes. Otherwise, it fails.

python
assert a != b
๐Ÿ’ป

Example

This example shows a simple pytest test function that asserts two numbers are not equal. It demonstrates how pytest reports success or failure.

python
def test_values_not_equal():
    x = 5
    y = 10
    assert x != y  # This will pass because 5 is not equal to 10

def test_values_equal():
    a = 'hello'
    b = 'hello'
    assert a != b  # This will fail because both strings are the same
Output
============================= test session starts ============================== collected 2 items test_sample.py .F [100%] =================================== FAILURES =================================== ______________________________ test_values_equal ______________________________ def test_values_equal(): a = 'hello' b = 'hello' > assert a != b # This will fail because both strings are the same E assert 'hello' != 'hello' test_sample.py:9: AssertionError =========================== short test summary info =========================== FAILED test_sample.py::test_values_equal - assert 'hello' != 'hello' ============================== 1 failed, 1 passed ==============================
โš ๏ธ

Common Pitfalls

One common mistake is using assert a != b when you actually want to check equality, or vice versa. Another is forgetting that assert stops the test immediately on failure, so subsequent code won't run.

Also, avoid using assertNotEqual from unittest in pytest tests; pytest prefers plain assert statements.

python
def test_wrong_assert():
    x = 3
    y = 3
    # Wrong: this will fail because x equals y
    assert x != y

# Correct way:
def test_correct_assert():
    x = 3
    y = 4
    assert x != y
Output
============================= test session starts ============================== collected 2 items test_sample.py F. [100%] =================================== FAILURES =================================== ______________________________ test_wrong_assert ______________________________ def test_wrong_assert(): x = 3 y = 3 > assert x != y E assert 3 != 3 test_sample.py:4: AssertionError ============================== 1 failed, 1 passed ==============================
๐Ÿ“Š

Quick Reference

Remember these tips when asserting not equal in pytest:

  • Use assert a != b for not equal checks.
  • pytest shows detailed error messages on failure.
  • Do not mix unittest assertions with pytest style.
  • Assertions stop test execution on failure.
โœ…

Key Takeaways

Use plain assert with != operator to check values are not equal in pytest.
pytest provides clear failure messages when assert conditions fail.
Avoid mixing unittest assertion methods with pytest style asserts.
Remember assert stops test execution immediately on failure.
Write simple, readable assert statements for clear test results.