Marks

Introduction

As automation projects grow, running every test every time becomes inefficient. Sometimes you only want to execute a small group of important tests, such as smoke tests, regression tests, or sanity tests.

PyTest provides Marks, which allow you to categorize tests using labels. Once tests are marked, you can run only the tests you need without executing the entire test suite.

In this tutorial, you’ll learn what PyTest Marks are, how they work, and how to use them in Selenium automation projects.


What are PyTest Marks?

PyTest Marks are labels that are attached to test functions.

These labels help organize tests into different categories.

For example:

                All Tests

                    │

      ┌─────────────┼─────────────┐
      │             │             │

   Smoke         Regression     Sanity

      │             │             │

      ▼             ▼             ▼

 Run Only      Run Only      Run Only
Smoke Tests   Regression    Sanity Tests

Each mark represents a logical group of tests.


Why Use Marks?

Marks help you:

  • Execute only selected tests.

  • Organize large automation suites.

  • Separate smoke, regression, and sanity tests.

  • Save execution time.

  • Improve test management.


How Marks Work

PyTest provides the @pytest.mark decorator.

Example:

@pytest.mark.smoke

This attaches the smoke label to the test.

You can then execute only smoke tests using:

pytest -s -m smoke

PyTest skips all tests that are not marked as smoke.


Example

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By


# Topic: 41. Advanced PyTest - Marks
# Practice site: https://www.testmuai.com/selenium-playground/checkbox-demo
# Run: pytest -s -m smoke 41_examples/test_02_marks.py
#
# Marks tag tests so you can select subsets, for example smoke or regression.


@pytest.mark.smoke
def test_marks_smoke_checkbox():
    driver = webdriver.Chrome()

    try:
        driver.get("https://www.testmuai.com/selenium-playground/checkbox-demo")
        checkbox = driver.find_element(By.ID, "isAgeSelected")
        checkbox.click()
        assert checkbox.is_selected()
    finally:
        driver.quit()

Understanding the Code

Import Required Libraries

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

These modules are required to:

  • Create PyTest tests.

  • Apply test marks.

  • Launch the Chrome browser.

  • Locate web elements.


Apply the Smoke Mark

@pytest.mark.smoke

The @pytest.mark.smoke decorator assigns the smoke label to this test.

This allows PyTest to include or exclude the test during execution based on the selected mark.


Define the Test Function

def test_marks_smoke_checkbox():

This is a standard PyTest test function.

Because it is marked as smoke, it becomes part of the smoke test suite.


Launch the Browser

driver = webdriver.Chrome()

Starts a new Chrome browser session.


Open the Practice Website

driver.get(
    "https://www.testmuai.com/selenium-playground/checkbox-demo"
)

Navigates to the Checkbox Demo page.


Locate and Click the Checkbox

checkbox = driver.find_element(
    By.ID,
    "isAgeSelected"
)

checkbox.click()

Locates the checkbox using its ID and clicks it.


Verify the Checkbox Selection

assert checkbox.is_selected()

The assert statement verifies that the checkbox is selected.

If the checkbox is not selected, PyTest marks the test as failed.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.


Registering Custom Marks

Before using a custom mark such as smoke, it should be registered with PyTest.

Example:

def pytest_configure(config):
    config.addinivalue_line(
        "markers",
        "smoke: smoke tests"
    )

This tells PyTest that smoke is a valid custom marker.

In larger projects, custom markers are often registered in conftest.py or pytest.ini.

Note: The pytest_configure() function is a PyTest Hook. You’ll learn about Hooks in detail later in this section.


Running Smoke Tests

To execute only smoke tests, use:

pytest -s -m smoke

PyTest runs only the tests marked with:

@pytest.mark.smoke

All other tests are skipped.


Practical Example

Suppose an e-commerce website contains 500 automated tests.

Before every deployment, the QA team runs only the smoke tests to verify that the application’s critical functionality is working.

Using marks makes this possible with a single command.


Automation Testing Example

Consider an online banking application.

Important scenarios such as:

  • User Login

  • Account Balance

  • Fund Transfer

are marked as smoke tests.

These tests execute first after every new build to ensure that the core features are functioning correctly.


Real-World Example

PyTest Marks are commonly used in:

  • Smoke testing

  • Regression testing

  • Sanity testing

  • API automation

  • Selenium frameworks

  • CI/CD pipelines

  • Enterprise automation projects

Most professional automation frameworks use marks to organize and execute tests efficiently.


Advantages of Marks

  • Organize tests into logical groups.

  • Execute only selected tests.

  • Reduce execution time.

  • Improve test maintenance.

  • Simplify CI/CD pipelines.

  • Support large automation frameworks.


Common Mistakes Beginners Make

Forgetting to Register Custom Marks

Custom marks such as smoke should be registered.

Otherwise, PyTest may display an Unknown Mark Warning.


Marking Every Test as Smoke

Smoke tests should contain only the application’s critical functionality.

Avoid marking every test as a smoke test.


Using Inconsistent Mark Names

Use meaningful and consistent names such as:

  • smoke

  • regression

  • sanity

Avoid creating unnecessary or confusing marker names.


Forgetting to Use the -m Option

Simply marking a test does not change execution.

Use:

pytest -m smoke

to execute only smoke tests.


Best Practices

  • Use marks to organize related tests.

  • Register custom marks properly.

  • Keep smoke tests fast and reliable.

  • Use meaningful marker names.

  • Combine marks with Selenium to build scalable automation frameworks.


Conclusion

PyTest Marks provide an easy way to organize Selenium tests into logical groups such as smoke, regression, and sanity. By assigning marks with @pytest.mark, testers can execute only the required subset of tests, making automation faster, more efficient, and easier to manage. Marks are an essential feature of professional Selenium + PyTest frameworks.


Frequently Asked Questions (FAQs)

What are PyTest Marks?

PyTest Marks are labels attached to test functions that group related tests together.


How do you create a smoke test?

Use:

@pytest.mark.smoke

above the test function.


How do I run only smoke tests?

Use:

pytest -s -m smoke

PyTest executes only tests marked as smoke.


Why should custom marks be registered?

Registering custom marks prevents Unknown Mark Warnings and improves project organization.


Where are PyTest Marks commonly used?

They are commonly used in smoke testing, regression testing, sanity testing, CI/CD pipelines, Selenium frameworks, and enterprise automation projects.


Key Takeaways

  • @pytest.mark is used to categorize tests.

  • Marks allow selected groups of tests to be executed.

  • Use pytest -m to run marked tests.

  • Register custom marks to avoid warnings.

  • Marks improve test organization and execution efficiency.

  • PyTest Marks are widely used in professional Selenium automation frameworks.