Why do testers use markers in pytest?
Think about how you might run only some tests instead of all.
Markers help testers organize tests into groups. This allows running only specific groups, like slow tests or database tests, making testing more efficient.
What will be the output when running pytest with the marker @pytest.mark.slow and using pytest -m slow?
import pytest @pytest.mark.slow def test_example(): assert True def test_fast(): assert True
Using -m slow runs tests marked as slow only.
The command pytest -m slow runs only tests marked with @pytest.mark.slow. Other tests are skipped.
Which assertion correctly checks if a test function has the marker database applied in pytest?
import pytest def test_sample(): pass # Assume test_sample is marked with @pytest.mark.database
Markers are stored in a list under pytestmark attribute.
Markers are stored as objects in the pytestmark list. Checking their name attribute confirms presence.
A test marked with @pytest.mark.skipif(True, reason='skip') is still running. What is the likely cause?
@pytest.mark.skipif(True, reason='skip') def test_skip(): assert True
Pytest only runs functions starting with test_.
If the function name does not start with test_, pytest ignores the marker and runs the function as normal.
How can you run pytest to execute tests marked as api but skip those also marked as slow?
Use logical operators and, not in marker expressions.
The expression api and not slow runs tests marked with api but excludes those also marked slow.