更改NUnit测试的名称

我希望我的基于NUnit框架的unit testing在Visual Studio测试资源管理器中更具人性化。

例如,我最好不要使用Test_Case_1TestCase1而是选择Test Case #1, Category: First, Category: Second (通过分配[Category]属性中的值),方法名称中不允许使用空格和字符。

我知道它在xUnit中是开箱即用的,但我不能参与其中,因为我使用的是我无法使用xUnit框架实现的自定义。

是否可以使用NUnit重写unit testing显示名称? 到目前为止,我可以看到, TestDetail FullName字段有私有的setter。

是否有其他方法或方法更改NUnit测试的显示名称?

如果使用参数化测试,则支持此function,您可以在添加TestCase属性时指定TestName

如果你没有使用TestCase ,那么你可以将它用作不太理想的工作来实现你想要做的事情。 所以你会像这样声明你的测试:

 [TestCase(null,TestName="Test Case #1, Category: First, Category: Second")] public void TestCase(object ignored) 

这不是理想的,因为它不是程序化的,因此您必须手动键入测试名称,而不是从测试方法的属性生成它。 您还必须将参数传递给方法,这是被ignored nullnull 。 当然,您可以开始使用参数化测试,在这种情况下,您将实际值传递给测试。

 [TestCase(5,TestName="Test Case #1, Category: First, Category: Second")] public void TestCase(int someInput) { Assert.AreEqual(5, someInput); } 

您可以创建自己的Name属性:

 // I used the same namespace for convenience namespace NUnit.Framework { public class NameAttribute : NUnitAttribute, IApplyToTest { public NameAttribute(string name) { Name = name; } public string Name { get; set; } public void ApplyToTest(Test test) { test.Properties.Add("Name", Name); } } } 

然后,您可以像使用常规NUnit属性一样使用它(就像类别和描述一样)。

 [Test, Name("My Awesome Test"), Category("Cool.Tests"), Description("All cool tests")] public void Test313() { // Do something } 

您可以在TestContext中查看数据:

 if (TestContext.CurrentContext.Test.Properties.ContainsKey("Name")) { name = TestContext.CurrentContext.Test.Properties.Get("Name") as string; }