簡體   English   中英

如何在運行時從NUnit測試運行中獲取單元測試方法屬性?

[英]How to get unit test method attributes at runtime from within an NUnit test run?

我在一個屬性中存儲有關給定測試的各種信息(多個bug跟蹤系統的ID),如下所示:

[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;
                }
            }
        }

問題是當在發布模式下通過NUnit運行測試時,應該具有測試名稱和這些屬性的方法將替換為以下內容:

System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at NUnit.Core.Reflect.InvokeMethod(MethodInfo method, Object fixture, Object[] args)

更新對於任何感興趣的人,我最終以下列方式實現代碼(以便可以訪問任何屬性值,而無需更改任何使用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"];
}

如果您可以使用單值測試用例版本字符串(即"001, B-8345, X543"而不是"001","B-8345","X543" ),您應該可以使用NUnit 2.5.7及更高版本中提供的TestContext功能。

具體來說,您可以像這樣定義和使用測試上下文屬性屬性TestCaseVersion

[Test, Property("TestCaseVersion", "001, B-8345, X543")]
public void TestContextPropertyTest()
{
    Console.WriteLine(TestContext.CurrentContext.Test.Properties["TestCaseVersion"]);
}

更新 BTW,如果您確實想要使用測試用例版本的多值表示,您可以定義多個屬性,如下所示:

[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"]);
}

可能是我不理解你的想法,但為什么不使用更簡單的解決方案呢?

[TestCase(TestName = "001, B-8345, X543")]
public void TestSomethingOrOther()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM