Assertions


pytest allows you to use the standard python assert for verifying expectations and values in Python tests.
filename: test_addition.py

import pytest
def test_add():
   
assert (10+5 ==15)
def testaddeven_num():
   
assert(4+4==9)

to assert that your function returns a certain value. If this assertion fails you will see the return value of the function call:
if you specify a message with the assertion like this:

assert(4+4==9,"Should be 8 after adding the two values")

then no assertion introspection takes places at all and the message will be simply shown in the traceback.


Assertions about expected exceptions
In order to write assertions about raised exceptions, you can use pytest.raises as a context manager like this:

import pytest
def test_zero_division():
    with pytest.raises(ZeroDivisionError):
        1 / 0


Comments