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.

你将学习

运行测试

🌐 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 检查器,它允许你逐步执行 Playwright API 调用,查看它们的调试日志,并探索定位器

🌐 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