Code reliability: Unit testing with XUnit and FluentAssertions in .NET Core 2 apps
Introduction
I decided to write this article because I’m really fan of XUnit and FluentAssertions expecially for its great syntax.
Xunit
xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework. Written by the original inventor of NUnit v2, xUnit.net is the latest technology for unit testing C#, F#, VB.NET and other .NET languages. xUnit.net works with ReSharper, CodeRush, TestDriven.NET and Xamarin. It is part of the .NET Foundation, and operates under their code of conduct. It is licensed under Apache 2 (an OSI approved license).
FluentAssertions
FluentAssertions is the best assertion framework in the .NET realm.
FluentAssertions supports the following .NET versions:
- .NET 4.0, 4.5 and 4.6
- CoreCLR, .NET Native, and Universal Windows Platform
- Windows Store Apps for Windows 8.1
- Silverlight 5
- Windows Phone 8.1
- Windows Phone Silverlight 8.0 and 8.1
- Portable Class Libraries
Fluent Assertions supports the following unit test frameworks:
- MSTest (Visual Studio 2010, 2012 Update 2, 2013 and 2015)
- MSTest2 (Visual Studio 2017)
- NUnit
- XUnit
- XUnit2
- MBUnit
- Gallio
- NSpec
- MSpec
Setup your .NET Core 2 project
Install XUnit
Download it via Nuget package manager:
Or type this command in Nuget package manager console:
PM> Install-Package xunit -Version 2.3.1
Install XUnit Visual Studio runner
This package allow to Visual Studio to discover XUnit unit tests, if you don’t install it, Visual Studio won’t detect them.
Download it via Nuget package manager:
Or type this command in Nuget package manager console:
PM> Install-Package xunit.runner.console -Version 2.3.1
Install FluentAssertions
Download it via Nuget package manager:
Or type this command in Nuget package manager console:
PM> Install-Package FluentAssertions -Version 5.0.0-rc0002
Write your unit tests
Example of a class to unit test:
public class Hello { private string _firstName { get; set; } private string _lastName { get; set; } public Hello(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; } public string HelloMan() { if (string.IsNullOrEmpty(_firstName)) throw new MissingFirstNameException(); return $"Hello {_firstName} {_lastName} !"; } public string Your() { return $"Hello {_firstName} {_lastName} !"; } } public class MissingFirstNameException: Exception { public MissingFirstNameException(): base("FirstName is missing") { } }
Example of a related Unit tests class:
public class HelloTests { [Fact] public void HelloManShouldBeWellFormated() { // Arrange var hello = new Hello("John", "Doe"); //Act var helloMan = hello.HelloMan(); //Assert helloMan .Should() .StartWith("Hello") .And .EndWith("!") .And .Contain("John") .And .Contain("Doe"); } [Fact] public void HelloManShouldBeRaiseExceptionWhenFirstNameIsNotSet() { // Arrange var hello = new Hello("", "Doe"); //Act Action actionHelloMan = () => hello.HelloMan(); //Assert actionHelloMan .Should() .Throw<MissingFirstNameException>() .WithMessage("FirstName is missing"); } }
Now just run your test with “Test” menu in Visual Studio:
That’s it!
Cute unit testing isn’t it? 😉