Markers for categorization in Selenium Python - Build an Automation Script
import pytest from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class LoginPage: def __init__(self, driver): self.driver = driver self.email_input = (By.ID, 'email') self.password_input = (By.ID, 'password') self.login_button = (By.ID, 'loginBtn') def open(self): self.driver.get('https://example.com/login') def login(self, email, password): self.driver.find_element(*self.email_input).send_keys(email) self.driver.find_element(*self.password_input).send_keys(password) self.driver.find_element(*self.login_button).click() class DashboardPage: def __init__(self, driver): self.driver = driver self.heading = (By.TAG_NAME, 'h1') def get_heading_text(self): return self.driver.find_element(*self.heading).text @pytest.mark.smoke @pytest.mark.login def test_login_success(): driver = webdriver.Chrome() wait = WebDriverWait(driver, 10) login_page = LoginPage(driver) dashboard_page = DashboardPage(driver) try: login_page.open() login_page.login('validuser@example.com', 'ValidPass123!') wait.until(EC.url_to_be('https://example.com/dashboard')) assert driver.current_url == 'https://example.com/dashboard', 'URL after login should be dashboard URL' wait.until(EC.visibility_of_element_located(dashboard_page.heading)) heading_text = dashboard_page.get_heading_text() assert heading_text == 'Welcome to your dashboard', f'Expected heading text but got "{heading_text}"' finally: driver.quit()
This test uses pytest markers @pytest.mark.smoke and @pytest.mark.login to categorize it as a smoke test and login test. This helps to run specific groups of tests easily.
The Page Object Model separates page details from the test logic. LoginPage handles login page actions, and DashboardPage handles dashboard page elements.
We use explicit waits with WebDriverWait and expected_conditions to wait for the URL to change and the dashboard heading to appear. This avoids flaky tests caused by timing issues.
Assertions check the URL and heading text with clear messages to help understand failures.
The finally block ensures the browser closes even if the test fails.
Now add data-driven testing with 3 different sets of valid login credentials using pytest parametrize.
Practice
Solution
Step 1: Understand what markers do
Markers are used to label tests so you can group or select them easily.Step 2: Identify the main purpose
This helps run only certain tests or skip some, improving test control.Final Answer:
To label and organize tests for selective running -> Option BQuick Check:
Markers = Organize tests [OK]
- Thinking markers speed up writing tests
- Believing markers fix test errors automatically
- Confusing markers with report generation
smoke to a test function in pytest?Solution
Step 1: Recall pytest marker syntax
Markers use dot notation: @pytest.mark.marker_name above the test.Step 2: Match syntax to options
Only @pytest.mark.smoke\ndef test_example(): pass uses correct dot notation without extra symbols.Final Answer:
@pytest.mark.smoke\ndef test_example(): pass -> Option AQuick Check:
Marker syntax = dot notation [OK]
- Using parentheses instead of dot notation
- Using brackets or arrows incorrectly
- Missing the @ symbol before pytest.mark
pytest -m smoke?
@pytest.mark.smoke def test_login(): pass @pytest.mark.regression def test_checkout(): pass def test_search(): pass
Solution
Step 1: Understand pytest -m option
The -m option runs tests with the given marker only.Step 2: Identify tests with 'smoke' marker
Onlytest_loginhas @pytest.mark.smoke.Final Answer:
Only test_login -> Option AQuick Check:
pytest -m smoke runs smoke tests only [OK]
- Running all tests ignoring markers
- Confusing marker names
- Assuming unmarked tests run with any marker
pytest -m regression. What is the error?
@pytest.mark.regresion def test_payment(): pass
Solution
Step 1: Check marker spelling
The marker used is 'regresion' but command uses 'regression'.Step 2: Understand effect of misspelling
pytest -m regression won't find tests marked 'regresion', so test is skipped.Final Answer:
Marker name is misspelled -> Option CQuick Check:
Marker spelling must match exactly [OK]
- Typos in marker names
- Forgetting @ before pytest.mark
- Assuming pytest runs all marked tests without -m
slow. Which pytest command should you use?Solution
Step 1: Understand pytest marker expressions
To exclude tests with a marker, use -m "not marker_name" syntax.Step 2: Match command to requirement
pytest -m "not slow" uses "not slow" to run all tests except those marked slow.Final Answer:
pytest -m "not slow" -> Option DQuick Check:
Use -m "not marker" to exclude tests [OK]
- Using -m marker to run only those tests
- Using unsupported --skip or --exclude options
- Not quoting the expression with spaces
Ready to prove it?
Pass the test and this pattern moves unknown
