Module 5 ยท Python for SDETs

Test data
setup & teardown

Every test starts from a state you control โ€” and leaves no mess behind

reset baseline arrange via API yield teardown factory fixtures
AI with Rufat
The flakiness you can't reproduce

Tests that share state fail in ways you can't reproduce

๐Ÿ”€

Order-dependent

test B only passes if test A ran first โ€” reorder and it breaks.

๐Ÿ‘ป

Leftover data

yesterday's order or user lingers and skews today's assertion.

๐ŸŽฒ

Green then red

the same test passes solo, fails in the suite โ€” the classic flake.

๐ŸŽฏThe fix: each test owns its state โ€” start from a known baseline, set up exactly what it needs, clean up after.
AI with Rufat
Known slate, every time

Reset to a known baseline

POST /api/reset
โ†’
seed data restored โœ…
โ†’
your test runs on a clean slate
@pytest.fixture
def reset_db(base_url):
requests.post(f"{base_url}/api/reset")
yield # test runs here, on fresh data
AI with Rufat
Arrange via the API, not the UI

Need a logged-in user with an order? Don't click through signup โ†’ login โ†’ checkout. POST the data straight in โ€” one fast, reliable call โ€” then test the thing you actually care about.

โšกAPI setup is faster and far less flaky than driving the UI to build state.
# arrange: one API call, not 6 UI clicks
order = api.post(
f"{base_url}/api/orders",
json={"email": user, "items": [โ€ฆ]},
).json()
# act + assert on what matters
assert order["total"] == pytest.approx(59.98)
AI with Rufat
Set up before yield, clean up after
@pytest.fixture
def temp_product(api, base_url):
# setup
p = api.post(โ€ฆ/products).json()
yield p # hand it to the test
# teardown โ€” always runs
api.delete(f"โ€ฆ/products/{p['id']}")
โ™ป๏ธCode before yield is setup; code after is teardown โ€” and it runs even if the test fails.
๐ŸญNeed several? A factory fixture returns a function the test calls N times โ€” each with a unique name, and reset_db reseeds it all away after.
AI with Rufat
โ™ป๏ธ

Own your
test state

Reset the baseline, arrange through the API, and let fixtures clean up

๐Ÿงช
Practice
A reset_db fixture + arrange an order via the API and assert its total
๐Ÿญ
Factory
A factory fixture that makes N products, each with a unique name
โžก๏ธ
Module 6
UI automation โ€” drive the browser with Playwright for Python
AI with Rufat
โ† / โ†’ ยท space