Running Playwright Automated Tests and Generating HTML Reports with Pytest
This article demonstrates how to use Playwright with Pytest to run automated browser tests, generate detailed HTML reports, and provides best‑practice guidelines for building a comprehensive, maintainable test suite for web applications, covering setup and teardown procedures.
Using Playwright to run automated tests and generate reports helps you better understand test results and issues.
Playwright test execution and report generation example:
import pytest
from playwright.sync_api import sync_playwright
# 定义测试用例类
class TestWebsite:
def setup_method(self, method):
# 在每个测试方法之前启动浏览器
self.playwright = sync_playwright()
self.browser = self.playwright.chromium.launch()
self.page = self.browser.new_page()
def teardown_method(self, method):
# 在每个测试方法之后关闭浏览器
self.browser.close()
self.playwright.stop()
def test_login(self):
self.page.goto('https://example.com/login')
# 执行登录操作
self.page.fill('input#username', 'testuser')
self.page.fill('input#password', 'password')
self.page.click('button#login-button')
# 验证登录后的页面
assert self.page.title() == 'Dashboard'
assert self.page.url == 'https://example.com/dashboard'
def test_search(self):
self.page.goto('https://example.com/dashboard')
# 执行搜索操作
self.page.fill('input#search-input', 'playwright')
self.page.press('input#search-input', 'Enter')
# 验证搜索结果
assert self.page.title() == 'Search Results'
assert 'playwright' in self.page.text_content('div#search-results')
# 运行测试并生成报告
if __name__ == '__main__':
pytest.main(['-v', '--html=report.html'])In this example we use the Pytest framework, define a TestWebsite class, and employ setup_method and teardown_method to start and stop the browser for each test. The test_login method simulates a login flow and verifies the page title and URL, while test_search performs a search and checks the results.
Running the script with pytest -v --html=report.html produces an report.html file in the current directory, which can be opened in a browser to view detailed test results and logs.
To build a comprehensive automated test suite, consider the following recommendations:
Design a test strategy: Identify application features, user roles, input variations, and boundary conditions to determine coverage.
Write clear test cases: Ensure each case has a defined goal, expected outcome, and meaningful naming.
Use diverse test data: Include normal, edge‑case, and error data, possibly via data‑driven or parameterized tests.
Handle test environments: Automate setup and teardown of databases, mock services, and configuration.
Apply appropriate assertions: Verify results with equality, containment, exception handling, and provide detailed failure messages.
Organize and manage test code: Use modular structures, version control, and code reviews for maintainability.
Execute tests in parallel: Leverage distributed frameworks or cloud platforms to speed up large suites.
Generate detailed reports: Use HTML, JUnit XML, or other formats to present results, failures, and logs.
Integrate with CI/CD: Trigger tests on each commit using tools like Jenkins or GitHub Actions to catch regressions early.
Maintain and update regularly: Refactor tests as the application evolves and keep the suite aligned with new features.
Automated testing is an ongoing process that requires continuous learning and improvement.
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.