Boost Your Code Quality with Pytest & Tox
Last Updated on March 31, 2024 by Editorial Team
Author(s): ronilpatil
Originally published on Towards AI.
In the realm of software development, ensuring code quality isnβt just a goal, itβs a mandate. With the ever-increasing complexity of applications, the need for robust testing methodologies has never been more critical. Enter unit testing β the cornerstone of a solid quality assurance strategy.
In this blog, weβll take our journey toward code confidence using two powerful tools β Pytest & Tox. Pytest, with its simplicity and flexibility, provides an elegant framework for writing and executing unit tests with ease. On the other hand, Tox enables us to create and manage local testing environments, ensuring consistency and reproducibility across different platforms and configurations.
If you havenβt gone through my previous series of blogs, Iβll recommend you to go through it.
Streamline ML Workflow with MLflowοΈ β I
PoA β Experiment Tracking U+1F504 U+007C Model Registry U+1F3F7οΈ U+007C Model Serving U+1F680
pub.towardsai.net
Streamline ML Workflow with MLflow β II
PoA β Experiment Tracking U+1F504 U+007C Model Registry U+1F3F7οΈ U+007C Model Serving
pub.towardsai.net
This is the next move in this series of blogs, here first Iβll explain βHow can we create a local testing environment using Toxβ, and then will test our code quality using Pytest.
Tox β Build QA Environment
First, install the tox using pip.
cmd: pip install tox
Once tox is installed, create tox.ini
configuration file. It defines various settings and options for running tests in multiple environments, such as different Python versions or with different dependencies. Below added tox.ini
config file for reference.
Here we will test our code on Python version 3.9 & 3.12. skipsdist = True
will skip the step of creating a distribution package during the testing process. Currently, our focus is to quickly iterate and test our code during development.
The [testenv]
section is a required section in the tox.ini
file and is used to define the default settings for test environments. When we define [testenv]
, Tox will automatically create a virtual environment for each environment listed in envlist
and apply the settings specified under [testenv]
. The name [testenv]
itself is not something we can change, it's a predefined section header that Tox recognizes. We can create as many [testenv]
sections as we need, each with its own set of dependencies and commands.
All the dependencies are specified using the deps
option under the [testenv]
section. The -r
flag indicates that Tox should read the dependencies from a requirements file.
The command to execute within the test environment is specified using commands
option under the [testenv]
section. In this case, it tells Tox to run Pytest with specific options (--verbose
for detailed output and --disable-warnings
to suppress warnings) on the test files located in the tests/
directory.
We can execute multiple test files or commands on different Python versions. Below, I added the GitHub repo link, you can check it out.
GitHub – Built an E2E MLFlow Pipeline
Contribute by ronylpatil
github.com
Pytest β The Matured Testing Framework
Instead of diving into theory, letβs jump to practical examples.
This is the simple testing code Iβve written to help you understand it better. I used a fixture
decorator on the top of add_num
the function to extend its functionality. And used params
an argument to test the function on various sets of parameters. Pytest will invoke the fixture once for each parameter value specified in the list. Inside the fixture function, the request.param
attribute allows you to access the current parameter value being used. Iβve written simple logic, but we may implement more complex ones based on our requirements.
You must refer to the official document for more details. Now, let's look at the actual code. Remember, hereβs how your folder structure would look with a βtestingβ directory.
project_name/
U+007C
βββ testing/ (This is where your tests go, we can use any name)
β βββ conftest.py
β βββ test_module1.py (file name must begin with test)
β βββ test_module2.py
β
βββ other directories...
In the above code snippet, Iβve written 8 test functions. All of them are using fixtures, but you may be surprised Iβve neither created nor imported any fixture in the codebase, but still, Iβm getting results!!! U+1F92F
Hereβs the Trick U+1F383, You can define fixtures in conftest.py
that provide a common setup or data for your tests. These fixtures can be used by any test file within the same directory and its subdirectories without the need of explicit imports, just pass the fixture as a parameter to your test function Thatβs it!!!
And everywhere I've used assert
statements, that check whether a condition is true, and if not, it raises an AssertionError
. Thatβs it!
Below added conftest.py
for your reference.
Output Snapshot
Basic Commands
1. cmd: tox -r
-- recreate the tox virtual environments from scratch
-- use if requirements.txt updated
2. cmd: tox
-- runs test environments defined in the tox.ini file
3. cmd: tox -e py3.9
--- runs the tox environment specified for Python v3.9, instead of py3.9
--- can use any python version
4. cmd: pytest
--- search for test files and run test functions
5. cmd: pytest testing/
--- runs tests present within the specified directory named "testing"
6. cmd: pytest testing/xyz.py
--- run specific test file present in "testing" directory
7. cmd: pytest testing/xyz.py::test_build_features
--- test specific function present in xyz.py test file
Tox vs Pytest Commands
Tox run the test specified in tox.ini
across different Python versions and environments configured in tox.ini
It is used to automate the execution of Pytest tests across different Python versions and environments.
Pytest is a testing framework used specifically for running Python tests. It provides a simple and flexible way to write and execute tests for Python code. If you use the Pytest command, the code will be tested in the current virtual environment. To execute the test in the Tox environment, use Tox commands. Both Tox & Pytest have different purposes in the context of testing.
Conclusion
Thatβs all, guys, I hope you enjoyed the blog. If this blog sparked your curiosity or ignited new ideas, follow me on Medium, GitHub & connect on LinkedIn, and letβs keep the curiosity alive.
Your questions, feedback, and perspectives are welcomed and celebrated. Feel free to reach out with any queries or share your thoughts.
Thank youU+1F64C &,
Keep pushing boundariesU+1F680
Cheers to bug-free codingU+1F37B
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI