TUnit - is there a way to name test cases like in NUnit #1711
Replies: 2 comments 1 reply
-
It's a bit of extra work - But you can create a custom attribute that implements The method you implement will be hit when a test with that attribute on it is discovered. You could then change the name by calling a method on the context object. Then add that attribute to your test. public class HtmlContentArgumentsDisplayNameAttribute : Attribute, ITestDiscoveryEventReceiver
{
public void OnTestDiscovery(DiscoveredTestContext discoveredTestContext)
{
var newTestDisplayName = discoveredTestContext.TestContext.GetTestDisplayName()
.Replace(NullContent, "NullContent")
.Replace(EmptyContent, "EmptyContent")
.Replace(ShortContent, "ShortContent")
.Replace(LongContent, "LongContent");
discoveredTestContext.SetDisplayName(newTestDisplayName);
}
} [Test]
[HtmlContentArgumentsDisplayNameAttribute]
[Arguments(NullContent)]
[Arguments(EmptyContent)]
[Arguments(ShortContent)]
[Arguments(LongContent)] |
Beta Was this translation helpful? Give feedback.
-
Interesting - quite a different approach than NUnit but I like how you can write code to derive the name rather than entering text. I did keep trying after posting, and I came up with an approach a bit closer to how it was in NUnit. I added a second parameter to the test which is the name of the test case. I then used the DisplayName attribute to set the name of the test to be that second parameter. It gave similar functionality to NUnit but you don't get the flexibility to write code to derive the name.
|
Beta Was this translation helpful? Give feedback.
-
I am almost finished migrating from NUnit to TUnit with just one outstanding issue. With NUnit it is possible to name each TestCase as shown below. Normally this would just be a cosmetic thing, but I am using VerifyTUnit to compare HTML output of Blazor components, with the TestCase name used for the Verify output filename.
TUnit names the test cases based on the argument values. In my case this will include the HTML and that's not great to use in the Verify output filenames. I can add logic to set the output filename based on the input values, but would prefer to be able to set the test case names. Is there a way in TUnit?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions