0
0
PyTesttesting~15 mins

Why markers categorize and control tests in PyTest - Automation Benefits in Action

Choose your learning style9 modes available
Use pytest markers to categorize and selectively run tests
Preconditions (2)
Step 1: Create three test functions: test_login, test_logout, test_profile
Step 2: Add a marker 'smoke' to test_login and test_logout
Step 3: Add a marker 'regression' to test_profile
Step 4: Run pytest with the option to only run tests marked 'smoke'
Step 5: Observe that only test_login and test_logout run
Step 6: Run pytest with the option to skip tests marked 'smoke'
Step 7: Observe that only test_profile runs
✅ Expected Result: Tests run according to the markers specified in the pytest command line options
Automation Requirements - pytest
Assertions Needed:
Verify that tests with 'smoke' marker run when selected
Verify that tests with 'smoke' marker are skipped when excluded
Best Practices:
Use @pytest.mark.<name> to mark tests
Use pytest command line options -m to select or exclude markers
Keep markers descriptive and consistent
Automated Solution
PyTest
import pytest

@pytest.mark.smoke
 def test_login():
     assert True

@pytest.mark.smoke
 def test_logout():
     assert True

@pytest.mark.regression
 def test_profile():
     assert True

# To run smoke tests only, use command:
# pytest -m smoke

# To skip smoke tests, use command:
# pytest -m "not smoke"

This script defines three test functions. Two are marked with @pytest.mark.smoke and one with @pytest.mark.regression. Markers help group tests.

Using pytest -m smoke runs only tests marked 'smoke'. Using pytest -m "not smoke" skips those tests.

This shows how markers categorize tests and control which tests run.

Common Mistakes - 3 Pitfalls
Not registering custom markers in pytest.ini
Using markers inconsistently or with typos
Running pytest without specifying markers when needed
Bonus Challenge

Add a new marker 'slow' to one test and run tests excluding 'slow' tests

Show Hint