
[ad_1]
Level up your test game with PyTest

Testing is an important aspect of the software development lifecycle. Gone are the days when software was 100% manually tested. Most companies are taking advantage of automation tools as part of the testing lifecycle. These tools help to control and automate the execution of tests on each commit or update on the code base.
Test automation is the key to a successful project that follows the Agile or DevOps methodology. This significantly improves the efficiency and accuracy of your product during each release.
In this tutorial, we are going to learn about a Python testing module called pytest
, on the basis of official document, pytest
Is:
“… an open source framework for writing small tests, yet scales to support complex functional testing for applications and libraries.”
Let’s move on to the next section and start installing the required modules.
It is highly recommended to create a virtual environment before continuing. Activate your virtual environment and run the following command:
pip install pytest
You can confirm the installation via
pip show pytest
either
pytest -h
In this section, we will go through the basic concepts behind it pytest
,
To begin, create a new Python file in your working directory called utils.py
, The file serves as a utility function for our test case. Add the following code inside the file:
def add(x, y):
return x + ydef subtract(x, y):
return x - ydef multiply(x, y):
return x * y
create a test script
After that, create a new python file named test_app.py
, You can name it anything you like, but it must follow one of the following syntaxes:
- starts with
test_
- ends with
_test
Let’s create some test functions inside test_app.py
file. pytest
Automatically executes tasks starting with test_
prefix. You only need to add the assertion inside the function. For example,
test in progress
Once you are done with that, run the following command on your terminal:
pytest
You should see the following output:

you can specify -v
Arguments for verbose output:
pytest -v
Running the above command gives the following output:

Let’s create a failing test case to understand how pytest
Reports errors. within test_app.py
replace the following statement with
assert utils.multiply(3, 3) == 9
To:
assert utils.multiply(3, 3) == 8
Run the test again, and you should get the following expected output for a failed test case:

change the value back 9
Once you are done with it.
You can group some test functions inside a class. have to start with the class Test
prefix for it to take effect.
using substring expressions
If you intend to run only a subset of the test tasks, you can choose to do it via:
- substring expression, or
- scar
For the first use case, just specify -k
argument followed by the desired string. For example, the following command will only run functions that contain a substring add
,
pytest -k add -v
notice that pytest
Collected three tasks and ran only one of them:

using markers
In addition, you can also use markers to narrow down your test tasks into different groups. Add the following import statement at the top of the file:
import pytest
Then, decorate the desired function with the following decorator. name
Represents the custom name for the marker. You can name it whatever you like.
@pytest.mark.<name>
I am going to mark two functions using basic
Name as:
For markers, you need to call -m
Instead the logic is as follows:
pytest -m basic -v
The following output will be displayed on your terminal.

Don’t be alarmed by the warnings, as this indicates that your marker is not yet registered. Just create a new file named pytest.ini
in the same directory. Inside it add the following configuration:
[pytest]
markers =
basic
You can add an optional statement to it as follows:
[pytest]
markers =
basic: mark test as basic
pytest
comes with built-in parametrize
Marker, which allows you to parameterize the input value for your functions. let’s modify test_multiplication
Functions to accept two input parameters:
- input value for multiplication
- expected output
You should end up with the following code snippet:
def test_multiplication(self, value, output):
assert utils.multiply(value, value) == output
Then, decorate the function as follows:
@pytest.mark.parametrize("value, output", [(2, 4), (3, 9), (4, 16), (5, 25)])
def test_multiplication(self, value, output):
assert utils.multiply(value, value) == output
You must provide a string representation of the expected input as the first parameter to the decorator.
The second parameter represents the input values passed to the function during the test. Since I have specified four items in the list, pytest
will run test_multiplication
Four times using the respective values.
When you run the test you should get the following output:

In addition, you can store the test data in a variable and re-use it on other test functions:
data = [(2, 4), (3, 9), (4, 16), (5, 25)]
Our final test script is as follows:
Let’s recap what we learned today.
We started with a brief explanation of the importance of testing and why we should automate the testing process.
Next, we installed pytest
modules in our virtual environment.
We went on to implement the three basic functions and created the corresponding test functions. In addition, we also learned to group functions using substring expressions or markers to form subset test cases.
Finally, we explored decorating our test functions with parameterize decorators.
Thank you for reading this piece. Hope to see you again in the next article!
[ad_2]
Source link
#Writing #Automation #Test #Scripts #Python #Wai #Phung #August