Let’s get started.
What Is Pytest?
Pytest is a popular Python testing framework used to write simple unit tests as well as complex functional test suites. It offers powerful features like:
- Auto-discovery of tests
- Fixtures for setup/teardown
- Parameterization for multiple test inputs
- Easy assertions with detailed error messages
Its simplicity and extensibility make it the go-to framework for many Python developers.
Step 1: Installing Pytest
Before you can run pytest, you need to install it. Run this command in your terminal:
pip install pytest
To verify the installation:
pytest --version
You should see the installed version of pytest.
Step 2: Writing Your First Test
Create a Python file called test_sample.py in your project directory. Pytest automatically discovers files that start with test_ or end with _test.py.
# test_sample.py
def func(x):
return x + 1
def test_answer():
assert func(3) == 4
In this example:
- func() is the function under test.
- test_answer() is your test function.
Step 3: How to Run Pytest
Once your test file is ready, open your terminal and run pytest with the following command:
pytest
Pytest will:
- Search for all test files in the current directory and subdirectories
- Run functions prefixed with test_
- Display a summary of passed and failed tests
Output Example:
collected 1 item
test_sample.py . [100%]
1 passed in 0.01s
Step 4: Organizing Tests
It’s a good practice to keep your tests in a dedicated folder, typically named tests/. Here’s a common structure:
my_project/
├── app/
│ └── main.py
├── tests/
│ ├── __init__.py
│ └── test_main.py
You can run all tests from the root directory using:
pytest tests/
Step 5: Using Fixtures for Setup and Teardown
Fixtures help you manage setup and cleanup for your tests. Example:
import pytest
@pytest.fixture
def input_value():
return 5
def test_add(input_value):
assert input_value + 5 == 10
Run this with:
pytest test_sample.py
Fixtures make your code DRY and reusable across multiple tests.
Step 6: Running Specific Tests
You can target specific files or functions:
Run a specific file:
pytest tests/test_main.py
Run a specific test function:
pytest tests/test_main.py::test_function_name
This is useful during development when you want to test only the part you’re working on.
Step 7: Parametrize Tests
Want to test multiple inputs without writing separate functions? Use @pytest.mark.parametrize.
import pytest
@pytest.mark.parametrize("input,expected", [
(2, 3),
(3, 4),
(4, 5),
])
def test_increment(input, expected):
assert input + 1 == expected
This runs the test 3 times with different values.
Step 8: Viewing Detailed Output
By default, pytest keeps the output short. To see more detailed output, add the -v flag:
pytest -v
To see print statements and logs:
pytest -s
Combine both:
pytest -v -s
Step 9: Cleaning Up Test Caches
Pytest creates a .pytest_cache/ directory. If needed, you can remove cache before running tests:
pytest --cache-clear
Bonus: Run Pytest with Coverage
Want to measure how much of your code is covered by tests?
First, install coverage plugin:
pip install pytest-cov
Then run:
pytest --cov=my_module tests/
This will show line-by-line coverage data for your Python modules.
Automate Testing with Keploy (for API-based projects)
If you're testing REST APIs and want to auto-generate test cases and mocks, consider using Keploy.io. It captures real API traffic and turns it into reproducible test cases — so you never miss edge cases and reduce the need for manual test writing.
Combine Keploy with Pytest to supercharge your backend testing strategy.
Final Thoughts
Running pytest is simple — but its full potential lies in how you write, organize, and extend your tests. With features like fixtures, parameterization, and coverage tools, Pytest allows developers to maintain high code quality with minimal friction.
So next time you ask, “how to run a pytest?” — remember, it’s not just about running tests, but running them smartly.
Read more on- https://keploy.io/blog/community/how-to-run-pytest-program