Playwright + Python · Lesson 28 of 37

0/37 done

Module 28: Test Data

28. Data-Driven Testing with @pytest.mark.parametrize

16 minlesson

Your task

Implement generate_random_user_payload():

  1. Return {"email": f"user_{int(time.time())}@targetqa.com", "role": "USER"}

Write it in the editor, then press Check answer (Ctrl + Enter).

Parameterized Testing in pytest

python
import pytest

@pytest.mark.parametrize("username,password,expected_error", [
    ("invalid_user", "Secret123", "User not found"),
    ("admin@targetqa.com", "wrong_pass", "Invalid password"),
    ("", "Secret123", "Username required"),
])
def test_login_validation_errors(page, username, password, expected_error):
    page.goto("https://targetqa.com/login")
    page.get_by_label("Username").fill(username)
    page.get_by_label("Password").fill(password)
    page.get_by_role("button", name="Log In").click()
    expect(page.locator(".error-banner")).to_contain_text(expected_error)

Key Takeaways & Walkthrough Summary

Testing multiple input combinations with one test function is simple in Python. In this lesson, you will master @pytest.mark.parametrize, loading external JSON fixtures, and generating dynamic random data. Let's master test data!

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.

Common Doubts & Lesson Q&A

Can I parametrize tests with external JSON files?

Yes! You can load JSON data into a list of tuples and pass that list directly to @pytest.mark.parametrize.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip