Manuals / Python / Chapter 8

C · Testing · intermediate · Week 3–4 · Chapter 8 of 13

Fixtures & parametrization

@pytest.fixture for shared setup. @pytest.mark.parametrize for data-driven tests. conftest.py for shared fixtures.

Path progress
56%

Step 1 of 3

Fixtures

Fixture functions provide test data or clients. pytest injects by parameter name.

FixturesDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Fixturesdrag · tap →Try thisdrag · tap →Follow the dashed drag · tap →

Example

import pytest

@pytest.fixture
def sample_users():
    return [{"username": "a"}, {"username": "b"}]

def test_user_count(sample_users):
    assert len(sample_users) == 2

Do this now

Create sample_users fixture returning list of dicts. Use in 2 tests.

Was this step clear?
Chapter learning outcomes
  • @pytest.fixture
  • parametrize
  • conftest.py
  • Fixture scope

Clear these before you leave

Side quest

URL builder tests

Parametrize build_url with trailing slash edge cases.