Friday, March 11, 2016

xUnit.Net replacement to TestCategory in MSTest

If you are migrating your Unit Tests from MSTest to xUnit.Net, you may be wondering how to migrate the TestCategory attribute available in MSTest over to xUnit.Net.

Well, fortunately, you can accomplish that pretty readily using the Trait attribute!!

http://dennymichael.net/2015/01/23/how-to-extendmigrate-traitattribute-to-xunit-v2-and-why-has-become-sealed/

Therefore, instead of something like this in MSTest:

[TestMethod, TestCategory(Unit)]

You will decorate your xUnit.Net Unit Test methods like this:

[Trait("Category", "Unit")]
[Fact]


Even better is that you can simply decorate your entire class with this Trait attribute so that you do not have to categorize your individual Test methods!

[Trait("Category", "Unit")]
public class UnitTest1


Unfortunately, when you run your tests in Test Explorer in Visual Studio, they still display in the following manner:






On the bright side, if you install the Resharper extension for xUnit (https://resharper-plugins.jetbrains.com/packages/CitizenMatt.Xunit/2.3.3), you will instead get Test Windows similar to the following:






You can learn how to download and install the Resharper extension for xUnit from here: https://resharper-plugins.jetbrains.com/packages/CitizenMatt.Xunit/Download

No comments:

Post a Comment