Customize Test Name in Xunit2
Asked Answered
P

2

8

When I run my tests today with xUnit v2, I typically use a naming convention like:

[Fact(DisplayName= "Method Will Do Something")] public void Method_Will_Do_Something() { }

What extensibility point can I plug into that will allow me set my test display name based on the naming conventions of my test method?

Pelt answered 27/5, 2015 at 19:28 Comment(0)
L
7

The simplest way: Custom fact attribute, discoverer, and test case.

Example: https://github.com/xunit/samples.xunit/tree/master/RetryFactExample

For your custom test case, derive from XunitTestCase and override the Initialize() method. After calling base.Initialize(), set the DisplayName property appropriately.

You can see the default behavior for XunitTestCase here: https://github.com/xunit/xunit/blob/master/src/xunit.execution/Sdk/Frameworks/XunitTestCase.cs

Likeness answered 9/6, 2015 at 20:45 Comment(0)
L
3

Create a custom class for your fact

public sealed class MyFactAttribute : FactAttribute
{
    public MyFactAttribute([CallerMemberName] string memberName = null)
    {
        DisplayName = memberName;
    }
}

And use as follows

[MyFact]
public void FileSystemProvider_Sync()
Longcloth answered 13/7, 2017 at 15:56 Comment(2)
Works like a charm, thnxGarrygarson
thanks, Brad answer is also correct but I thought a very simple here is the code just copy and paste it would be helpful.Longcloth

© 2022 - 2024 — McMap. All rights reserved.