On my previous post I created a Custom TestCategory Attribute and a custom xUnit.Net TestClassCommand to run tests by category. Now I want to create a Custom RunAfterTestFailed attribute to run a method whenever a test fails.

We have the following test class,

We will use CustomTestClassCommandAttribute and CustomTestClassCommand, previously created, and make the following
changes to EnumerateTestCommands method since we need a custom command to be able to handle errors when executing test methods.

AfterTestFailedCommand will handle calling the execution of the test method and calling the proper method whenever a test fails.

I liked the extensibility that xUnit.Net provides, even when there isn’t a built in solution for TestCategory and RunAfterTestFailed attributes, I was able to build it by looking at the source code, the unit tests and examples available in CodePlex (the framework has unit tests!).

Hope this series of posts was helpful to get you started in migrating from MSTest to xUnit.Net and writing custom extensions for this framework.