C# 如何在运行时从NUnit测试运行中获取单元测试方法属性?
我将关于给定测试的各种信息(多个bug跟踪系统的ID)存储在如下属性中:C# 如何在运行时从NUnit测试运行中获取单元测试方法属性?,c#,reflection,attributes,nunit,C#,Reflection,Attributes,Nunit,我将关于给定测试的各种信息(多个bug跟踪系统的ID)存储在如下属性中: [TestCaseVersion("001","B-8345","X543")] public void TestSomethingOrOther() 为了在测试过程中获取这些信息,我编写了以下代码: public string GetTestID() { StackTrace st = new StackTrace(1); StackFrame sf;
[TestCaseVersion("001","B-8345","X543")]
public void TestSomethingOrOther()
为了在测试过程中获取这些信息,我编写了以下代码:
public string GetTestID()
{
StackTrace st = new StackTrace(1);
StackFrame sf;
for (int i = 1; i <= st.FrameCount; i++)
{
sf = st.GetFrame(i);
if (null == sf) continue;
MethodBase method = sf.GetMethod();
if (method.GetCustomAttributes(typeof(TestAttribute), true).Length == 1)
{
if (method.GetCustomAttributes(typeof(TestCaseVersion), true).Length == 1)
{
TestCaseVersion tcv =
sf.GetMethod().GetCustomAttributes(typeof(TestCaseVersion), true).OfType<TestCaseVersion>()
.First();
return tcv.TestID;
}
}
}
更新
对于任何感兴趣的人,我以以下方式实现代码(这样就可以访问任何属性值,而无需更改任何使用TestCaseVersion属性的现有代码):
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method, AllowMultiple = false)]
public class TestCaseVersion : PropertyAttribute
{
public TestCaseVersion(string testCaseCode, string story, string task, string description)
{
base.Properties.Add("TestId", testCaseCode);
base.Properties.Add("Description", description);
base.Properties.Add("StoryId", story);
base.Properties.Add("TaskId", task);
}
}
public string GetTestID()
{
return TestContext.CurrentContext.Test.Properties["TestId"];
}
也许我不明白你的想法,但为什么不使用更简单的解决方案呢
[TestCase(TestName = "001, B-8345, X543")]
public void TestSomethingOrOther()
如果您可以使用单值测试用例版本字符串(即
“001、B-8345、X543”
而不是“001”、“B-8345”、“X543”
),您应该能够使用NUnit 2.5.7及更高版本中提供的功能
具体而言,您可以定义并使用测试上下文属性TestCaseVersion,如下所示:
[Test, Property("TestCaseVersion", "001, B-8345, X543")]
public void TestContextPropertyTest()
{
Console.WriteLine(TestContext.CurrentContext.Test.Properties["TestCaseVersion"]);
}
[Test, Property("MajorVersion", "001"),
Property("MinorVersion", "B-8345"), Property("Build", "X543")]
public void TestContextPropertyTest()
{
Console.WriteLine(TestContext.CurrentContext.Test.Properties["MajorVersion"]);
Console.WriteLine(TestContext.CurrentContext.Test.Properties["MinorVersion"]);
Console.WriteLine(TestContext.CurrentContext.Test.Properties["Build"]);
}
更新顺便说一句,如果您确实希望使用测试用例版本的多值表示,您可以定义多个属性,如下所示:
[Test, Property("TestCaseVersion", "001, B-8345, X543")]
public void TestContextPropertyTest()
{
Console.WriteLine(TestContext.CurrentContext.Test.Properties["TestCaseVersion"]);
}
[Test, Property("MajorVersion", "001"),
Property("MinorVersion", "B-8345"), Property("Build", "X543")]
public void TestContextPropertyTest()
{
Console.WriteLine(TestContext.CurrentContext.Test.Properties["MajorVersion"]);
Console.WriteLine(TestContext.CurrentContext.Test.Properties["MinorVersion"]);
Console.WriteLine(TestContext.CurrentContext.Test.Properties["Build"]);
}
我对NUnit TestContext中的TestName很满意,但我想通过简单地将它们添加到属性中来提供多个附加值。如果我在TestCase属性中使用它们,我必须将它们作为参数添加到测试方法中,并且如果不向每个测试添加附加代码,它们仍然无法访问。但是,您打算如何使用
GetTestID()
,添加附加信息以断言消息?我在所有测试调用的方法上使用GetTestID来记录测试结果。谢谢,这也是我能找到的唯一解决方案。我希望有一种方法可以获得实际测试方法的任何其他属性,但这必须进行更新!!!如果可以的话,我会给你第二次投票 :-)