DEV Community

niuniu
niuniu

Posted on

Quick Tip: Python pytest Examples

Quick Tip

Python pytest examples:

# Basic test
def test_addition():
    assert 1 + 1 == 2

# Test with fixtures
@pytest.fixture
def sample_data():
    return {'name': 'Alice', 'age': 30}

def test_sample(sample_data):
    assert sample_data['name'] == 'Alice'

# Parametrize tests
@pytest.mark.parametrize("input,expected", [
    (1, 2),
    (2, 4),
    (3, 6),
])
def test_multiply(input, expected):
    assert input * 2 == expected
Enter fullscreen mode Exit fullscreen mode

Powered by MonkeyCode: https://monkeycode-ai.net/

python #testing #tips

Top comments (0)