Pytest allows to group tests using markers.
pytest.mark decorator is used to mark a test function with custom metadata like
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:
@pytest.mark.smoke 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:-
@pytest.mark.smoke @pytest.mark.regression 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 selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium import webdriver import time @pytest.fixture(scope="class") 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 driver.get("http://seleniumeasy.com/test") driver.maximize_window() yield driver driver.close() @pytest.mark.usefixtures("setup") class TestExample: @pytest.mark.smoke def test_title(self): print("Verify title...") assert "Selenium Easy" in self.driver.title @pytest.mark.smoke 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 @pytest.mark.regression @pytest.mark.smoke def test_practicing(self): print("verifying exercise--") startpractisingBtn = self.driver.find_element_by_id('btn_basic_example') startpractisingBtn.click() time.sleep(10) 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
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"
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 @pytest.mark.smoke class TestClass: def test_smoke1(self): pass def test_smoke2(self): pass def test_smoke3(self): pass