Search code examples
c#resharperxunittestdriven.net

Can we customize the output of XUnit and TestDriven.Net


I am using XUnit with TestDriven.Net or Resharper test runner to execute my tests. I really like the BDD style of writing my tests so I was wondering if there is some what that we can modify the output of these frameworks?

I like to name my test with underscores and want to split the test name and format it in Given, When, Then format. Is it at all possible with these tools?


Solution

  • I'm not sure what you try to do. It seems that you want to change test names displayed on a certain test runnder. Displaying test names are actually depended on just test runner(tool). This means that we could customize names or couldn't by which test runner we use.

    Checked out the following code, which could be the code that you want to do. If the code is not the case, at least, I think that it can show you some idea about how to customize test names.

    public class Given_Foo
    {
        [Test]
        public void Then_Bar_returns_correct_result()
        {
            Assert.True(flase, "Check out test names...");
        }
    }
    
    public class TestAttribute : FactAttribute
    {
        public TestAttribute()
        {
        }
    
        protected override IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo method)
        {
            yield return new CustomNamedTestCommand(method);
        }
    }
    
    public class CustomNamedTestCommand : FactCommand
    {
    
        public CustomNamedTestCommand(IMethodInfo method) : base(method)
        {
            this.DisplayName = DisplayName.Replace("_", " ");
        }
    }