pytest is a testing framework for Python that makes it easy to write simple as well as scalable test cases. It’s used for writing unit tests, integration tests, and even more advanced testing scenarios. Some of its features include:
pytest can automatically find tests in your codebase.pytest provides detailed information about the failure.# test_sample.py
def test_addition():
assert 1 + 1 == 2
def test_subtraction():
assert 2 - 1 == 1
To run the tests, navigate to the directory containing the tests and run:
pytest
Fixtures allow setup and teardown logic for tests.
import pytest
@pytest.fixture
def sample_data():
return {"key": "value"}
def test_sample_data(sample_data):
assert sample_data["key"] == "value"