In our previous article — “Introduction to Python for Test Automation” — we discussed why Python is a powerful language for building automation testing frameworks. We also explored the benefits of Python’s readability, rich ecosystem, and seamless integration with CI/CD pipelines.
Now, let’s go deeper and talk about how to practically implement test automation using one of Python’s most widely adopted testing libraries — pytest.
pytest isn’t just a testing framework; it's an ecosystem designed for fast, scalable, readable, and maintainable test automation.
Why pytest for Test Automation?
- pytest offers a significant advantage over traditional unittest or nose:
- Function-based testing: No need to create classes unnecessarily.
- Automatic test discovery: Zero configuration needed to find test files.
- Powerful fixture system: Dependency injection for setup/teardown.
- Built-in assertions: Native assert statements with introspection.
- Massive plugin ecosystem: Support for parallel execution, HTML reports, mocking, and more.
Step 1: Installing pytest
Start with setting up your environment.
Install using pip:
pip install pytest
Optional: Use a virtual environment
python -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\activate.bat # Windows
Verify the installation:
pytest --version
Step 2: Project Structure and Test Discovery
pytest uses convention over configuration, so if your files and test functions follow naming conventions, they’re automatically detected.
File Naming Conventions
Test file must start with test_ or end with _test.py
Test function must start with test_
project/
│
├── tests/
│ ├── test_login.py
│ ├── test_api.py
│ └── conftest.py # Shared fixtures and hooks
│
├── src/
│ └── app_logic.py
│
└── pytest.ini # Configuration file
Test Discovery:
pytest
pytest will search for all matching tests under the current directory.
Step 3: Writing Your First Test
Let's write a simple test to validate a function:
File: test_math.py
def add(x, y):
return x + y
def test_addition():
assert add(2, 3) == 5
Running the Test:
pytest test_math.py
Output:
collected 1 item
test_math.py . [100%]
pytest provides clean, readable, and color-coded output.
Step 4: Configuring pytest with pytest.ini
Create a pytest.ini or pyproject.toml file to customize test behavior.
Example pytest.ini:
[pytest]
addopts = -v --maxfail=2 --disable-warnings
testpaths = tests
python_files = test_*.py
What It Does:
- -v: Verbose output
- --maxfail: Stop after 2 failures
- testpaths: Where pytest should look for tests
- python_files: Pattern for test files
🔍 Pro Tip: For larger teams or CI/CD pipelines, version-controlling this config ensures consistency.
Step 5: Fixtures – Reusable Setup Logic
pytest’s fixture system lets you abstract test setup and teardown into reusable functions:
import pytest
@pytest.fixture
def user_data():
return {"username": "admin", "password": "secure123"}
def test_username(user_data):
assert user_data["username"] == "admin"
Fixture Scope:
You can control how often a fixture is invoked:
@pytest.fixture(scope="module") # "function", "class", "module", "session"
Fixtures improve test readability, modularity, and maintainability.
Final Thoughts
pytest stands out as the most efficient, flexible, and developer-friendly framework for Python test automation. Whether you’re just starting out or scaling a large QA project, pytest offers the simplicity of writing tests, the power of fixtures, and the extensibility of plugins — all of which make it the go-to choice for modern test automation.
By mastering the basics of pytest — from installation and configuration to writing and running tests — you're laying the foundation for a scalable, maintainable, and reliable automation suite.
As a leading Web and mobile automation testing company, at Testrig Technologies, we help startups and enterprises build scalable test automation frameworks. Our QA engineers specialize in creating CI/CD-ready, Python-based testing architectures that reduce release cycles and improve quality at every stage.
Top comments (0)