Integrating Playwright with unittest or pytest for Efficient Automated Testing
This article explains how to combine Playwright with Python's unittest or pytest frameworks to build robust test suites, design data‑driven tests, run tests in parallel or distributed environments, and integrate the workflow into CI/CD pipelines such as GitHub Actions and Jenkins.
Introduction: As web applications become increasingly complex, automated testing is crucial for quality assurance, and integrating Playwright with existing development and CI/CD processes ensures reliable releases.
1. Building test suites with unittest or pytest
Both unittest and pytest are popular Python testing frameworks that can be combined with Playwright for organized and maintainable automation.
import unittest
from playwright.sync_api import sync_playwright
class TestExample(unittest.TestCase):
def setUp(self):
self.playwright = sync_playwright().start()
self.browser = self.playwright.chromium.launch(headless=False)
self.page = self.browser.new_page()
def test_title(self):
self.page.goto("https://example.com")
self.assertEqual(self.page.title(), "Example Domain")
def tearDown(self):
self.browser.close()
self.playwright.stop()
if __name__ == "__main__":
unittest.main()Using pytest simplifies the code further:
def test_example(page):
page.goto("https://example.com")
assert page.title() == "Example Domain"2. Data‑driven testing
Parameterizing tests with pytest allows the same test case to run with multiple inputs, improving coverage.
import pytest
@pytest.mark.parametrize("query", ["hello", "world"])
def test_search(page, query):
page.goto("https://google.com")
page.fill('input[name="q"]', query)
page.press('input[name="q"]', 'Enter')
assert query in page.title()3. Parallel execution and distributed testing
Install the pytest‑xdist plugin to run tests concurrently across CPU cores.
pip install pytest-xdistRun tests in parallel:
pytest -n autoFor distributed testing across browsers and platforms, services like Sauce Labs or BrowserStack can be combined with Playwright.
4. CI/CD integration
Integrate Playwright tests into CI pipelines to execute automatically on each commit.
GitHub Actions example (create .github/workflows/playwright.yml ):
name: Playwright Tests
on:
push:
branches:
- main
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- run: npm ci
- run: npx playwright install
- run: npx playwright testJenkins pipeline example:
pipeline {
agent any
stages {
stage('Install Dependencies') {
steps {
sh 'npm ci'
}
}
stage('Run Tests') {
steps {
sh 'npx playwright install'
sh 'npx playwright test'
}
}
}
}Conclusion: By following these guidelines, you can build comprehensive Playwright test suites, apply data‑driven and parallel testing techniques, and seamlessly embed automated tests into CI/CD workflows for faster, more reliable software delivery.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.