Open In App

Execute a Subset of Test Suite in Pytest

Last Updated : 04 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A testing framework in Python that is used to write API test cases is called Pytest. There are some circumstances in which the user doesn’t want to execute all the test suites, but want to execute only certain test cases. In such instances, you can run test suites based on substring matching of test names or on the markers applied.

Execute a Subset of Test Suite in Pytest

Below are the ways by which we can execute a subset of the test suite in Pytest:

  • Based on the substring matching of test names
  • Based on the markers applied

Based on Substring Matching of Test Names

The word that helps to execute the test cases that have a common substring in them is known as substring_match. In this method, we will see how we can execute a subset of test suites in Pytest based on the substring matching of test names.

Syntax:

pytest main.py -k substring_match -v

Here,

  • substring_match: It is the substring to be matched while executing the tests in Pytest.

Example: In this example, we have created a program that has four test cases, checking floor as function name test_check_floor, equality as function name test_equal, subtraction as function name test_check_difference, and square root as function name test_square_root. Here, two functions have checked as common substrings in their names, thus we will execute this subset of test suites.

Python3




# Importing the math library
import math
 
# Creating first test case
def test_check_floor():
   num = 6
   assert num==math.floor(6.34532)
 
# Creating second test case
def test_equal():
   assert 50 == 49
 
# Creating third test case
def test_check_difference():
   assert 99-43==57
 
# Creating fourth test case
def test_square_root():
   val=8
   assert val==math.sqrt(81)


Output

Now, we will run the following command with substring_match as check as follows:

pytest main.py -k check -v

Output

Based on the Markers Applied

The word which helps to execute the test cases which have common marker name is known as marker_name. In this method, we will see how we can execute a subset of test suites in Pytest based on the markers applied.

Syntax:

In main.py file, write the following line on the top of the test case:

@pytest.mark.marker_name

In terminal, write the following command to execute a subset of test suites:

pytest main.py -k marker_name -v

Here,

  • marker_name: It is the marker name to be written on top of tests which we want to execute together.

Example: In this example, we have created a program that has four test cases, checking floor, equality, subtraction, and square root. Here, we have used the marker execute on the first and third test case.

Python3




# Importing the math and Pytest libraries
import math
import pytest
 
# Creating first test case
@pytest.mark.execute
def test_floor():
   num = 6
   assert num==math.floor(6.34532)
 
# Creating second test case
def test_equal():
   assert 50 == 49
 
# Creating third test case
@pytest.mark.execute
def test_difference():
   assert 99-43==57
 
# Creating fourth test case
def test_square_root():
   val=8
   assert val==math.sqrt(81)


Output

Now, we will run the following command with marker_name as execute as follows:

pytest main.py -k execute -v

Output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads