Jiaxi Liu (Jesse)

Master’s Graduate

Software Engineer | Scalable APIs · Web Scraping · Data Integration · Code Quality & Refactoring

Back to Blog

pytest, Robot Framework, and Playwright: Fixtures, Keyword-Driven Testing, and E2E Tests

These tools solve different testing problems. pytest is a Python testing framework, Robot Framework is keyword-driven automation, and Playwright is modern web E2E testing.

pytest Discovery

pytest discovers:

  • test_*.py
  • *_test.py
  • functions starting with test_
def test_add():
    assert 1 + 2 == 3

Fixtures

Fixtures prepare test state and can clean up afterward.

import pytest
 
@pytest.fixture
def setup_env():
    print("prepare")
    yield
    print("cleanup")

Use a fixture by naming it as a test parameter.

def test_example(setup_env):
    assert True

Fixture Scope

Scope controls lifecycle:

  • function: once per test function
  • class: once per test class
  • module: once per file
  • session: once per test session

In real projects, shared fixtures usually live in conftest.py.

Robot Framework

Robot Framework uses keyword-driven tests that are easier for business stakeholders to read.

*** Test Cases ***
Login With Valid User
    Open Browser To Login Page
    Login With Valid Credentials    ${USERNAME}    ${PASSWORD}
    Page Should Contain Element    id=dashboard

It is useful for acceptance tests and workflow automation.

Playwright

Playwright is suited for modern web E2E.

test("user can login", async ({ page }) => {
  await page.goto("/login");
  await page.getByLabel("Username").fill("jesse");
  await page.getByLabel("Password").fill("password");
  await page.getByRole("button", { name: "Login" }).click();
  await expect(page.getByTestId("dashboard")).toBeVisible();
});

Reducing Flaky Tests

Avoid fixed sleeps as a universal solution.

Better options:

  • Wait for elements to be visible
  • Wait for buttons to be clickable
  • Wait for requests
  • Wait for URL changes
  • Prepare data through API or database
  • Keep UI tests focused on core paths

Stable tests wait for explicit conditions, not arbitrary time.