docs(dotnet): add docs about MSTest (#15526)
This commit is contained in:
parent
5fc637e44a
commit
342c971169
|
|
@ -13,7 +13,12 @@ isolated between the tests.
|
|||
|
||||
<!-- TOC -->
|
||||
|
||||
## Creating an NUnit project
|
||||
## NUnit
|
||||
|
||||
Playwright provides base classes to write tests with NUnit via the [`Microsoft.Playwright.NUnit`](https://www.nuget.org/packages/Microsoft.Playwright.NUnit) package.
|
||||
|
||||
|
||||
### Creating an NUnit project
|
||||
|
||||
```bash
|
||||
# Create a new project
|
||||
|
|
@ -22,17 +27,14 @@ cd PlaywrightTests
|
|||
# Add the required reference
|
||||
dotnet add package Microsoft.Playwright.NUnit
|
||||
dotnet build
|
||||
# Install the required pre-requisites
|
||||
playwright install
|
||||
# Install the required browsers and operating system dependencies
|
||||
pwsh bin\Debug\netX\playwright.ps1 install --with-deps
|
||||
```
|
||||
|
||||
Create a PageTests.cs file.
|
||||
Modify the UnitTest1.cs:
|
||||
|
||||
```csharp
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Playwright.NUnit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace PlaywrightTests;
|
||||
|
||||
|
|
@ -40,17 +42,18 @@ namespace PlaywrightTests;
|
|||
public class MyTest : PageTest
|
||||
{
|
||||
[Test]
|
||||
public async Task ShouldAdd()
|
||||
async public Task ShouldHaveTheCorrectSlogan()
|
||||
{
|
||||
int result = await Page.EvaluateAsync<int>("() => 7 + 3");
|
||||
Assert.AreEqual(10, result);
|
||||
await Page.GotoAsync("https://playwright.dev");
|
||||
await Expect(Page.Locator("text=enables reliable end-to-end testing for modern web apps")).ToBeVisibleAsync();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ShouldMultiply()
|
||||
public async Task ShouldHaveTheCorrectTitle()
|
||||
{
|
||||
int result = await Page.EvaluateAsync<int>("() => 7 * 3");
|
||||
Assert.AreEqual(21, result);
|
||||
await Page.GotoAsync("https://playwright.dev");
|
||||
var title = Page.Locator(".navbar__inner .navbar__title");
|
||||
await Expect(title).ToHaveTextAsync("Playwright");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
@ -63,32 +66,34 @@ dotnet test
|
|||
|
||||
Run your tests against WebKit
|
||||
|
||||
Windows
|
||||
```bash tab=bash-bash lang=csharp
|
||||
BROWSER=webkit dotnet test
|
||||
```
|
||||
|
||||
```bash
|
||||
```batch tab=bash-batch lang=csharp
|
||||
set BROWSER=webkit
|
||||
dotnet test
|
||||
```
|
||||
|
||||
Linux & Mac
|
||||
|
||||
```bash
|
||||
BROWSER=webkit dotnet test
|
||||
```powershell tab=bash-powershell lang=csharp
|
||||
$env:BROWSER="webkit"
|
||||
dotnet test
|
||||
```
|
||||
|
||||
Run your tests with GUI
|
||||
|
||||
Window
|
||||
```bash tab=bash-bash lang=csharp
|
||||
HEADED=1 dotnet test
|
||||
```
|
||||
|
||||
```bash
|
||||
```batch tab=bash-batch lang=csharp
|
||||
set HEADED=1
|
||||
dotnet test
|
||||
```
|
||||
|
||||
Linux & Mac
|
||||
|
||||
```bash
|
||||
HEADED=1 dotnet test
|
||||
```powershell tab=bash-powershell lang=csharp
|
||||
$env:HEADED="1"
|
||||
dotnet test
|
||||
```
|
||||
|
||||
You can also choose specifically which tests to run, using the [filtering capabilities](https://docs.microsoft.com/en-us/dotnet/core/testing/selective-unit-tests?pivots=nunit):
|
||||
|
|
@ -97,17 +102,17 @@ You can also choose specifically which tests to run, using the [filtering capabi
|
|||
dotnet test --filter "Name~ShouldAdd"
|
||||
```
|
||||
|
||||
## Running NUnit tests in Parallel
|
||||
### Running NUnit tests in Parallel
|
||||
|
||||
By default NUnit will run all test files in parallel, while running tests inside each file sequentially. It will create as many processes as there are cores on the host system. You can adjust this behavior using the NUnit.NumberOfTestWorkers parameter.
|
||||
|
||||
For CPU-bound tests, we recommend using as many workers as there are cores on your system, divided by 2. For IO-bound tests you can use as many workers as you have cores.
|
||||
|
||||
## Using Verbose API Logs with NUnit
|
||||
### Using Verbose API Logs
|
||||
|
||||
When you have enabled the [verbose API log](./debug.md#verbose-api-logs), via the `DEBUG` environment variable, you will see the messages in the standard error stream. In NUnit, within Visual Studio, that will be the `Tests` pane of the `Output` window. It will also be displayed in the `Test Log` for each test.
|
||||
|
||||
## Using the .runsettings file
|
||||
### Using the .runsettings file
|
||||
|
||||
When running tests from Visual Studio, you can take advantage of the `.runsettings` file.
|
||||
|
||||
|
|
@ -135,9 +140,151 @@ If you want to enable debugging, you can set the `DEBUG` variable to `pw:api` as
|
|||
</RunSettings>
|
||||
```
|
||||
|
||||
## Base NUnit classes for Playwright
|
||||
### Base NUnit classes for Playwright
|
||||
|
||||
There are few base classes available to you in Microsoft.Playwright.NUnit namespace:
|
||||
There are few base classes available to you in `Microsoft.Playwright.NUnit` namespace:
|
||||
|
||||
|Test |Description|
|
||||
|--------------|-----------|
|
||||
|PageTest |Each test gets a fresh copy of a web [Page] created in its own unique [BrowserContext]. Extending this class is the simplest way of writing a fully-functional Playwright test.<br></br><br></br>Note: You can override the `ContextOptions` method in each test file to control context options, the ones typically passed into the [`method: Browser.newContext`] method. That way you can specify all kinds of emulation options for your test file individually.|
|
||||
|ContextTest |Each test will get a fresh copy of a [BrowserContext]. You can create as many pages in this context as you'd like. Using this test is the easiest way to test multi-page scenarios where you need more than one tab.<br></br><br></br>Note: You can override the `ContextOptions` method in each test file to control context options, the ones typically passed into the [`method: Browser.newContext`] method. That way you can specify all kinds of emulation options for your test file individually.|
|
||||
|BrowserTest |Each test will get a browser and can create as many contexts as it likes. Each test is responsible for cleaning up all the contexts it created.|
|
||||
|PlaywrightTest|This gives each test a Playwright object so that the test could start and stop as many browsers as it likes.|
|
||||
|
||||
## MSTest
|
||||
|
||||
Playwright provides base classes to write tests with MSTest via the [`Microsoft.Playwright.MSTest`](https://www.nuget.org/packages/Microsoft.Playwright.MSTest) package.
|
||||
|
||||
### Creating an MSTest project
|
||||
|
||||
```bash
|
||||
# Create a new project
|
||||
dotnet new mstest -n PlaywrightTests
|
||||
cd PlaywrightTests
|
||||
# Add the required reference
|
||||
dotnet add package Microsoft.Playwright.MSTest
|
||||
dotnet build
|
||||
# Install the required browsers and operating system dependencies
|
||||
pwsh bin\Debug\netX\playwright.ps1 install --with-deps
|
||||
```
|
||||
|
||||
Modify the UnitTest1.cs:
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright.MSTest;
|
||||
|
||||
namespace PlaywrightTests;
|
||||
|
||||
[TestClass]
|
||||
public class UnitTest1: PageTest
|
||||
{
|
||||
[TestMethod]
|
||||
async public Task ShouldHaveTheCorrectSlogan()
|
||||
{
|
||||
await Page.GotoAsync("https://playwright.dev");
|
||||
await Expect(Page.Locator("text=enables reliable end-to-end testing for modern web apps")).ToBeVisibleAsync();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
async public Task ShouldHaveTheCorrectTitle()
|
||||
{
|
||||
await Page.GotoAsync("https://playwright.dev");
|
||||
var title = Page.Locator(".navbar__inner .navbar__title");
|
||||
await Expect(title).ToHaveTextAsync("Playwright");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Run your tests against Chromium
|
||||
|
||||
```bash
|
||||
dotnet test
|
||||
```
|
||||
|
||||
Run your tests against WebKit
|
||||
|
||||
```bash tab=bash-bash lang=csharp
|
||||
BROWSER=webkit dotnet test
|
||||
```
|
||||
|
||||
```batch tab=bash-batch lang=csharp
|
||||
set BROWSER=webkit
|
||||
dotnet test
|
||||
```
|
||||
|
||||
```powershell tab=bash-powershell lang=csharp
|
||||
$env:BROWSER="webkit"
|
||||
dotnet test
|
||||
```
|
||||
|
||||
Run your tests with GUI
|
||||
|
||||
```bash tab=bash-bash lang=csharp
|
||||
HEADED=1 dotnet test
|
||||
```
|
||||
|
||||
```batch tab=bash-batch lang=csharp
|
||||
set HEADED=1
|
||||
dotnet test
|
||||
```
|
||||
|
||||
```powershell tab=bash-powershell lang=csharp
|
||||
$env:HEADED="1"
|
||||
dotnet test
|
||||
```
|
||||
|
||||
You can also choose specifically which tests to run, using the [filtering capabilities](https://docs.microsoft.com/en-us/dotnet/core/testing/selective-unit-tests?pivots=mstest):
|
||||
|
||||
```bash
|
||||
dotnet test --filter "Name~ShouldAdd"
|
||||
```
|
||||
|
||||
### Running MSTest tests in Parallel
|
||||
|
||||
By default MSTest will run all classes in parallel, while running tests inside each class sequentially. It will create as many processes as there are cores on the host system. You can adjust this behavior by using the following CLI parameter or using a `.runsettings` file, see below.
|
||||
|
||||
```bash
|
||||
dotnet test --settings:.runsettings -- MSTest.Parallelize.Workers=4
|
||||
```
|
||||
|
||||
### Using Verbose API Logs
|
||||
|
||||
When you have enabled the [verbose API log](./debug.md#verbose-api-logs), via the `DEBUG` environment variable, you will see the messages in the standard error stream. In MSTest, within Visual Studio, that will be the `Tests` pane of the `Output` window. It will also be displayed in the `Test Log` for each test.
|
||||
|
||||
### Using the .runsettings file
|
||||
|
||||
When running tests from Visual Studio, you can take advantage of the `.runsettings` file.
|
||||
|
||||
For example, to specify the amount of workers (`MSTest.Parallelize.Workers`), you can use the following snippet:
|
||||
|
||||
```xml
|
||||
<RunSettings>
|
||||
<!-- MSTest adapter -->
|
||||
<MSTest>
|
||||
<Parallelize>
|
||||
<Workers>4</Workers>
|
||||
<Scope>ClassLevel</Scope>
|
||||
</Parallelize>
|
||||
</MSTest>
|
||||
</RunSettings>
|
||||
```
|
||||
|
||||
If you want to enable debugging, you can set the `DEBUG` variable to `pw:api` as documented, by doing:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RunSettings>
|
||||
<RunConfiguration>
|
||||
<EnvironmentVariables>
|
||||
<DEBUG>pw:api</DEBUG>
|
||||
</EnvironmentVariables>
|
||||
</RunConfiguration>
|
||||
</RunSettings>
|
||||
```
|
||||
|
||||
### Base MSTest classes for Playwright
|
||||
|
||||
There are few base classes available to you in `Microsoft.Playwright.MSTest` namespace:
|
||||
|
||||
|Test |Description|
|
||||
|--------------|-----------|
|
||||
|
|
|
|||
Loading…
Reference in a new issue