Pytest - the assert
statement#
The assert
statement in Python is used for debugging and testing purposes. It allows us to check if a condition is True
, and if it's not, it will Raise an Exception
called AssertionError
.
This can be very helpful in writing unit tests to verify if the expected results match the actual results.
Check it out#
- Let's see below
addition.py
and its test case.
test_file.py
from addition import addition
def test_addition_int():
assert addition(4, 5) == 9
assert addition(12, 2) == 14
def test_addition_str():
assert addition('a', 'b') == 'ab'
- to test it out, run
docker build -t pytest-assertion docs/learning-python/unit-testing/assertion/ && docker run pytest-assertion