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
- N.A. stands for blank (you don’t have to write anything as a replacement for the attribute).
- No verbs (like are, is, etc) are used in the method names of Xunit framework.
- You can refer to this for more such differences.
Examples (templates)
MS Test
- using System;
- using System.Threading.Tasks;
- using SampleApplication.OrderAdmin.Core.Services.OrderCreation;
- using Moq;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
-
- namespace SampleApplication.OrderAdmin.Core.Tests.Services.OrderCreation
- {
- [TestClass]
- public class OrderCreationServiceTests
- {
-
- [TestInitialize]
- public void TestInitialize()
- {
-
- }
-
- [TestMethod]
- public async Task SampleTestMethod1()
- {
-
- }
-
- [DataTestMethod]
- [DataRow(3, 5, Membership.Basic)]
- [DataRow(0, 4, Membership.Basic)]
- [DataRow(8, 5, Membership.Premium)]
- [DataRow(5, 4, Membership.Premium)]
- public void SampleTestMethod2(double sampleparameter1, int sampleparameter2, Membership sampleparameter3)
- {
-
- }
- }
- }
Nunit
- using System;
- using System.Threading.Tasks;
- using SampleApplication.OrderAdmin.Core.Services.OrderCreation;
- using Moq;
- using NUnit.Framework;
-
- namespace SampleApplication.OrderAdmin.Core.Tests.Services.OrderCreation
- {
- [TestFixture]
- public class OrderCreationServiceTests
- {
-
- [SetUp]
- public void TestInitialize()
- {
-
- }
-
- [Test]
- public async Task SampleTestMethod1()
- {
-
- }
-
- [TestCase(3, 5, Membership.Basic)]
- [TestCase(0, 4, Membership.Basic)]
- [TestCase(8, 5, Membership.Premium)]
- [TestCase(5, 4, Membership.Premium)]
- public void SampleTestMethod2(double sampleparameter1, int sampleparameter2, Membership sampleparameter3)
- {
-
- }
- }
- }
Xunit
- using System;
- using System.Threading.Tasks;
- using WiredBrainCoffee.CupOrderAdmin.Core.Services.OrderCreation;
- using Moq;
- using Xunit;
-
- namespace WiredBrainCoffee.CupOrderAdmin.Core.Tests.Services.OrderCreation
- {
- public class OrderCreationServiceTests
- {
-
- public OrderCreationServiceTests()
- {
-
- }
-
- [Fact]
- public async Task SampleTestMethod1()
- {
-
- }
-
- [Theory]
- [InlineData(3, 5, Membership.Basic)]
- [InlineData(0, 4, Membership.Basic)]
- [InlineData(8, 5, Membership.Premium)]
- [InlineData(5, 4, Membership.Premium)]
- public void SampleTestMethod2(double sampleparameter1, int sampleparameter2, Membership sampleparameter3)
- {
-
- }
- }
- }