pytest is a mature, full-featured Python testing framework that helps you write simple, scalable, and robust tests. It's widely used for all types of software testing, including unit, integration, and functional testing.
Key features and concepts of pytest:
1. Simple Test Syntax: pytest encourages writing concise and readable tests using standard `assert` statements instead of verbose `self.assertEqual()` or similar methods found in `unittest`.
2. Automatic Test Discovery: By default, pytest automatically finds tests in files named `test_-.py` or `-_test.py`, and within those files, functions named `test_-` or classes named `Test-` (without an `__init__` method).
3. Fixtures: Fixtures are functions that pytest runs before (and sometimes after) your test functions. They are used to set up a baseline state for tests, provide data, or manage resources (like database connections, temporary files, etc.). Fixtures ensure tests are isolated and repeatable. They are declared using the `@pytest.fixture` decorator and are injected into test functions by naming them as arguments.
4. Parametrization: pytest allows you to run the same test function multiple times with different sets of input data. This is achieved using the `@pytest.mark.parametrize` decorator, significantly reducing code duplication.
5. Plugins and Extensibility: pytest has a rich plugin ecosystem. Plugins extend its functionality for various purposes, such as test coverage (`pytest-cov`), parallel test execution (`pytest-xdist`), reporting, mocking, and more. It also provides hooks for users to write their own custom plugins.
6. Detailed Test Reports: When tests fail, pytest provides detailed traceback information, making it easier to diagnose issues.
7. Marks: Markers (`@pytest.mark.<name>`) allow you to attach metadata to test functions, enabling selective test execution (e.g., `pytest -m "slow"` to run only tests marked as 'slow').
Why use pytest?
- Readability and Simplicity: Tests are easier to write and understand.
- Flexibility: Highly configurable and extensible to fit complex testing needs.
- Efficiency: Powerful features like fixtures and parametrization help avoid code duplication and manage test setup effectively.
- Rich Ecosystem: A vast collection of plugins supports almost any testing scenario.
Basic Usage: Install with `pip install pytest`. To run tests, simply navigate to your project directory in the terminal and type `pytest`.
Example Code
First, install pytest: pip install pytest
--- 1. Create a module to test (e.g., my_functions.py) ---
my_functions.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a - b
--- 2. Create a test file (e.g., test_my_functions.py) ---
test_my_functions.py
import pytest
from my_functions import add, subtract, multiply
Basic tests using assert statements
def test_add_positive_numbers():
assert add(1, 2) == 3
def test_add_negative_numbers():
assert add(-1, -2) == -3
def test_add_zero():
assert add(0, 5) == 5
Test with a fixture (example: a reusable input for tests)
@pytest.fixture
def sample_numbers():
return 10, 5
def test_subtract_numbers(sample_numbers):
num1, num2 = sample_numbers
assert subtract(num1, num2) == 5
def test_multiply_numbers(sample_numbers):
num1, num2 = sample_numbers
assert multiply(num1, num2) == 50
Parametrized test (running the same test with different inputs)
@pytest.mark.parametrize("input_a, input_b, expected_output", [
(1, 1, 2),
(2, 3, 5),
(0, 0, 0),
(-1, 1, 0)
])
def test_add_various_inputs(input_a, input_b, expected_output):
assert add(input_a, input_b) == expected_output
You can also mark tests to categorize them
@pytest.mark.slow
def test_a_slow_operation():
Imagine this test takes a long time to run
assert True Placeholder
--- 3. How to run these tests ---
Save the files as `my_functions.py` and `test_my_functions.py`
in the same directory.
Open your terminal in that directory and run:
pytest
To run only tests marked as 'slow':
pytest -m slow
To run tests with verbose output:
pytest -v
To run tests and stop after the first failure:
pytest -x








pytest