[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,Signature sig,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,CultureInfo culture) at NUnit.Core.Reflect.InvokeMethod(MethodInfo method,Object fixture,Object[] args)
UPDATE
对于任何有兴趣的人,我以以下方式解决了实现代码(从而可以访问任何属性值,而不用更改任何使用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"]; }
解决方法
具体来说,您可以定义和使用测试上下文Property属性TestCaseVersion,如下所示:
[Test,Property("TestCaseVersion","001,X543")] public void TestContextPropertytest() { Console.WriteLine(TestContext.CurrentContext.Test.Properties["TestCaseVersion"]); }
UPDATE BTW,如果您想要使用测试用例版本的多值表示形式,则可以定义多个属性,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"]); }
原文地址:https://www.jb51.cc/csharp/93887.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。