How to group test cases in PyTest

When working with pytest, organizing and grouping test cases efficiently can make test execution smoother and debugging much easier. Pytest offers various ways to categorize and run tests selectively, whether you’re dealing with unit tests, integration tests, or end-to-end scenarios. This article explores multiple ways to group test cases in pytest and how to execute them effectively.

  • Improved Organization: A well-organized test suite is easier to navigate and understand, especially for larger projects.
  • Targeted Test Runs: You can run specific groups of tests, saving time and resources. For example, you might want to run only the tests related to a specific feature or module.
  • Faster Feedback: By running smaller, focused test groups, you can get quicker feedback on specific changes, accelerating the development cycle.

Pytest offers several flexible ways to group test cases:

Let’s look at all of them one by one

Pytest provides markers to categorize tests. We can define custom markers and use them to group test cases.

For example, let’s use three markers – group1, group2, and group3.

Syntax of assigning markers

@pytest.mark.${markerName}
def test_1():
    assert True

Here is our test_codekru.py file, which contains six test cases, each assigned a specific marker.

import pytest

@pytest.mark.group1
def test_1():
    assert True

@pytest.mark.group1
def test_2():
    assert True

@pytest.mark.group2
def test_3():
    assert True

@pytest.mark.group2
def test_4():
    assert True

@pytest.mark.group3
def test_5():
    assert True

@pytest.mark.group3
def test_6():
    assert True

We have assigned two test cases to each marker.

Now, let’s say that we want to execute only the group1 marker, so we can easily do so using the command below.

pytest -m group1

Here is the output after running the command –

Output of pytest -m command

As you can see, only two tests from the group1 marker were executed, while the rest were skipped.

We can use the “or” keyword to run multiple groups or markers simultaneously.

pytest -m "group1 or group2"

Below is the output of this command that we executed on our test_codekru.py file.

Run multiple markers at once

You might have noticed certain warnings along with results in our runs. So, how to avoid these warnings?

These warnings pop up because PyTest doesn’t recognize the markers. To fix this, we need to register our custom markers so PyTest knows what they are.

Create a pytest.ini file and define the markers in that file.

[pytest]
markers =
    group1: Marks a test as part of group1
    group2: Marks a test as part of group2
    group3: Marks a test as part of group3
define markers/groups in pytest.ini file

Now, rerun the “pytest -m "group1 or group2” command and observe the output.

rerunning the multiple groups command

As you can see, the warnings are gone now.

Just like we can run a specific group or marker, we can also exclude one if needed.

We can do so by using the “not” keyword with the marker/group name to exclude it from execution.

pytest -m "not group1"

The above command will run all markers except “group1”.

Exclude specific group

We can also organize our test cases into classes and run specific ones as needed. So, let’s rewrite our test_codekru.py file.

class TestLogin:
    def test_valid_login(self):
        assert True

    def test_invalid_login(self):
        assert True

class TestSignup:
    def test_valid_signup(self):
        assert True

    def test_invalid_signup(self):
        assert True

class TestLogout:
    def test_valid_logout(self):
        assert True

    def test_invalid_logout(self):
        assert True

Here, you can see that we grouped our test cases into classes, and now we will see how we can run specific classes.

We can run a specific class by using the below command –

pytest -k ${className}

To run the "TestLogin" class, use the following command:

pytest -k TestLogin
running specific class

We can see that only 2 cases were executed out of 6.

We can run multiple classes at the same time by using “or” between the class names.

So, let’s run TestLogin and TestSignup classes using the following command –

 pytest -k "TestLogin or TestSignup"

So, here 4 tests were executed that belonged to TestLogin and TestSignup classes.

Just like we can run a specific class, we can also exclude one using the below command –

 pytest -k "not TestLogin"

The above command will execute all tests except those in the “TestLogin” class.

exclude specific class in pytest

We can organize our tests by creating separate files and folders and grouping them based on functionality.

E.g., we created two files, test_login.py and test_signup.py, and have put login-specific cases in the test_login.py file and signup-specific cases in the test_signup.py file.

Below is our project structure –

Now, we can choose to run a specific folder or a specific file.

Run the below command to run a specific folder –

pytest tests/login
Run a specific folder

Run the below command to run a specific file –

pytest tests/login/test_login.py
Run specific file

This is it. We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *