Adding integration tests

Integration tests are a logical extension of unit tests. They test the integration between multiple technical components within your applications in a real environment with access to external data sources (such as databases, web services, and caches).
The goal of this type of test is to ensure that everything is working well together and providing the expected functionalities when combining various technical components to create application behavior.

Furthermore, integration tests should always have cleanup steps so that they can run repeatedly without error and don't leave any artifacts behind in databases or filesystems.

In the following example, you will learn how to apply integration tests to the Tic-Tac-Toe demo application:

  1. Add a new project of the xUnit Test Project (.NET Core) type called TicTacToe.IntegrationTests to the TicTacToe Solution, update the NuGet packages, and add references to the TicTacToe and TicTacToe.Logging projects as shown in the preceding unit tests project.
  2. Add the Microsoft.AspNetCore.TestHost NuGet package to the IntegrationTests project, as shown in the following screenshot. This allows us to create fully automated integration tests using xUnit: 

  1. Delete the autogenerated class, add a new class called IntegrationTests.cs, and implement a new method called ShouldGetHomePageAsync:
        [Fact]
public async Task ShouldGetHomePageAsync()
{
var response = await _httpClient.GetAsync("/");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.
ReadAsStringAsync();
Assert.Contains("Welcome to the Tic-Tac-Toe Desktop
Game!", responseString);
}
  1. Run the tests in Test Explorer and ensure that they execute successfully.

Now that you have learned how to test your applications, you can continue to add additional unit and integration tests to fully understand these concepts and to build test coverage that will allow you to provide high-quality applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.160.119