Introduction
Test functions help check if parts of your code work correctly. They make sure your program does what you expect.
Test functions help check if parts of your code work correctly. They make sure your program does what you expect.
def test_function_name(): assert expression
Test functions start with test_ so pytest can find them.
Use assert to check if something is true.
def test_addition(): assert 2 + 2 == 4
def test_string_length(): assert len('hello') == 5
def test_list_contains(): assert 3 in [1, 2, 3]
This program defines a simple add function and tests it with three cases. Each assert checks if the result is correct.
def add(a, b): return a + b def test_add(): assert add(3, 4) == 7 assert add(-1, 1) == 0 assert add(0, 0) == 0
Keep test functions small and focused on one thing.
Use clear names starting with test_ so pytest finds them easily.
Run tests often to catch problems early.
Test functions check if code works as expected.
They start with test_ and use assert statements.
Running tests helps keep your code reliable and bug-free.
def test_sum():
result = 2 + 3
assert result == 5def test_multiply():
result = 4 * 5
assert result = 20def max_number(numbers):
return max(numbers)
# Choose the correct test function:Pass the test and this pattern moves unknown