Skip to main content

运行和调试测试

介绍

¥Introduction

你可以运行单个测试、一组测试或所有测试。使用 --browser 标志可以在一个浏览器或多个浏览器上运行测试。默认情况下,测试以无头模式运行,这意味着运行测试时不会打开浏览器窗口,结果将显示在终端中。如果你愿意,你可以使用 --headed CLI 参数在有头模式下运行测试。

¥You can run a single test, a set of tests or all tests. Tests can be run on one browser or multiple browsers by using the --browser flag. By default, tests are run in a headless manner, meaning no browser window will be opened while running the tests and results will be seen in the terminal. If you prefer, you can run your tests in headed mode by using the --headed CLI argument.

你将学习

¥You will learn

运行测试

¥Running tests

命令行

¥Command Line

要运行测试,请使用 pytest 命令。这将默认在 Chromium 浏览器上运行你的测试。默认情况下,测试在无头模式下运行,这意味着运行测试时不会打开浏览器窗口,并且结果将在终端中看到。

¥To run your tests, use the pytest command. This will run your tests on the Chromium browser by default. Tests run in headless mode by default meaning no browser window will be opened while running the tests and results will be seen in the terminal.

pytest

在 Head 模式下运行测试

¥Run tests in headed mode

要在 head 模式下运行测试,请使用 --headed 标志。这将在运行测试时打开一个浏览器窗口,完成后浏览器窗口将关闭。

¥To run your tests in headed mode, use the --headed flag. This will open up a browser window while running your tests and once finished the browser window will close.

pytest --headed

在不同浏览器上运行测试

¥Run tests on different browsers

要指定要在哪个浏览器上运行测试,请使用 --browser 标志,后跟浏览器名称。

¥To specify which browser you would like to run your tests on, use the --browser flag followed by the name of the browser.

pytest --browser webkit

要指定多个浏览器来运行测试,请多次使用 --browser 标志,后跟每个浏览器的名称。

¥To specify multiple browsers to run your tests on, use the --browser flag multiple times followed by the name of each browser.

pytest --browser webkit --browser firefox

运行特定测试

¥Run specific tests

要运行单个测试文件,请传入要运行的测试文件的名称。

¥To run a single test file, pass in the name of the test file that you want to run.

pytest test_login.py

要运行一组测试文件,请传入要运行的测试文件的名称。

¥To run a set of test files, pass in the names of the test files that you want to run.

pytest tests/test_todo_page.py tests/test_landing_page.py

要运行特定测试,请传入要运行的测试的函数名称。

¥To run a specific test, pass in the function name of the test you want to run.

pytest -k test_add_a_todo_item

并行运行测试

¥Run tests in parallel

要并行运行测试,请使用 --numprocesses 标志,后跟要运行测试的进程数。我们建议使用一半的逻辑 CPU 核心。

¥To run your tests in parallel, use the --numprocesses flag followed by the number of processes you would like to run your tests on. We recommend half of logical CPU cores.

pytest --numprocesses 2

(假设已安装 pytest-xdist。更多信息请参阅 此处。)

¥(This assumes pytest-xdist is installed. For more information see here.)

更多信息,请参阅 Playwright Pytest 用法 或 Pytest 的 常规 CLI 用法 文档。

¥For more information, see Playwright Pytest usage or the Pytest documentation for general CLI usage.

调试测试

¥Debugging tests

由于 Playwright 使用 Python 运行,你可以使用你选择的调试器进行调试,例如使用 Visual Studio Code 中的 Python 扩展。Playwright 附带 Playwright Inspector,可让你单步执行 Playwright API 调用、查看其调试日志并探索 locators

¥Since Playwright runs in Python, you can debug it with your debugger of choice, e.g., with the Python extension in Visual Studio Code. Playwright comes with the Playwright Inspector which allows you to step through Playwright API calls, see their debug logs and explore locators.

要调试所有测试,请运行以下命令。

¥To debug all tests, run the following command.

PWDEBUG=1 pytest -s

要调试一个测试文件,请运行以下命令,后跟要调试的测试文件的名称。

¥To debug one test file, run the command followed by the name of the test file that you want to debug.

PWDEBUG=1 pytest -s test_example.py

要调试特定测试,请添加 -k,后跟要调试的测试的名称。

¥To debug a specific test, add -k followed by the name of the test that you want to debug.

PWDEBUG=1 pytest -s -k test_get_started_link

此命令将打开浏览器窗口以及 Playwright 检查器。你可以使用检查器顶部的逐步按钮来逐步完成测试。或者按下播放按钮,从头到尾运行测试。测试完成后,浏览器窗口将关闭。

¥This command will open up a Browser window as well as the Playwright Inspector. You can use the step over button at the top of the inspector to step through your test. Or press the play button to run your test from start to finish. Once the test has finished, the browser window will close.

调试时,你可以使用“选择定位器”按钮选择页面上的一个元素,并查看 Playwright 用于查找该元素的定位器。你还可以编辑定位器并查看它在浏览器窗口中实时高亮。使用“复制定位器”按钮将定位器复制到剪贴板,然后将其粘贴到测试中。

¥While debugging you can use the Pick Locator button to select an element on the page and see the locator that Playwright would use to find that element. You can also edit the locator and see it highlighting live on the Browser window. Use the Copy Locator button to copy the locator to your clipboard and then paste it into your test.

Playwright Inspector

查看我们的 调试指南,了解更多关于 Playwright 检查器 以及如何使用 浏览器开发者工具 进行调试的信息。

¥Check out our debugging guide to learn more about the Playwright Inspector as well as debugging with Browser Developer tools.

下一步是什么

¥What's next