Set up unit testing with xUnit in .NET Core

A quick post on getting going with xUnit in .NET Core.

    1. Create a .NET Core Class Library.
    2. Open a command prompt and cd to your project folder.
    3. Add the following nuget packages:
      dotnet add package Microsoft.NET.Test.Sdk
      dotnet add package xunit
      dotnet add package xunit.runner.visualstudio
    4. Add a test to your test class e.g.
      using Xunit;
      
      namespace ClassLibrary1
      {
          public class Class1
          {
              [Fact]
              public void Test1()
              {
                  Assert.Equal(1, 1);
              }
          }
      }
      

Hopefully this help you avoid errors like No test is available in c:\project\ClassLibrary1.dll. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again. when xunit.runner.visualstudio is not referenced. Or Unable to find tests for C:\project\ClassLibrary1.dll. Make sure test project has a nuget reference of package "Microsoft.NET.Test.Sdk" and framework version settings are appropriate. Rerun with /diag option to diagnose further. when Microsoft.NET.Test.Sdk is not referenced.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s