Click here to Skip to main content
13,559,281 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 3 Mar 2011
Licenced CPOL

xUnit.Net – Running the tests (RunAfterTestFailed Custom Attribute)

, 3 Mar 2011
Rate this:
Please Sign up or sign in to vote.
xUnit.Net – Running the tests (RunAfterTestFailed Custom Attribute)

In my last post, I created a Custom TestCategory Attribute and custom xUnit.Net TestClassCommand to run test 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:

using Xunit;
using Xunit.Extensions;

namespace xUnitCustomizations
    public class TestClass
        [Fact, TestCategory("Unit")]
        public void FailedTest1()

        [Fact, TestCategory("Unit")]
        public void FailedTest2()
            throw new InvalidOperationException();

        public void TestFailed()
            Debug.WriteLine("Run this whenever a test fails");

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

public class CustomTestClassCommand : ITestClassCommand

    #region ITestClassCommand Members


    public IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo testMethod)
        string skipReason = MethodUtility.GetSkipReason(testMethod);

        if (skipReason != null)
            yield return new SkipCommand(testMethod, 
                MethodUtility.GetDisplayName(testMethod), skipReason);
            foreach (var testCommand in cmd.EnumerateTestCommands(testMethod))
                yield return new AfterTestFailedCommand(testCommand);

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

public class AfterTestFailedCommand : DelegatingTestCommand
    public AfterTestFailedCommand(ITestCommand innerCommand)
    : base(innerCommand)

    public override MethodResult Execute(object testClass)
        MethodResult result = null;
        Type testClassType = testClass.GetType();
            result = InnerCommand.Execute(testClass);
            if (!(result is PassedResult))
                foreach (MethodInfo method in testClassType.GetMethods())
                    foreach (
                    var attr in method.GetCustomAttributes(
                    typeof(RunAfterTestFailedAttribute), true))
                        method.Invoke(testClass, null);
        return result;

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Denmark Denmark
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Roland Ebner21-Aug-12 20:33
memberRoland Ebner21-Aug-12 20:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 3 Mar 2011
Article Copyright 2011 by mariangemarcano
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid