Skip to main content

运行和调试测试

介绍

¥Introduction

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

¥You can run a single test, a set of tests or all tests. Tests can be run on different browsers. 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 headless test run parameter.

你将学习

¥You will learn

运行测试

¥Running tests

运行所有测试

¥Run all tests

使用以下命令运行所有测试。

¥Use the following command to run all tests.

dotnet test

在 Head 模式下运行测试

¥Run tests in headed mode

使用以下命令在头模式下运行测试,并为每个测试打开一个浏览器窗口。

¥Use the following command to run your tests in headed mode opening a browser window for each test.

HEADED=1 dotnet test

在不同浏览器上运行测试浏览器:浏览器环境

¥Run tests on different browsers: Browser env

通过 BROWSER 环境变量指定你希望在哪个浏览器上运行测试。

¥Specify which browser you would like to run your tests on via the BROWSER environment variable.

BROWSER=webkit dotnet test

在不同浏览器上运行测试浏览器:启动配置

¥Run tests on different browsers: launch configuration

通过调整启动配置选项来指定你希望在哪个浏览器上运行测试:

¥Specify which browser you would like to run your tests on by adjusting the launch configuration options:

dotnet test -- Playwright.BrowserName=webkit

要在多个浏览器或配置上运行测试,你需要多次调用 dotnet test 命令。你可以在那里指定 BROWSER 环境变量,或通过 runsettings 文件设置 Playwright.BrowserName

¥To run your test on multiple browsers or configurations, you need to invoke the dotnet test command multiple times. There you can then either specify the BROWSER environment variable or set the Playwright.BrowserName via the runsettings file:

dotnet test --settings:chromium.runsettings
dotnet test --settings:firefox.runsettings
dotnet test --settings:webkit.runsettings
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<Playwright>
<BrowserName>chromium</BrowserName>
</Playwright>
</RunSettings>

有关更多信息,请参阅 Microsoft 文档中的 选择性单元测试

¥For more information see selective unit tests in the Microsoft docs.

运行特定测试

¥Run specific tests

要运行单个测试文件,请使用筛选标志,后跟要运行的测试的类名。

¥To run a single test file, use the filter flag followed by the class name of the test you want to run.

dotnet test --filter "ExampleTest"

要运行一组测试文件,请使用筛选标志,后跟要运行的测试的类名。

¥To run a set of test files, use the filter flag followed by the class names of the tests you want to run.

dotnet test --filter "ExampleTest1|ExampleTest2"

要运行具有特定标题的测试,请使用过滤标志,后跟 Name~ 和测试标题。

¥To run a test with a specific title use the filter flag followed by Name~ and the title of the test.

dotnet test --filter "Name~GetStartedLink"

使用多个工作线程运行测试:

¥Run tests with multiple workers:

dotnet test -- MSTest.Parallelize.Workers=5

调试测试

¥Debugging Tests

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

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

PWDEBUG=1 dotnet test

debugging tests with 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