Grouping automation tests using Pytest.mark

Pytest allows to group tests using markers. pytest.mark decorator is used to mark a test function with custom metadata like @pytest.mark.smoke

This is very handy when we want to run only a subset of tests like "smoke tests" to quickly verify if the changes made by the developer not breaking any major functionalities.

You can mark a test function with custom metadata like below:

def test_case_one():
    assert True

A test can have more than one marker, and also a marker can be on multiple tests. Please look at the below example:-

def test_case_two():
    assert True

To use a marker, we simply add annotation to tell pytest that test belongs to a particular group or multiple groups.

We will see below example using selenium webdriver and pytest : -

import pytest
from import expected_conditions as EC
from import WebDriverWait
from import By
from selenium import webdriver
import time

def setup(request):
    print("initiating chrome driver")
    driver = webdriver.Chrome(executable_path=r"C:/Users/HarryDev/Downloads/chromedriver_win32/chromedriver.exe")
    request.cls.driver = driver

    yield driver

class TestExample:

    def test_title(self):
        print("Verify title...")
        assert "Selenium Easy" in self.driver.title

    def test_content_text(self):
        print("Verify content on the page...")
        centertext = self.driver.find_element_by_css_selector('.tab-content .text-center').text
        assert "WELCOME TO SELENIUM EASY DEMO" == centertext

    def test_practicing(self):
        print("verifying exercise--")
        startpractisingBtn = self.driver.find_element_by_id('btn_basic_example')
        WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#basic .head')))

Now, let’s just run above tests that are marked with marker name using -m command line option like below :-

 pytest -v -m regression

pytest run single marker

If we want to run all the selenium automation tests except regression, we can do like below using 'not' :-

 pytest -v -m "not regression"

Now there may be a case where we want to execute selenium webdriver tests which are both 'regression' and 'smoke'. To do that, we should use command like below :-

 pytest -v -m "smoke and regression"

pytest run multiple markers

In the above case, it has picked only one test which is marked with both 'smoke' and 'regression'. Please see the below image.

NOTE: - If you are using expressions such as "smoke and regression" then both smoke and regression need to be marked for at least one test. If there are no tests found, it will just deselect all the tests and nothing will happen.

You may also use pytest.mark decorators with classes to apply markers to all of its test methods in class.

import pytest
class TestClass:
    def test_smoke1(self):
    def test_smoke2(self):
   def test_smoke3(self):
Selenium Tutorials: 


Hi ,
your tutorials are great.
Request you to provide complete tutorial on pytest and selenium with python.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.