pytestEasy Examples

Feature-rich testing: fixtures, parametrize, plugins, auto-discovery

Getting started with pytest

Installation and basic usage of pytest.

python
# Install: pip install pytest
# test_example.py
import pytest

def add(a, b):
    return a + b

def test_add():
    assert add(1, 2) == 3
    assert add(-1, 1) == 0

def test_add_strings():
    assert add("hello", " world") == "hello world"

@pytest.fixture
def sample_data():
    return [1, 2, 3, 4, 5]

def test_sum(sample_data):
    assert sum(sample_data) == 15
Expected Output
# Expected output shown below
# (Run locally with: pytest)

pytest is a third-party package. Feature-rich testing: fixtures, parametrize, plugins, auto-discovery. Install with: pip install pytest

Common pytest operations

Frequently used features of pytest.

python
# Install: pip install pytest
import pytest

# Common pytest patterns
print(f"pytest version: {pytest.__version__}")

These are the most commonly used features of pytest in everyday development.

Want to try these examples interactively?

Open Easy Playground