.Net Core 1.1 – WebApi Integration testing

Things have changed a bit since the DNX .Net WebApi Integration testing post that I wrote about a year ago. This post will show updated code for .Net Core 1.1, I’m using the TestHost library as before.

I’m using these Nuget packages
xunit 2.3.0-beta1-build3642
Microsoft.AspNet.TestHost 1.1.1

Here is the UnitTest itself

using System;
using System.Diagnostics;
using System.Net.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.AspNetCore.Hosting;
using Xunit;
using DevOps.Notes.Core.Util;

namespace DevOps.Notes.Web.Spec
  public class ValuseControllerTest
    public async void PingTheServerRepeat()
      // Setup client & server
      TestServer server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
      HttpClient client = server.CreateClient();
      // Make a request and check response
      var getResponse = await client.GetAsync(“api/values/”);
      var response = await getResponse.Content.ReadAsStringAsync();
      TraceLog.Log(“web api, response=” + response);

      // Hit the webapi, repeatedly
      Stopwatch watch = Stopwatch.StartNew();
      for (int i = 0; i < Util.Repeat; i++)
        getResponse = await client.GetAsync(“/api/values/”+i);
        response = await getResponse.Content.ReadAsStringAsync();
      TraceLog.Log($“WepApi, round trips ={Util.Repeat}, Execution time, millisec={watch.ElapsedMilliseconds}, average roundtrip=” + ((double)watch.ElapsedMilliseconds / Util.Repeat));
      TraceLog.Log(“web api, response=” + response);

      // Done, release resources



Leave a Reply