Grouping related tests helps keep your tests organized and easy to find. It makes running and understanding tests simpler.
Grouping related tests in PyTest
import pytest @pytest.mark.group_name def test_example(): assert True # Or use classes to group tests class TestGroupName: def test_one(self): assert True def test_two(self): assert True
You can use @pytest.mark to tag tests with a group name.
Using classes groups tests inside one container without needing extra decorators.
import pytest @pytest.mark.login def test_login_success(): assert True @pytest.mark.login def test_login_failure(): assert False
TestMathOperations are grouped together.class TestMathOperations: def test_add(self): assert 1 + 1 == 2 def test_subtract(self): assert 2 - 1 == 1
This example shows two ways to group tests: using @pytest.mark and using classes.
Tests marked with 'calculator' belong to one group, and string tests are grouped in a class.
import pytest @pytest.mark.calculator def test_multiply(): assert 3 * 3 == 9 @pytest.mark.calculator def test_divide(): assert 10 / 2 == 5 class TestStringMethods: def test_upper(self): assert 'hello'.upper() == 'HELLO' def test_isupper(self): assert 'HELLO'.isupper() is True
Use descriptive group names to make tests easy to find.
You can run tests by group using pytest -m group_name.
Classes group tests logically and can share setup code with setup_method.
Grouping tests keeps your test suite organized and easier to manage.
Use @pytest.mark or classes to group related tests.
Running tests by group helps focus on specific features or fixes.
Practice
Solution
Step 1: Understand test grouping purpose
Grouping tests helps organize them logically by feature or behavior.Step 2: Identify the main benefit
This organization makes the test suite easier to maintain and understand.Final Answer:
It keeps the test suite organized and easier to manage -> Option AQuick Check:
Grouping = Organized tests [OK]
- Thinking grouping speeds up tests automatically
- Believing grouping removes need for assertions
- Assuming grouping disables parallel runs
Solution
Step 1: Recall pytest syntax for marking tests
pytest uses @pytest.mark.<groupname> to mark tests for grouping.Step 2: Match correct syntax
@pytest.mark.fast is the correct decorator to mark a test as 'fast'.Final Answer:
@pytest.mark.fast\ndef test_example(): pass -> Option CQuick Check:
Use @pytest.mark.groupname for grouping [OK]
- Using @pytest.group instead of @pytest.mark
- Using @pytest.tag which does not exist
- Incorrect decorator names like @pytest.group_mark
pytest -m "slow"?
import pytest
@pytest.mark.slow
def test_a():
assert True
@pytest.mark.fast
def test_b():
assert True
class TestGroup:
@pytest.mark.slow
def test_c(self):
assert True
def test_d(self):
assert True
Solution
Step 1: Understand pytest -m option
The -m option runs tests marked with the given marker, here 'slow'.Step 2: Identify tests marked 'slow'
test_a and test_c have @pytest.mark.slow, so they run. test_b and test_d do not.Final Answer:
test_a and test_c only -> Option AQuick Check:
-m "slow" runs tests marked slow [OK]
- Running all tests ignoring marks
- Including unmarked tests in filtered run
- Confusing class-level marks with method marks
TestMath?
class TestMath:
def test_add():
assert 1 + 1 == 2
def test_subtract(self):
assert 2 - 1 == 1
Solution
Step 1: Check method definitions inside test classes
Instance methods inside test classes must have self as first parameter.Step 2: Identify the error
test_add is missing self, so pytest will error or skip it.Final Answer:
test_add is missing the self parameter -> Option DQuick Check:
Test methods in classes need self [OK]
- Omitting self in test methods
- Thinking class names cannot start with Test
- Removing assertions from test methods
Solution
Step 1: Understand pytest options for running grouped tests
pytest uses -m to run tests marked with a specific marker like 'login'.Step 2: Identify correct command
pytest -m login runs all tests marked with @pytest.mark.login, including those inside classes.Final Answer:
pytest -m login -> Option BQuick Check:
-m runs marked tests, -k filters by test name [OK]
- Using -k which filters by test name, not marker
- Using nonexistent options like --group or --run-login
- Assuming class name filters tests automatically
Ready to prove it?
Pass the test and this pattern moves unknown
