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.
Step 2
Run the below command from Package Manager console.
- Install-Package xunit -Version 2.4.0
Or search xUnit.net in NuGet Package Manager as in the below screen.
Go to Tools =>NuGet Package Manager=>Manage NuGet Package for Solution.
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.
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.
- using Xunit;
-
- namespace TDD.xUnit.net.Client
- {
- public class Class1
- {
- [Fact]
- public void PassingTest()
- {
- Assert.Equal(4, Add(2, 2));
- }
-
- [Fact]
- public void FailingTest()
- {
- Assert.Equal(5, Add(2, 2));
- }
-
- int Add(int x, int y)
- {
- return x + y;
- }
- }
- }
Step 5
Open Visual Studio test explorer to see xUnit test cases. Go to menu Tests=>Windows=>Tests Explorer as below screen.
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.
- Install-Package more.xunit.runner.visualstudio -Version 2.3.1
See the following screen.
Step 7
Now build your project and you can debug and run the xUnit.net Unit test cases as in the below screen.
Now let's run the all the xUnit unit test cases as in the following screen.
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#.