From b78435fc99d43e6419cf02e8d7acb6fbe4973f6b Mon Sep 17 00:00:00 2001 From: martincostello Date: Sun, 22 Dec 2024 09:49:22 +0000 Subject: [PATCH] Migrate to xunit v3 Migrate tests to xunit v3. --- tests/TodoApp.Tests/ApiTests.cs | 57 +++++++++++++----------- tests/TodoApp.Tests/TodoApp.Tests.csproj | 7 ++- tests/TodoApp.Tests/UITests.cs | 14 +++--- 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/tests/TodoApp.Tests/ApiTests.cs b/tests/TodoApp.Tests/ApiTests.cs index 2a5631de..d7c3fef6 100644 --- a/tests/TodoApp.Tests/ApiTests.cs +++ b/tests/TodoApp.Tests/ApiTests.cs @@ -11,7 +11,7 @@ namespace TodoApp; -[Collection(TodoAppCollection.Name)] +[Collection] public class ApiTests { public ApiTests(TodoAppFixture fixture, ITestOutputHelper outputHelper) @@ -31,10 +31,11 @@ public ApiTests(TodoAppFixture fixture, ITestOutputHelper outputHelper) public async Task Can_Manage_Todo_Items_With_Api() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var client = await CreateAuthenticatedClientAsync(); // Act - Get all the items - var items = await client.GetFromJsonAsync("/api/items"); + var items = await client.GetFromJsonAsync("/api/items", cancellationToken); // Assert - There should be no items items.ShouldNotBeNull(); @@ -47,20 +48,20 @@ public async Task Can_Manage_Todo_Items_With_Api() var newItem = new CreateTodoItemModel { Text = text }; // Act - Add a new item - using var createdResponse = await client.PostAsJsonAsync("/api/items", newItem); + using var createdResponse = await client.PostAsJsonAsync("/api/items", newItem, cancellationToken); // Assert - An item was created createdResponse.StatusCode.ShouldBe(HttpStatusCode.Created); createdResponse.Headers.Location.ShouldNotBeNull(); - using var createdJson = await createdResponse.Content.ReadFromJsonAsync(); + using var createdJson = await createdResponse.Content.ReadFromJsonAsync(cancellationToken); // Arrange - Get the new item's URL and Id var itemUri = createdResponse.Headers.Location; var itemId = createdJson!.RootElement.GetProperty("id").GetString(); // Act - Get the item - var item = await client.GetFromJsonAsync(itemUri); + var item = await client.GetFromJsonAsync(itemUri, cancellationToken); // Assert - Verify the item was created correctly item.ShouldNotBeNull(); @@ -70,12 +71,12 @@ public async Task Can_Manage_Todo_Items_With_Api() item.Text.ShouldBe(text); // Act - Mark the item as being completed - using var completedResponse = await client.PostAsJsonAsync(itemUri + "/complete", new { }); + using var completedResponse = await client.PostAsJsonAsync(itemUri + "/complete", new { }, cancellationToken); // Assert - The item was completed completedResponse.StatusCode.ShouldBe(HttpStatusCode.NoContent); - item = await client.GetFromJsonAsync(itemUri); + item = await client.GetFromJsonAsync(itemUri, cancellationToken); item.ShouldNotBeNull(); item.Id.ShouldBe(itemId); @@ -83,7 +84,7 @@ public async Task Can_Manage_Todo_Items_With_Api() item.IsCompleted.ShouldBeTrue(); // Act - Get all the items - items = await client.GetFromJsonAsync("/api/items"); + items = await client.GetFromJsonAsync("/api/items",cancellationToken); // Assert - The item was completed items.ShouldNotBeNull(); @@ -98,12 +99,12 @@ public async Task Can_Manage_Todo_Items_With_Api() item.LastUpdated.ShouldNotBeNull(); // Act - Delete the item - using var deletedResponse = await client.DeleteAsync(itemUri); + using var deletedResponse = await client.DeleteAsync(itemUri, cancellationToken); // Assert - The item no longer exists deletedResponse.StatusCode.ShouldBe(HttpStatusCode.NoContent); - items = await client.GetFromJsonAsync("/api/items"); + items = await client.GetFromJsonAsync("/api/items", cancellationToken); items.ShouldNotBeNull(); items.Items.ShouldNotBeNull(); @@ -111,12 +112,12 @@ public async Task Can_Manage_Todo_Items_With_Api() items.Items.ShouldNotContain(x => x.Id == itemId); // Act - using var getResponse = await client.GetAsync(itemUri); + using var getResponse = await client.GetAsync(itemUri, cancellationToken); // Assert getResponse.StatusCode.ShouldBe(HttpStatusCode.NotFound); - var problem = await getResponse.Content.ReadFromJsonAsync(); + var problem = await getResponse.Content.ReadFromJsonAsync(cancellationToken); problem.ShouldNotBeNull(); problem.Status.ShouldBe(StatusCodes.Status404NotFound); @@ -130,16 +131,17 @@ public async Task Can_Manage_Todo_Items_With_Api() public async Task Cannot_Create_Todo_Item_With_No_Text() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var client = await CreateAuthenticatedClientAsync(); var item = new CreateTodoItemModel { Text = string.Empty }; // Act - var response = await client.PostAsJsonAsync("/api/items", item); + var response = await client.PostAsJsonAsync("/api/items", item, cancellationToken); // Assert response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); - var problem = await response.Content.ReadFromJsonAsync(); + var problem = await response.Content.ReadFromJsonAsync(cancellationToken); problem.ShouldNotBeNull(); problem.Status.ShouldBe(StatusCodes.Status400BadRequest); @@ -153,25 +155,26 @@ public async Task Cannot_Create_Todo_Item_With_No_Text() public async Task Cannot_Complete_Todo_Item_Multiple_Times() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var client = await CreateAuthenticatedClientAsync(); var item = new CreateTodoItemModel { Text = "Something" }; - using var createdResponse = await client.PostAsJsonAsync("/api/items", item); + using var createdResponse = await client.PostAsJsonAsync("/api/items", item, cancellationToken); createdResponse.StatusCode.ShouldBe(HttpStatusCode.Created); createdResponse.Headers.Location.ShouldNotBeNull(); var itemUri = createdResponse.Headers.Location; - using var completedResponse = await client.PostAsJsonAsync(itemUri + "/complete", new { }); + using var completedResponse = await client.PostAsJsonAsync(itemUri + "/complete", new { }, cancellationToken); completedResponse.StatusCode.ShouldBe(HttpStatusCode.NoContent); // Act - using var response = await client.PostAsJsonAsync(itemUri + "/complete", new { }); + using var response = await client.PostAsJsonAsync(itemUri + "/complete", new { }, cancellationToken); // Assert response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); - var problem = await response.Content.ReadFromJsonAsync(); + var problem = await response.Content.ReadFromJsonAsync(cancellationToken); problem.ShouldNotBeNull(); problem.Status.ShouldBe(StatusCodes.Status400BadRequest); @@ -185,25 +188,26 @@ public async Task Cannot_Complete_Todo_Item_Multiple_Times() public async Task Cannot_Complete_Deleted_Todo_Item() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var client = await CreateAuthenticatedClientAsync(); var item = new CreateTodoItemModel { Text = "Something" }; - using var createdResponse = await client.PostAsJsonAsync("/api/items", item); + using var createdResponse = await client.PostAsJsonAsync("/api/items", item, cancellationToken); createdResponse.StatusCode.ShouldBe(HttpStatusCode.Created); createdResponse.Headers.Location.ShouldNotBeNull(); var itemUri = createdResponse.Headers.Location; - using var deletedResponse = await client.DeleteAsync(itemUri); + using var deletedResponse = await client.DeleteAsync(itemUri, cancellationToken); deletedResponse.StatusCode.ShouldBe(HttpStatusCode.NoContent); // Act - using var response = await client.PostAsJsonAsync(itemUri + "/complete", new { }); + using var response = await client.PostAsJsonAsync(itemUri + "/complete", new { }, cancellationToken); // Assert response.StatusCode.ShouldBe(HttpStatusCode.NotFound); - var problem = await response.Content.ReadFromJsonAsync(); + var problem = await response.Content.ReadFromJsonAsync(cancellationToken); problem.ShouldNotBeNull(); problem.Status.ShouldBe(StatusCodes.Status404NotFound); @@ -217,25 +221,26 @@ public async Task Cannot_Complete_Deleted_Todo_Item() public async Task Cannot_Delete_Todo_Item_Multiple_Times() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var client = await CreateAuthenticatedClientAsync(); var item = new CreateTodoItemModel { Text = "Something" }; - using var createdResponse = await client.PostAsJsonAsync("/api/items", item); + using var createdResponse = await client.PostAsJsonAsync("/api/items", item, cancellationToken); createdResponse.StatusCode.ShouldBe(HttpStatusCode.Created); createdResponse.Headers.Location.ShouldNotBeNull(); var itemUri = createdResponse.Headers.Location; - using var deletedResponse = await client.DeleteAsync(itemUri); + using var deletedResponse = await client.DeleteAsync(itemUri, cancellationToken); deletedResponse.StatusCode.ShouldBe(HttpStatusCode.NoContent); // Act - using var response = await client.DeleteAsync(itemUri); + using var response = await client.DeleteAsync(itemUri, cancellationToken); // Assert response.StatusCode.ShouldBe(HttpStatusCode.NotFound); - var problem = await response.Content.ReadFromJsonAsync(); + var problem = await response.Content.ReadFromJsonAsync(cancellationToken); problem.ShouldNotBeNull(); problem.Status.ShouldBe(StatusCodes.Status404NotFound); diff --git a/tests/TodoApp.Tests/TodoApp.Tests.csproj b/tests/TodoApp.Tests/TodoApp.Tests.csproj index 9aede7d3..a4b2a278 100644 --- a/tests/TodoApp.Tests/TodoApp.Tests.csproj +++ b/tests/TodoApp.Tests/TodoApp.Tests.csproj @@ -2,6 +2,7 @@ false $(NoWarn);CA1861 + Exe TodoApp net9.0 @@ -10,16 +11,15 @@ - + - - + @@ -31,7 +31,6 @@ - true diff --git a/tests/TodoApp.Tests/UITests.cs b/tests/TodoApp.Tests/UITests.cs index de5153a5..0e475562 100644 --- a/tests/TodoApp.Tests/UITests.cs +++ b/tests/TodoApp.Tests/UITests.cs @@ -5,7 +5,7 @@ namespace TodoApp; -[Collection(HttpServerCollection.Name)] +[Collection] public class UITests : IAsyncLifetime { public UITests(HttpServerFixture fixture, ITestOutputHelper outputHelper) @@ -98,7 +98,7 @@ await browser.WithPageAsync(async page => await items[0].DeleteAsync(); await items[1].CompleteAsync(); - await Task.Delay(TimeSpan.FromSeconds(0.5)); + await Task.Delay(TimeSpan.FromSeconds(0.5), TestContext.Current.CancellationToken); // Assert items = await app.GetItemsAsync(); @@ -120,13 +120,17 @@ await browser.WithPageAsync(async page => }); } - public Task InitializeAsync() + public ValueTask InitializeAsync() { InstallPlaywright(); - return Task.CompletedTask; + return ValueTask.CompletedTask; } - public Task DisposeAsync() => Task.CompletedTask; + public ValueTask DisposeAsync() + { + GC.SuppressFinalize(this); + return ValueTask.CompletedTask; + } private static void InstallPlaywright() {