27. Test Lifecycle Management, Teardown Guarantees & Scope

pytest yield Fixtures for Teardown

import 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()

Task

  • Implement safe_context_runner(context_creator, test_fn):
  • 1. context = context_creator().
  • 2. In try block, call test_fn(context).
  • 3. In finally block, call context.close().
πŸ’‘Common doubts
Did you like the problem?
Lesson 27 of 37
Python
Loading code editor…
Next