Click here to Skip to main content
13,045,285 members (48,441 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 29 Jan 2011

xUnit.Net – Running the tests (TestCategory)

, 29 Jan 2011
Rate this:
Please Sign up or sign in to vote.
A solution for converting MSTest TestCategory attribute to an equivalent implementation in xUnit.Net

In my previous post, I showed an example about converting an MSTest class to xUnit.Net, and now I want to provide a solution for converting the MSTest TestCategory attribute to an equivalent implementation in xUnit.Net.

MSTest allows us to run the test that belongs to a specific category. Let’s see a solution on how this can be accomplished in xUnit.Net.

using Xunit;
using Xunit.Extensions;

namespace xUnitCustomizations
    public class TestClass
        [Fact, TestCategory("Unit")]
        public void FastTest()
            Debug.WriteLine("fast test executed");

        [Fact, TestCategory("Integration")]
        public void SlowTest()
            Debug.WriteLine("slow test executed");

The CustomTestClassCommandAttribute attribute is used to indicate that a custom test runner will be used:

public class CustomTestClassCommandAttribute : RunWithAttribute
    public CustomTestClassCommandAttribute() : base(typeof(CustomTestClassCommand)) { }

CustomTestClassCommand is the class that implements ITestClassCommand and acts as the runner for the test fixture.

public class CustomTestClassCommand : ITestClassCommand
    // Delegate most of the work to the existing TestClassCommand class so that we
    // can preserve any existing behavior (like supporting IUseFixture<T>).
    readonly TestClassCommand cmd = new TestClassCommand();
    Random randomizer = new Random();

    #region ITestClassCommand Members
    public object ObjectUnderTest
        get { return cmd.ObjectUnderTest; }

    public ITypeInfo TypeUnderTest
        get { return cmd.TypeUnderTest; }
        set { cmd.TypeUnderTest = value; }

    public int ChooseNextTest(ICollection<IMethodInfo> testsLeftToRun)
        return randomizer.Next(testsLeftToRun.Count);

    public Exception ClassFinish()
        return cmd.ClassFinish();

    public Exception ClassStart()
        return cmd.ClassStart();

    public IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo testMethod)
        return cmd.EnumerateTestCommands(testMethod);

    public bool IsTestMethod(IMethodInfo testMethod)
        return cmd.IsTestMethod(testMethod);

    public IEnumerable<IMethodInfo> EnumerateTestMethods()
        string category;

        foreach (IMethodInfo method in cmd.EnumerateTestMethods())
            category = string.Empty;
            foreach (IAttributeInfo attr in method.GetCustomAttributes(
                category = attr.GetPropertyValue<string>("Value");

            if (category.ToLower().Contains("unit"))
            // We can make this configurable
                yield return method;

The method public IEnumerable<IMethodInfo> EnumerateTestMethods() filters the tests methods by TestCategory's attribute Value; note that we can make this configurable so we can configure our CI server to run the unit test as soon as there are changes in the repository to provide quick feedback and schedule (e.g., once a day) the execution slower to like an integration or Web UI test.


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

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 29 Jan 2011
Article Copyright 2011 by mariangemarcano
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid