Skip to content

Instantly share code, notes, and snippets.

@askingalot
Last active March 29, 2019 18:19
Show Gist options
  • Save askingalot/f0d459f17f332b6f50ef331b7d3f1e68 to your computer and use it in GitHub Desktop.
Save askingalot/f0d459f17f332b6f50ef331b7d3f1e68 to your computer and use it in GitHub Desktop.
Steps to Setup Integration Testing for ASP.NET Core Web API

Steps

  1. Open your Web API solution in Visual Studio
  2. In the Solution Explorer, right-click the Solution to open a menu.
  3. Use the menu select Add / New Project to open the Add New Project dialog.
  4. On the left panel, click the Test node under Visual C#.
  5. In the center panel, click xUnit Test Project (.NET Core).
  6. Enter a name for your project in the Name textbox at the bottom of the form. (something like "<YOUR_WEB_API_PROJECT_NAME>.Tests", ex. StudentExercisesAPI.Tests)

Now we need to setup the new Test Project with a reference to your Web API project AND with the correct Nuget packages needed to perform Integration Tests. Visual Studio offers several different ways of adding these references, however, the simplest way is to edit the test project's .csproj file.

  1. In the Solution Explorer, right-click the Test Project and select Edit <Your_Test_Project>.csproj in the middle of the context menu.
  2. Add the appropriate PackageReferences to ensure you have the following references in your project.
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
  </ItemGroup>
  1. Below the PackageReferencess add the following ProjectReference section to give your Test Project access to your Web API Project. NOTE: Make sure you fill in the __YOUR SOLUTION NAME__ and __YOUR WEB API PROJECT NAME__ placeholders with the names of your solution and project.
  <ItemGroup>
    <ProjectReference Include="..\__YOUR SOLUTION NAME__\__YOUR WEB API PROJECT NAME__.csproj" />
  </ItemGroup>
  1. Create a new class in your Test Project. Call it APIClientProvider. Replace the code in the new file with the following code. Note: Make sure to fill in the __YOUR WEB API PROJECT NAMESPACE__ and __YOUR TEST PROJECT NAMESPACE__ placeholders.
using Microsoft.AspNetCore.Mvc.Testing;
using System.Net.Http;
using Xunit;
using __YOUR WEB API PROJECT NAMESPACE__;

namespace __YOUR TEST PROJECT NAMESPACE__
{
    class APIClientProvider : IClassFixture<WebApplicationFactory<Startup>>
    {
        public HttpClient Client { get; private set; }
        private readonly WebApplicationFactory<Startup> _factory = new WebApplicationFactory<Startup>();

        public APIClientProvider()
        {
            Client = _factory.CreateClient();
        }

        public void Dispose()
        {
            _factory?.Dispose();
            Client?.Dispose();
        }
    }
}
  1. In the Test menu click the Window/Test Explorer item to open the Test Explorer.

Now it's time to write soem tests

  1. Make a new class called something like <SOME_CONTROLLER_NAME>Tests (ex. StudentsControllerTests.
  2. Add a test method to the new class.

Example

    public class StudentsControllerTests
    {
        [Fact]
        public async Task Get_All_Students_Returns_Some_Students()
        {
            using (HttpClient client = new APIClientProvider().Client)
            {
                var response = await client.GetAsync("/api/students");

                response.EnsureSuccessStatusCode();
            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment