Group the test cases

Pytest has the feature to group the test cases by using python decorator “@pytest.mark” along with this we need to add the group name “@pytest.mark.group_name”.

How to use mark.

  • First we need to import pytest module to use this decorator.
  • Now we need to decorate on top of method name along with marker name as “@pytest.mark.mark_name”
  • After grouping all the test cases.Now by using -m marker_name argument we can execute required test cases. Command looks like “pytest -v -m marker_name”


In the below example we have two files which have 8 test cases. test_Group_Example1.py and test_Group_Example2.py In those classes we have totally 8 test cases which are grouped into two as a profile and feeds.



Run those two files using the below command.

pytest -v -m marker_name


Here we are running only test cases which are related to profile.

pytest -v -m profile

test_Group_Example1.py

import pytest


@pytest.mark.profile
def test_methodA():
    print("This is method A")


@pytest.mark.profile
def test_methodB():
    print("This is method B")


def test_Example1():
    print("This is Example1 method")


def test_Example2():
    print("This is Example2 method")




test_Group_Example2.py

import pytest


@pytest.mark.profile
def test_methodC():
    print("This is method C")


@pytest.mark.profile
def test_methodD():
    print("This is method D")


@pytest.mark.feeds
def test_Example3():
    print("This is Example3 method")


@pytest.mark.feeds
def test_Example4():
    print("This is Example4 method")


OutPut data

In the output we can see only 4 test cases are executed which are related to the profile group and remaining test cases which are related to the feed group are deselected.

admin@admin-MacBook grouptests % pytest -v -m profile
============================================================================ test session starts ============================================================================
platform darwin -- Python 3.8.3, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8
cachedir: .pytest_cache
rootdir: /Users/admin/Documents/Skill2Lead/Tutorials/pytest/grouptests
plugins: rerunfailures-9.0, allure-pytest-2.8.16, ordering-0.6
collected 8 items / 4 deselected / 4 selected                                                                                                                               

test_Group_Example1.py::test_methodA PASSED                                                                                                                           [ 25%]
test_Group_Example1.py::test_methodB PASSED                                                                                                                           [ 50%]
test_Group_Example2.py::test_methodC PASSED                                                                                                                           [ 75%]
test_Group_Example2.py::test_methodD PASSED                                                                                                                           [100%]

================================================================ 4 passed, 4 deselected in 0.07s ================================================================
admin@admin-MacBook grouptests % 




If we need to execute test cases which are related to the feeds group then run the below command.

pytest -v -m feeds