0
0
PyTesttesting~3 mins

Why Custom markers in PyTest? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could run just the tests you want with a simple tag?

The Scenario

Imagine you have a big set of tests for your app. You want to run only the tests that check login features today, but you have to open each test file and find those tests manually.

The Problem

This manual way is slow and tiring. You might miss some tests or run the wrong ones. It's easy to make mistakes and waste time running tests you don't need right now.

The Solution

Custom markers let you tag tests with labels like 'login' or 'slow'. Then you can tell pytest to run only tests with those tags. This saves time and keeps your testing organized.

Before vs After
Before
def test_login_valid():
    # test code

def test_profile_update():
    # test code

# Manually run tests by picking them one by one
After
import pytest

@pytest.mark.login
def test_login_valid():
    # test code

@pytest.mark.profile
def test_profile_update():
    # test code

# Run tests with 'login' marker using: pytest -m login
What It Enables

You can quickly run focused groups of tests, making your testing faster and smarter.

Real Life Example

A developer fixes a bug in the payment system and runs only tests marked 'payment' to check the fix without waiting for unrelated tests.

Key Takeaways

Manual test selection is slow and error-prone.

Custom markers tag tests for easy grouping.

Run only needed tests quickly with markers.