in Agile practices

Early feedback from your code. Run your tests at build time with Xunit

Developing large applications  having a quality check, now days is a must. Such as quality check can be done in multiple ways: unit testing, automation testing, integration testing, others.The following example will show you a simple way of triggering of unit tests at build time using Xunit.

 

What’s cool about this approach ? It’s handy.

Xunit already has integration with Visual Studio, you can also install Xunit.Net runner extension which allows you to run tests within Test Explorer, but with this approach the unit tests will be triggered after the build is done.

 

Step 1: Create a class library project

Step 2: In Package Manager Console run the following two commands with the default project set to the newly created project:

PM> Install-Package xunit

PM> Install-Package xunit.runners

Step 3: Add reference on the project to xunit.runner.msbuild.dll. This DLL is the root of your solution in …\packages\xunit.runners.1.9.2\tools\ subdirectory.

image

Step 4: Right click, unload project and then right click again and click on edit csproj

Step 5: Add the following snippet at the end of the file just before </Project> element. Note: Assembly file name (UnitTests.dll) is the output file of the project created at Step 1 – please change that.

image

  <UsingTask AssemblyFile="bin\debug\xunit.runner.msbuild.dll" TaskName="Xunit.Runner.MSBuild.xunit" />
  <Target Name="AfterBuild">
    <xunit Assembly="bin\debug\UnitTests.dll" />
  </Target>

Step 6: Add a that will fail test using Xunit framework.

namespace UnitTests
{
    using Xunit;

    public class FutureTests
    {
        [Fact]
        public void DummyTest()
        {
            Assert.Equal(1, 2);
        }
    }
}

Step 7: Rebuild the solution and you should get a build error. Fix the test and rebuild again.

image