Archive for the ‘.net’ Category.

UnitTest MsSql database using Slacker, SlackerRunner

Our open source MsSql UnitTest framework Slacker and SlackerRunner was featured on Microsoft Channel 9. Now it’s easier than ever to UnitTest your database and add it into your CI,CD build pipeline.

Watch Eric Kang from Microsoft explain in detail what you need and how it works.

Nlog configuration

I needed to add Nlog tracing that must be configurable from the config file. Finding a simple sample took a me a while. Therefor I’m putting the needed pieces here.

App.config

<?xml version=“1.0” encoding=“utf-8” ?>
<configuration>
  <configSections>
    <section name=“nlog” type=“NLog.Config.ConfigSectionHandler, NLog”/>
  </configSections>
  <nlog xmlns=“http://www.nlog-project.org/schemas/NLog.xsd”
    xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
    autoReload=“true”
    throwExceptions=“false”>

    <variable name=“appName” value=“sample” />
    <targets async=“true”>
      <target xsi:type=“File”
                name=“default”
                fileName=“sample.log”
                keepFileOpen=“false”
                DeleteOldFileOnStartup=“true”
            />

    </targets>
    <rules>
      <logger name=“*” writeTo=“default” minlevel=“Info” />
    </rules>
  </nlog>
</configuration>

And then in code, you can just do the following.

using NLog;
        Logger logger = LogManager.GetLogger(“mylogger”);
        logger.Log(LogLevel.Debug, “Hellow world”);

.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
  {
    [Fact]
    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();
      }
      watch.Stop();
      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
      server.Dispose();
      client.Dispose();

    }

  }
}

TFS 2015, Xunit transform to Trx

It turns out I was wrong in my last post about transforming nUnit tests to Trx ( Ms Test ) format for display in TFS 2015. It fails short as the nUnit format that xUnit spits out does not have any console or standard out traces. Therefor when you want to view more information about your tests or error traces on failures non are available. I went down the road to use the nUnit format because TFS was supposed to take it and display it in TFS without tranforming to Trx but that does not work. So the solution is to use the xUnit Xml V2 format by passing the -xml switch to xUnit. Then convert the xml to Trx using your Xslt transform. Mine is here on GitHub if you want a copy.

DNX .Net WebApi Integration testing

I needed to compare some gRPC round trip numbers to plain .Net WebApi. So the question became how to test WepApi round trips from UnitTest harness.

It’s actually pretty simple if you are using the newer DNX libraries. I’m using the TestHost library to get things done.

From project.json

“System.Net.Http”: “4.0.1-beta-23516”,
“Microsoft.AspNet.TestHost”: “1.0.0-rc1-final”,

The UnitTest itself

[Fact]
    public async void PingTheServerRepeat()
    {
      // Setup client & server
      TestServer server = new TestServer(TestServer.CreateBuilder().UseStartup<Startup>());
      HttpClient client = server.CreateClient();

      // Make a request and check response
      var getResponse = await client.GetAsync(“/api/values/5”);
      var response = await getResponse.Content.ReadAsStringAsync();
      Logger.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();
      }
      watch.Stop();
      Logger.Log($“WepApi, round trips={Util.Repeat}, Execution time, millisec={watch.ElapsedMilliseconds}, average roundtrip=” + ((double)watch.ElapsedMilliseconds / Util.Repeat));
      Logger.Log(“web api, response=” + response);

      // Done, release resources
      server.Dispose();
      client.Dispose();
    }

TFS 2015, Xunit, Nunit, transform to Trx

As TFS vNext currently doesn’t have NoShadowCopy option ( should be coming with the new DNX / 2016 ). I had to modify my new TFS build definition to use xUnit runner to run my tests and then have it output the result file in the optional xunit xml format ( -nunit option ). However true to form TFS 2015 doesn’t understand the Nunit xml format even though they have that as one of their options on the UnitTest results upload vNext task. The next part was to figure out how to transform the Nunit xml to TRX in order for TFS to be able to show the results on the dashboard. Using nxslt3 to transform and NUnitToMSTest.xlst transformation seems to be the way to go. However NUnitToMSTest.xlst doesn’t transform it correctly, at least not to the liking of VS2012, VS2015 or TFS 2015. So I had to modify the xlst slightly and then it will load in VS and the TFS dashboard. Below is the new version of NUnitToMSTest.xlst.

New version
NUnitToMSTest.xslt