chore(generator): use new .NET test attributes (#17172)

This commit is contained in:
Max Schmitt 2022-09-08 01:44:58 +02:00 committed by GitHub
parent c91df61ca3
commit 15add13a6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 46 additions and 28 deletions

View file

@ -94,6 +94,7 @@ using NUnit.Framework;
namespace PlaywrightTests namespace PlaywrightTests
{ {
[TestFixture]
public class TestGitHubAPI : PlaywrightTest public class TestGitHubAPI : PlaywrightTest
{ {
static string REPO = "test-repo-2"; static string REPO = "test-repo-2";
@ -102,7 +103,7 @@ namespace PlaywrightTests
private IAPIRequestContext Request = null; private IAPIRequestContext Request = null;
[Test] [PlaywrightTest]
public async Task ShouldCreateBugReport() public async Task ShouldCreateBugReport()
{ {
var data = new Dictionary<string, string>(); var data = new Dictionary<string, string>();
@ -129,7 +130,7 @@ namespace PlaywrightTests
Assert.AreEqual("Bug description", issue?.GetProperty("body").GetString()); Assert.AreEqual("Bug description", issue?.GetProperty("body").GetString());
} }
[Test] [PlaywrightTest]
public async Task ShouldCreateFeatureRequests() public async Task ShouldCreateFeatureRequests()
{ {
var data = new Dictionary<string, string>(); var data = new Dictionary<string, string>();
@ -219,6 +220,7 @@ using NUnit.Framework;
namespace PlaywrightTests namespace PlaywrightTests
{ {
[TestFixture]
public class TestGitHubAPI : PlaywrightTest public class TestGitHubAPI : PlaywrightTest
{ {
static string REPO = "test-repo-2"; static string REPO = "test-repo-2";
@ -227,7 +229,7 @@ namespace PlaywrightTests
private IAPIRequestContext Request = null; private IAPIRequestContext Request = null;
[Test] [PlaywrightTest]
public async Task ShouldCreateBugReport() public async Task ShouldCreateBugReport()
{ {
var data = new Dictionary<string, string>(); var data = new Dictionary<string, string>();
@ -254,7 +256,7 @@ namespace PlaywrightTests
Assert.AreEqual("Bug description", issue?.GetProperty("body").GetString()); Assert.AreEqual("Bug description", issue?.GetProperty("body").GetString());
} }
[Test] [PlaywrightTest]
public async Task ShouldCreateFeatureRequests() public async Task ShouldCreateFeatureRequests()
{ {
var data = new Dictionary<string, string>(); var data = new Dictionary<string, string>();
@ -341,7 +343,7 @@ The following test creates a new issue via API and then navigates to the list of
project to check that it appears at the top of the list. The check is performed using [LocatorAssertions]. project to check that it appears at the top of the list. The check is performed using [LocatorAssertions].
```csharp ```csharp
[Test] [PlaywrightTest]
public async Task LastCreatedIssueShouldBeFirstInTheList() public async Task LastCreatedIssueShouldBeFirstInTheList()
{ {
var data = new Dictionary<string, string>(); var data = new Dictionary<string, string>();
@ -364,7 +366,7 @@ The following test creates a new issue via user interface in the browser and the
it was created: it was created:
```csharp ```csharp
[Test] [PlaywrightTest]
public async Task LastCreatedIssueShouldBeOnTheServer() public async Task LastCreatedIssueShouldBeOnTheServer()
{ {
await Page.GotoAsync("https://github.com/" + USER + "/" + REPO + "/issues"); await Page.GotoAsync("https://github.com/" + USER + "/" + REPO + "/issues");

View file

@ -54,9 +54,10 @@ using NUnit.Framework;
namespace PlaywrightTests; namespace PlaywrightTests;
[TestFixture]
public class ExampleTests : PageTest public class ExampleTests : PageTest
{ {
[Test] [PlaywrightTest]
public async Task StatusBecomesSubmitted() public async Task StatusBecomesSubmitted()
{ {
// .. // ..

View file

@ -56,9 +56,10 @@ using NUnit.Framework;
namespace PlaywrightTests; namespace PlaywrightTests;
[TestFixture]
public class ExampleTests : PageTest public class ExampleTests : PageTest
{ {
[Test] [PlaywrightTest]
public async Task NavigatetoLoginPage() public async Task NavigatetoLoginPage()
{ {
// .. // ..

View file

@ -55,9 +55,10 @@ using NUnit.Framework;
namespace PlaywrightTests; namespace PlaywrightTests;
[TestFixture]
public class ExampleTests : PageTest public class ExampleTests : PageTest
{ {
[Test] [PlaywrightTest]
public async Task StatusBecomesSubmitted() public async Task StatusBecomesSubmitted()
{ {
await Page.Locator("#submit-button").ClickAsync(); await Page.Locator("#submit-button").ClickAsync();

View file

@ -98,9 +98,10 @@ using NUnit.Framework;
namespace PlaywrightTests; namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)] [Parallelizable(ParallelScope.Self)]
[TestFixture]
public class Tests : PageTest public class Tests : PageTest
{ {
[Test] [PlaywrightTest]
public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage() public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
{ {
await Page.GotoAsync("https://playwright.dev"); await Page.GotoAsync("https://playwright.dev");
@ -135,7 +136,7 @@ namespace PlaywrightTests;
[TestClass] [TestClass]
public class UnitTest1 : PageTest public class UnitTest1 : PageTest
{ {
[TestMethod] [PlaywrightTestMethod]
public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage() public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
{ {
await Page.GotoAsync("https://playwright.dev"); await Page.GotoAsync("https://playwright.dev");

View file

@ -260,9 +260,10 @@ using NUnit.Framework;
namespace PlaywrightTests; namespace PlaywrightTests;
[TestFixture]
public class ExampleTests : PageTest public class ExampleTests : PageTest
{ {
[Test] [PlaywrightTest]
public async Task StatusBecomesSubmitted() public async Task StatusBecomesSubmitted()
{ {
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted"); await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");

View file

@ -41,16 +41,17 @@ using Microsoft.Playwright.NUnit;
namespace PlaywrightTests; namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)] [Parallelizable(ParallelScope.Self)]
[TestFixture]
public class MyTest : PageTest public class MyTest : PageTest
{ {
[Test] [PlaywrightTest]
public async Task ShouldHaveTheCorrectSlogan() public async Task ShouldHaveTheCorrectSlogan()
{ {
await Page.GotoAsync("https://playwright.dev"); await Page.GotoAsync("https://playwright.dev");
await Expect(Page.Locator("text=enables reliable end-to-end testing for modern web apps")).ToBeVisibleAsync(); await Expect(Page.Locator("text=enables reliable end-to-end testing for modern web apps")).ToBeVisibleAsync();
} }
[Test] [PlaywrightTest]
public async Task ShouldHaveTheCorrectTitle() public async Task ShouldHaveTheCorrectTitle()
{ {
await Page.GotoAsync("https://playwright.dev"); await Page.GotoAsync("https://playwright.dev");
@ -121,9 +122,10 @@ using Microsoft.Playwright.NUnit;
namespace PlaywrightTests; namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)] [Parallelizable(ParallelScope.Self)]
[TestFixture]
public class MyTest : PageTest public class MyTest : PageTest
{ {
[Test] [PlaywrightTest]
public async Task TestWithCustomContextOptions() public async Task TestWithCustomContextOptions()
{ {
// The following Page (and BrowserContext) instance has the custom colorScheme, viewport and baseURL set: // The following Page (and BrowserContext) instance has the custom colorScheme, viewport and baseURL set:
@ -215,6 +217,10 @@ There are a few base classes available to you in `Microsoft.Playwright.NUnit` na
|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.| |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.| |PlaywrightTest|This gives each test a Playwright object so that the test could start and stop as many browsers as it likes.|
### 'No test is available'
You need to add `[TestFixture]` to your test class. NUnit does not discover tests without it, if the `TestAttribute` comes from a different assembly.
## MSTest ## MSTest
Playwright provides base classes to write tests with MSTest via the [`Microsoft.Playwright.MSTest`](https://www.nuget.org/packages/Microsoft.Playwright.MSTest) package. Playwright provides base classes to write tests with MSTest via the [`Microsoft.Playwright.MSTest`](https://www.nuget.org/packages/Microsoft.Playwright.MSTest) package.
@ -244,14 +250,14 @@ namespace PlaywrightTests;
[TestClass] [TestClass]
public class UnitTest1: PageTest public class UnitTest1: PageTest
{ {
[TestMethod] [PlaywrightTestMethod]
public async Task ShouldHaveTheCorrectSlogan() public async Task ShouldHaveTheCorrectSlogan()
{ {
await Page.GotoAsync("https://playwright.dev"); await Page.GotoAsync("https://playwright.dev");
await Expect(Page.Locator("text=enables reliable end-to-end testing for modern web apps")).ToBeVisibleAsync(); await Expect(Page.Locator("text=enables reliable end-to-end testing for modern web apps")).ToBeVisibleAsync();
} }
[TestMethod] [PlaywrightTestMethod]
public async Task ShouldHaveTheCorrectTitle() public async Task ShouldHaveTheCorrectTitle()
{ {
await Page.GotoAsync("https://playwright.dev"); await Page.GotoAsync("https://playwright.dev");
@ -329,7 +335,7 @@ namespace PlaywrightTests;
[TestClass] [TestClass]
public class UnitTest1 : PageTest public class UnitTest1 : PageTest
{ {
[TestMethod] [PlaywrightTestMethod]
public async Task TestWithCustomContextOptions() public async Task TestWithCustomContextOptions()
{ {
// The following Page (and BrowserContext) instance has the custom colorScheme, viewport and baseURL set: // The following Page (and BrowserContext) instance has the custom colorScheme, viewport and baseURL set:

View file

@ -24,9 +24,10 @@ using Microsoft.Playwright.NUnit;
namespace PlaywrightTests; namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)] [Parallelizable(ParallelScope.Self)]
[TestFixture]
public class Tests : PageTest public class Tests : PageTest
{ {
[Test] [PlaywrightTest]
public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage() public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
{ {
await Page.GotoAsync("https://playwright.dev"); await Page.GotoAsync("https://playwright.dev");
@ -61,7 +62,7 @@ namespace PlaywrightTests;
[TestClass] [TestClass]
public class UnitTest1 : PageTest public class UnitTest1 : PageTest
{ {
[TestMethod] [PlaywrightTestMethod]
public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage() public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
{ {
await Page.GotoAsync("https://playwright.dev"); await Page.GotoAsync("https://playwright.dev");
@ -136,9 +137,10 @@ using NUnit.Framework;
namespace PlaywrightTests; namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)] [Parallelizable(ParallelScope.Self)]
[TestFixture]
public class Tests : PageTest public class Tests : PageTest
{ {
[Test] [PlaywrightTest]
public async Task BasicTest() public async Task BasicTest()
{ {
await Page.GotoAsync("https://playwright.dev"); await Page.GotoAsync("https://playwright.dev");
@ -157,7 +159,7 @@ namespace PlaywrightTests;
[TestClass] [TestClass]
public class UnitTest1 : PageTest public class UnitTest1 : PageTest
{ {
[TestMethod] [PlaywrightTestMethod]
public async Task BasicTest() public async Task BasicTest()
{ {
await Page.GotoAsync("https://playwright.dev"); await Page.GotoAsync("https://playwright.dev");
@ -190,9 +192,10 @@ using NUnit.Framework;
namespace PlaywrightTests; namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)] [Parallelizable(ParallelScope.Self)]
[TestFixture]
public class Tests : PageTest public class Tests : PageTest
{ {
[Test] [PlaywrightTest]
public async Task MainNavigation() public async Task MainNavigation()
{ {
// Assertions use the expect API. // Assertions use the expect API.
@ -218,7 +221,7 @@ namespace PlaywrightTests;
[TestClass] [TestClass]
public class UnitTest1 : PageTest public class UnitTest1 : PageTest
{ {
[TestMethod] [PlaywrightTestMethod]
public async Task MainNavigation() public async Task MainNavigation()
{ {
// Assertions use the expect API. // Assertions use the expect API.

View file

@ -195,7 +195,8 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
using Microsoft.Playwright.${this._mode === 'nunit' ? 'NUnit' : 'MSTest'}; using Microsoft.Playwright.${this._mode === 'nunit' ? 'NUnit' : 'MSTest'};
using Microsoft.Playwright; using Microsoft.Playwright;
${this._mode === 'nunit' ? '[Parallelizable(ParallelScope.Self)]' : '[TestClass]'} ${this._mode === 'nunit' ? `[Parallelizable(ParallelScope.Self)]
[TestFixture]` : '[TestClass]'}
public class Tests : PageTest public class Tests : PageTest
{`); {`);
const formattedContextOptions = formatContextOptions(options.contextOptions, options.deviceName); const formattedContextOptions = formatContextOptions(options.contextOptions, options.deviceName);
@ -206,7 +207,7 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
}`); }`);
formatter.newLine(); formatter.newLine();
} }
formatter.add(` [${this._mode === 'nunit' ? 'Test' : 'TestMethod'}] formatter.add(` [${this._mode === 'nunit' ? 'PlaywrightTest' : 'PlaywrightTestMethod'}]
public async Task MyTest() public async Task MyTest()
{`); {`);
return formatter.format(); return formatter.format();

View file

@ -232,7 +232,7 @@ public class Tests : PageTest
}; };
} }
[TestMethod] [PlaywrightTestMethod]
public async Task MyTest() public async Task MyTest()
{ {
// Go to ${emptyHTML} // Go to ${emptyHTML}
@ -250,6 +250,7 @@ test(`should print a valid basic program in nunit`, async ({ runCLI }) => {
using Microsoft.Playwright; using Microsoft.Playwright;
[Parallelizable(ParallelScope.Self)] [Parallelizable(ParallelScope.Self)]
[TestFixture]
public class Tests : PageTest public class Tests : PageTest
{ {
public override BrowserNewContextOptions ContextOptions() public override BrowserNewContextOptions ContextOptions()
@ -260,7 +261,7 @@ public class Tests : PageTest
}; };
} }
[Test] [PlaywrightTest]
public async Task MyTest() public async Task MyTest()
{ {
// Go to ${emptyHTML} // Go to ${emptyHTML}