Syntactical Difference Between MSTest Nunit And Xunit

In this article, you will learn about the syntactical difference between MSTest NUnit and XUnit.

Recently, I got a chance to work with unit testing on one of my projects. The task was to migrate all the existing unit tests from the MS Test framework to Xunit framework in a .NET application.
 
I was quite familiar with MS Test framework but had not worked with Xunit. Thus, the process of reading began!
 
While studying, I happened to find that MS Test, Nunit and Xunit (no doubt, there can be so many other frameworks too) were the most frequently used test frameworks, when it comes to working with unit testing of .NET applications.
 
This article won’t compare these frameworks (with respect to usability, performance or popularity) as many authors have already done their part and made all types of possible comparisons. Here, I am planning to point out only the major syntactical differences between the 3 testing frameworks mentioned above. Though the code inside the actual test cases (methods) remains almost the same for all 3, significant changes are seen in the attributes that are mentioned over the classes and the methods.
 
I assume that displaying a chart of comparison would be of much more use rather than writing a long theory over this.
 
Following is the chart which shows the syntactical differences between these testing frameworks.
 
Feel free to add your points through the comments and I shall expand this chart accordingly,
 
Factor
MS Test
NUnit
XUnit
Mark test class
[TestClass]
[TestFixture]
N.A.
Mark test method
[TestMethod]
[Test]
[Fact]
Mark initialization method
[TestInitialize]
[Setup]
N.A.
(usually, constructor of the test class is used for initialization)
For Data Driven Tests
Mark data driven test method
[DataTestMethod]
N.A.
[Theory]
Provide data to the method through parameters
[DataRow( _ , _)]
[TestCase( _ , _)]
[InlineData( _ , _)]
Some of the frequently used framework methods
Assert.AreEqual()
Assert.IsNotNull()
Assert.AreEqual()
Assert.IsNotNull()
Assert.Equal()
Assert.NotNull()
 
Note
  1. N.A. stands for blank (you don’t have to write anything as a replacement for the attribute).
  2. No verbs (like are, is, etc) are used in the method names of Xunit framework.
  3. You can refer to this for more such differences.
Examples (templates)
 
MS Test 
  1. using System;  
  2. using System.Threading.Tasks;  
  3. using SampleApplication.OrderAdmin.Core.Services.OrderCreation;  
  4. using Moq;  
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  6.   
  7.   
  8. namespace SampleApplication.OrderAdmin.Core.Tests.Services.OrderCreation  
  9. {  
  10.   [TestClass]  
  11.   public class OrderCreationServiceTests  
  12.   {  
  13.   
  14.     [TestInitialize]  
  15.     public void TestInitialize()  
  16.     {  
  17.       
  18.     }  
  19.   
  20.     [TestMethod]  
  21.     public async Task SampleTestMethod1()  
  22.     {  
  23.       
  24.     }  
  25.   
  26.     [DataTestMethod]  
  27.     [DataRow(3, 5, Membership.Basic)]  
  28.     [DataRow(0, 4, Membership.Basic)]  
  29.     [DataRow(8, 5, Membership.Premium)]  
  30.     [DataRow(5, 4, Membership.Premium)]  
  31.     public void SampleTestMethod2(double sampleparameter1, int sampleparameter2, Membership sampleparameter3)  
  32.     {  
  33.       
  34.     }  
  35.   }  
  36. }  
Nunit
  1. using System;  
  2. using System.Threading.Tasks;  
  3. using SampleApplication.OrderAdmin.Core.Services.OrderCreation;  
  4. using Moq;  
  5. using NUnit.Framework;  
  6.   
  7. namespace SampleApplication.OrderAdmin.Core.Tests.Services.OrderCreation  
  8. {  
  9.   [TestFixture]  
  10.   public class OrderCreationServiceTests  
  11.   {  
  12.   
  13.     [SetUp]  
  14.     public void TestInitialize()  
  15.     {  
  16.         
  17.     }  
  18.   
  19.     [Test]  
  20.     public async Task SampleTestMethod1()  
  21.     {  
  22.       
  23.     }  
  24.   
  25.     [TestCase(3, 5, Membership.Basic)]  
  26.     [TestCase(0, 4, Membership.Basic)]  
  27.     [TestCase(8, 5, Membership.Premium)]  
  28.     [TestCase(5, 4, Membership.Premium)]  
  29.     public void SampleTestMethod2(double sampleparameter1, int sampleparameter2, Membership sampleparameter3)  
  30.     {  
  31.         
  32.     }  
  33.   }  
  34. }  
Xunit
  1. using System;    
  2. using System.Threading.Tasks;    
  3. using WiredBrainCoffee.CupOrderAdmin.Core.Services.OrderCreation;    
  4. using Moq;    
  5. using Xunit;    
  6.     
  7. namespace WiredBrainCoffee.CupOrderAdmin.Core.Tests.Services.OrderCreation    
  8. {    
  9.   public class OrderCreationServiceTests    
  10.   {    
  11.     
  12.     public OrderCreationServiceTests()    
  13.     {    
  14.           
  15.     }    
  16.     
  17.     [Fact]    
  18.     public async Task SampleTestMethod1()    
  19.     {    
  20.           
  21.     }    
  22.     
  23.     [Theory]    
  24.     [InlineData(3, 5, Membership.Basic)]    
  25.     [InlineData(0, 4, Membership.Basic)]    
  26.     [InlineData(8, 5, Membership.Premium)]    
  27.     [InlineData(5, 4, Membership.Premium)]    
  28.     public void SampleTestMethod2(double sampleparameter1, int sampleparameter2, Membership sampleparameter3)    
  29.     {    
  30.         
  31.     }    
  32.   }    
  33. }