Xunit - test framework for Unit Testing

There are three different test frameworks for Unit Testing supported by ASP.NET Core: MSTest, xUnit, and NUnit; that allow us to test our code in a consistent way.

Instead of creating a new test, we can use these two attributes: Theory and InlineData to create a single data driven test.
using UnitTest.Controllers;
using Xunit;
namespace TestProject1
{
    public class UnitTest1
    {
        [Theory]
        [InlineData(1, "Jignesh")]
        [InlineData(2, "Rakesh")]
        [InlineData(3, "Not Found")]
        public void Test3(int empId, string name)
        {
            HomeController home = new HomeController();
            string result = home.GetEmployeeName(empId);
            Assert.Equal(name, result);
        }
    }
}


To unit test the controller having dependency on ILogger service, we have to pass ILogger object.
private readonly ILogger _logger;



If you want, you can override that by setting the Name property on your Fact attribute -- often a good idea to improve readability. This example changes the name of the test in Test Explorer to "Customer Name Update":
[Fact(Name = "Customer Name Update"]
public void ChangesCustomerName()
{}


To turn off a test method, set the Skip property on the Fact attribute to the reason that you've turned the test off (unfortunately the reason isn't currently displayed in Test Explorer).
[Fact(Skip ="Test data not available")]
public void ChangesCustomerName()
{}


The Trait attribute lets you organize tests into groups by creating category names and assigning values to them.
This example creates a category called Customer with the value "Update":
[Fact(DisplayName = "Change Name2")]
[Trait("Customer", "Update")]
public void ChangesCustomerName()
{}

By default, xUnit runs tests in different test classes in parallel, which can significantly shorten the time to run all your tests. It also means that xUnit effectively ignores the Run Tests in Parallel setting at the top of the Test Explorer window.
You assign tests to a collection using the Collection attribute, passing a name for the collection.
This code assigns both the PremiumCustomerTests and the CashOnlyCustomerTests test classes to a collection called Customer Updates, ensuring that the tests in the two classes aren't run in parallel:
[Collection("Customer Updates")]
public class PremiumCustomerTests
{
   ... //test methods ...
}
[Collection("Customer Updates")]
public class CashOnlyCustomerTests
{
 ... //test methods ...
}


It is essentially a testing framework which provides a set of attributes and methods we can use to write the test code for our applications. Some of those attributes, we are going to use are:
[Fact] – attribute states that the method should be executed by the test runner
[Theory] – attribute implies that we are going to send some parameters to our testing code. So, it is similar to the [Fact] attribute, because it states that the method should be executed by the test runner, but additionally implies that we are going to send parameters to the test method
[InlineData] – attribute provides those parameters we are sending to the test method. If we are using the [Theory] attribute, we have to use the [InlineData] as well.