intermediateLibraries
Testing with pytest
Write and run tests to ensure code correctness.
In this lesson
01Writing Tests
Use pytest for simple and powerful testing.
Python
1# calculator.py2def add(a, b):3 return a + b45def divide(a, b):6 if b == 0:7 raise ValueError("Cannot divide by zero")8 return a / b910# test_calculator.py11import pytest12from calculator import add, divide1314def test_add():15 assert add(2, 3) == 516 assert add(-1, 1) == 01718def test_divide():19 assert divide(10, 2) == 5.020 with pytest.raises(ValueError):21 divide(10, 0)2223# Run: pytest test_calculator.py -v