As an experienced Python developer, setting up a robust test automation harness is one of the best investments you can make in a project. After having worked on large and small Python codebases for over a decade, I‘ve found pytest to be the most versatile, scalable, and developer-friendly testing framework out there.
In this comprehensive guide, I‘ll share everything I‘ve learned about installing, configuring and using pytest for testing Python applications on Linux. I‘ll cover pytest best practices gathered from many teams and projects that have reaped enormous benefits by adopting pytest testing early on.
Why Testing Matters
Let‘s first talk about why testing matters – both from a business and a technical perspective.
The Business Viewpoint
According to a Capgemini study, the top motivations organizations have for test automation are:
- Faster time to market (70% respondents)
- Improved software quality (62%)
- Improved test coverage (60%)
- Cost savings in the long run (57%)
As you can see, the incentives are clear. Test automation allows software teams to ship better-quality products faster. This explains why testing is no longer optional for any high-performing technology team.
The Developer Viewpoint
As developers, we instinctively dislike repetitive manual work. Testing inherently falls into that bucket – executing the same test cases, again and again, to ensure nothing breaks.
Without automation, testing becomes a mind-numbing process that eats into development time. No one likes spending hours running manual tests after every code change!
Automation enables us to break free of that vicious cycle and focus on the more interesting aspects – the new features and code.
The incentive for developers is very simple – leveraging automation allows you to develop faster. Period.
Now that we‘re aligned on why test automation is critical, let‘s look at how pytest fits into the picture.
Why Pytest Has Become So Popular
The Python testing landscape has several frameworks to choose from including unittest, nose2 and pytest. Over the last 5 years pytest has emerged as the most popular choice due to its simplicity and versatility.
As per the 2020 Python Developers Survey, over 66% of developers now use pytest as their primary testing framework. In fact, pytest usage has nearly doubled since 2016 – a testament to its adoption rate.

