Special thank you for Channel 9 and @skimedic for sharing his unit testing knowledge.

XUnit leverage some of the new features to help developers write cleaner test, as tests should be kept clean and treated as first-class citizens.

  • NUnit was not fully compatible with .NET Core 2 at the time
  • xUnit is aimed at improving test isolation and trying to codify a set of rules to establish a testing standard.
  • xUnit [Fact] and [Theory] attributes are extensible, so you can implement your own testing functionality.xUnit doesn’t use Test Lists and .vsmdi files to keep track of your tests.
  • Microsoft is using xUnit internally, one of its creators is from Microsoft. xUnit was also created by one of the original authors of NUnit.
  • Side-by-side Performance Comparison of testing frameworks can be found here https://blogs.msdn.microsoft.com/visualstudio/2017/11/16/test-experience-improvements/

Where to get it and how to add it to your projects (2 ways)

  1. Use Nuget to add xunit as well as the runner
  2. Add New Project – .NET Core xUnit Test Project

Features

  • It does work with live unit testing
  • Add [Fact] attribute on a method to mark it as a test
  • Use Theory and InlineData as mechanism to pass data into the test
  • [Theory] attribute to mark a method as a test and setup incoming parameters (2 approaches InlineData and MemberData)
  • [InlineData(1,2,3)
    • approach to pass data into the test method (add additional InlineData attributes for multiple runs of same test)
      SNAGHTML2e5cfd1e
  • MemberData(nameof(IEnumerable<object>
    • In the example below we are using a method to return data to be used (however this method could also read data from other files such as text or excel to be returned)
    • approach to pass data into the test method
      SNAGHTML2e5bc0f5
  • xUnit runs it’s tests in parallel to take advantage of today’s processors
    • tests within one class are run serial
    • tests in multiple classes are run parallel
    • can be disabled by creating a test collection (add attribute [Collection({name})], and all within the same collection will be run serial
  • in order to test that an exception is thrown
    • Assert.Throws<InvalidOperationException>(() => ThrowAnError());
      image
  • Setup/Teardown
    • There are no [Setup] and [Teardown] attributes, this is done using the test class’ constructor and an IDisposable. This encourages developers to write cleaner tests.
    • use constructor in place of Setup attributes to prepare tests
    • now use IDisposable to replace teardown i.e. public class ATestClass : IDisposable
      public void Dispose() {
         //something here to clean up
      }
  • for input/output there exists ITestOutputHelper as shown below to output log
    image

Notes

References:

https://xunit.github.io/docs/why-did-we-build-xunit-1.0.html
https://dev.to/hatsrumandcode/net-core-2-why-xunit-and-not-nunit-or-mstest--aei

https://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Unit-Testing-xUnit
https://xunit.github.io/

Comments


Comments are closed

About Us

Web/Mobile Solutions

Our Contacts

Cincinnati, OH 45069