Playwright + Python · Lesson 27 of 37
Module 27: Test Lifecycle
Your task
Implement safe_context_runner(context_creator, test_fn):
Write it in the editor, then press Check answer (Ctrl + Enter).
yield Fixtures for Teardownimport pytest
@pytest.fixture
def api_client(playwright):
# Setup:
request_context = playwright.request.new_context(base_url="https://api.targetqa.com")
yield request_context
# Teardown:
request_context.dispose()Managing test execution lifecycles guarantees that failed tests do not leave unclosed files or video recordings behind. In this lesson, you will learn to structure pytest yield fixtures for clean setup and teardown. Let's master test lifecycles!
Part of the Playwright + Python course (Playwright with Python). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
function (default: runs per test), class (runs per class), module (runs per file), and session (runs once per entire test suite).
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___