SyntaxStudy
Sign Up
Python Beginner 3 min read

pytest Basics

pytest Tests

pytest uses plain assert statements with helpful failure messages. Group tests in classes or plain functions.

Example
class TestMath:
    def test_add(self):
        assert 2 + 2 == 4

    def test_divide(self):
        assert 10 / 2 == 5.0

def test_string():
    result = "hello".upper()
    assert result == "HELLO"

def test_list():
    items = [1, 2, 3]
    assert len(items) == 3
    assert 2 in items
Pro Tip

pytest rewrites assert statements to show the actual vs expected values on failure — no assertEqual needed.