相关疑难解决方法(0)

如何在运行时从NUnit测试运行中获取单元测试方法属性?

我在一个属性中存储有关给定测试的各种信息(多个bug跟踪系统的ID),如下所示:

[TestCaseVersion("001","B-8345","X543")]
public void TestSomethingOrOther()
Run Code Online (Sandbox Code Playgroud)

为了在测试过程中获取此信息,我编写了以下代码:

     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;
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

问题是当在发布模式下通过NUnit运行测试时,应该具有测试名称和这些属性的方法将替换为以下内容:

System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, …
Run Code Online (Sandbox Code Playgroud)

c# reflection attributes nunit

11
推荐指数
1
解决办法
9780
查看次数

标签 统计

attributes ×1

c# ×1

nunit ×1

reflection ×1