Image source: JetBrains Python Developers Survey 2020
What explains this rapid rise? Based on my experience helping teams implement automation, I‘ve found the following factors drive pytest‘s popularity:
Easy to start: pytest has a very simple syntax that makes writing tests extremely terse and readable. Tests look almost like plain assert statements in code rather than being wrapped in classes and methods. This makes adopting pytest very easy even for those without prior testing experience.
Scales up: While simple on the surface, pytest offers powerful features like markers, fixtures,parametrization and plugins. These make it easy to tackle automation for complex systems and apps without much effort.
Extensible: Pytest has a vibrant ecosystem of hundreds of plugins for needs ranging from test reporting to performance measurement. If the built-in features don‘t cut it, chances are there is already a plugin that helps.
Ultra-popular: As more and more Python teams adopt pytest, it makes collaboration easier when people switch projects. There is no context-switching needed between different styles and frameworks.
The numbers speak for themselves – pytest offers the best of both worlds – ease of use and depth of capabilities. That‘s why it has overtaken other Python testing options.
With pytest firmly established as the leading choice, let‘s jump into getting it set up on your Linux machines.
Installing Pytest on Linux
Now that I‘ve convinced you to use pytest (I hope!), let‘s look at how to get it installed on your Linux distribution of choice.
I‘ll cover methods for the most popular Linux distros – Ubuntu, RHEL/CentOS, Arch Linux and Fedora.
Prerequisites
Before we being, double check that your Linux machine meets these prerequisites:
- Python 3.6 or higher: Pytest 5.x+ requires Python 3.6+. Verify by running
python3 --version - pip package manager: Needed for installing pytest using
pip. Install pip if missing by runningapt install python3-piporyum install python3-pip - User with sudo access: Required for globally installing pytest system-wide
Method #1: Install using System Package Manager
The easiest way to install pytest on Linux is leveraging your distribution‘s native package manager.
On Debian/Ubuntu systems, use apt:
sudo apt update
sudo apt install python3-pytest
On RHEL/CentOS machines, use yum:
sudo yum install python3-pytest
And on Fedora, use dnf:
sudo dnf install python3-pytest
This will install the latest version of pytest available in your distribution‘s repositories.
Pros:
- Easy installation without needing pip
- Native integration with OS package updates
Cons:
- Versions may be outdated compared to PyPI
- Isolation issues if you use virtual environments
So while the system package manager gets pytest installed quickly, it comes with some caveats.
Method #2: Install using Pip
The second method is to use Python‘s own package manager pip to grab pytest directly from PyPI (the Python Package Index).
Run the following command system-wide:
python3 -m pip install -U pytest
You can also install it for just your user to avoid sudo:
python3 -m pip install --user -U pytest
And here‘s how to install it inside a virtual environment:
python3 -m venv my_env
source my_env/bin/activate
pip install -U pytest
Pros:
- Faster access to new versions
- Avoid isolation issues with virtual environments
- More control over which pytest version to use
Cons:
- Slower than using native package managers
- pip packages don‘t integrate with system updates
Based on my experience with many teams, I recommend using pip virtual environments as the standard method for installing pytest. This offers maximum control and isolation for your automation needs.
Verifying the Pytest Installation
With pytest installed via either method, run this command to verify the version installed:
pytest --version
# OR
python3 -m pytest --version
This will print out the pytest version number:
pytest 7.2.0
If you don‘t see a version, pytest is likely not installed correctly or missing from your system PATH.
As a rule of thumb I suggest always using virtual environments for your Python automation work. This keeps dependencies isolated from your system Python which prevents a whole host of issues down the line.
Organizing Pytest Tests
Now that we have everything installed, let‘s talk a bit about best practices around organizing your test files and directories.
Grouping tests logically is vital because suites grow in size and complexity as you expand test coverage. Some suggestions based on my experience:
- Keep test files independent and small. Target one component per file.
- Mirror code layout. Follow the same src layout for test modules. For example
/apifolder containstest_api.py - Place shared fixtures & helpers inside a
conftest.pyfile that pytest automtically discovers - Use folders by type (unit,integration etc) or phase (smoke, end-to-end etc)
- Apply a consistent naming convention like
test_*or*_test.pyfor test modules
Here is an example layout for a web app:
pytest-project
├── conftest.py
├── integration
│ ├── test_web.py
│ └── test_ORM.py
└── unit
├── test_models.py
├── test_views.py
These simple rules go a long way in maintaining your test suites over time.
Sample Pytest Test
Let‘s create a sample test file that checks if pytest is configured correctly:
# test_func.py
def test_passing():
assert (1, 2, 3) == (1, 2, 3)
def test_failing():
assert 1 == 2
This contains:
- test_passing: checks a trivial assert to confirm pytest works
- test_failing: a test we expect to fail
Run the tests:
pytest test_func.py
Output:
============================= test session starts =============================
platform linux -- Python 3.6.8, pytest-7.2.0, pluggy-1.0.0
cachedir: .pytest_cache
rootdir: /home/myuser/projects
collected 2 items
test_func.py .F [100%]
================================== FAILURES ===================================
_______________________________ test_failing ________________________________
def test_failing():
> assert 1 == 2
E assert 1 == 2
test_func.py:8: AssertionError
============================ 1 failed, 1 passed in 0.12s =========================
We can see one test passed while test_failing failed as expected. This confirms that:
- Pytest is installed properly
- It‘s discovering and executing test functions
- Assertions and failure reporting are working
With this foundation in place, you can start writing test cases to validate your application code using pytest‘s powerful features.
In Closing
I hope this guide offered you a comprehensive overview of installing and configuring pytest for Python testing on Linux systems.
We took a quick tour of pytest‘s capabilities, why it has become so popular, and how to organize tests and directories. Finally, we validated a sample pytest test to confirm everything is good to go.
Automated testing is a crucial discipline for any serious Python project. Pytest‘s simplicity makes it very easy to adopt but it offers all the power and extensibility you need to test complex applications. I hope you found this guide helpful. Happy testing!


