Skip to content

Python assert multiple conditions

  • by

If you want multiple conditions in Python to assert and want to do more than one comparison then compare tuples. Or simply write out multiple assertions.

Python asserts multiple conditions

Simple example code.

def foo(x):
return x + 1


def bar(y):
return y - 1


def test_foo():
# some expensive calculation
a = foo(10)

# another expensive calculation
b = bar(10)

assert (a, b) == (10, 9)


test_foo()

Output:

Python asserts multiple conditions

Source: https://stackoverflow.com/questions/43119776/

Simply write out 3 assertions with the inputs and expected outputs.

def my_func(value):
    return False if value >= 0 else True

def test_my_func(self):
    assert my_func(1) is False
    assert my_func(-1) is True
    assert my_func(0) is True

Do comment if you have any doubts or suggestions on this Python topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading