Setup xUnit.net Unit Testing In Class Library Project

In this article, we will learn how to set up xUnit.net in the class library project step by step from scratch in Visual Studio 2017.
 
Step 1 

Create a class library project "TDD.xUnit.net.Client" as in the below screen.
 
Setup xUnit.net Unit Testing In Class Library Project 
 
Step 2

Run the below command from Package Manager console.
  1. Install-Package xunit -Version 2.4.0  
Setup xUnit.net Unit Testing In Class Library Project
 
Or search xUnit.net in NuGet Package Manager as in the below screen.

Go to Tools =>NuGet Package Manager=>Manage NuGet Package for Solution.
 
Setup xUnit.net Unit Testing In Class Library Project
 
Setup xUnit.net Unit Testing In Class Library Project 
 
Step 3

You can install xUnit.net by Package Manager console or NuGet Package Manager as in the above screen.

Let's install xUnit.net from Package Manager console.
 
Setup xUnit.net Unit Testing In Class Library Project 

Step 4

Now, you are ready to write xUnit.net Unit Test cases. Now, let's write the first xUnit.net Unit Test case. Copy and paste the below code in "Class1.cs" file. 
  1. using Xunit;  
  2.   
  3. namespace TDD.xUnit.net.Client  
  4. {  
  5.     public class Class1  
  6.     {  
  7.         [Fact]  
  8.         public void PassingTest()  
  9.         {  
  10.             Assert.Equal(4, Add(2, 2));  
  11.         }  
  12.   
  13.         [Fact]  
  14.         public void FailingTest()  
  15.         {  
  16.             Assert.Equal(5, Add(2, 2));  
  17.         }  
  18.   
  19.         int Add(int x, int y)  
  20.         {  
  21.             return x + y;  
  22.         }  
  23.     }  
  24. }  
Step 5

Open Visual Studio test explorer to see xUnit test cases. Go to menu Tests=>Windows=>Tests Explorer as below screen.

Setup xUnit.net Unit Testing In Class Library Project

Note
At this point in time you can discover all written xUnit Unit Test cases in Visual Studio 2017 Tests Explorer Window but you cannot debug and run these unit test cases because xUnit runners did not install yet for this solution. You have to install xUnit runners "xunit.console.runner" or "xunit.runner.visualstudio" to run and debug xUnit unit tests  cases. You can install runners through "Package Manager Console" or "Nuget Package Manager" as we did in step-02.
 
Step 6

Now, let's install runners from Package Manager Console just by running the following command.
  1. Install-Package more.xunit.runner.visualstudio -Version 2.3.1  
See the following screen.
 
Setup xUnit.net Unit Testing In Class Library Project
 
Step 7

Now build your project and you can debug and run the xUnit.net Unit test cases as in the below screen.
 
Setup xUnit.net Unit Testing In Class Library Project 
 
Setup xUnit.net Unit Testing In Class Library Project
 
Now let's run the all the xUnit unit test cases as in the  following screen.
 
Setup xUnit.net Unit Testing In Class Library Project 
 
Step 8 - Done.

Congratulations! You have successfully created xUnit.net unit test case TDD environment in Visual Studio 2017. If you have any query or concern, just let me know or just put in the comment box and I will respond as soon as possible. I am open to discussing anything, even silly questions as well. If you have any suggestions related to this article, please let me know and I promise I will improve this article to a maximum level.

Summary

In this article, we have learned how to set up xUnit.net unit test case TDD environment in Visual Studio 2017 in C#.


Similar Articles