我将有关给定测试的各种信息(多个错误跟踪系统的 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;
}
}
}
问题是,当在Release模式下通过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 功能.
具体来说,您可以像这样定义和使用测试上下文 Property 属性 TestCaseVersion:
[Test, Property("TestCaseVersion", "001, B-8345, X543")]
public void TestContextPropertyTest()
{
Console.WriteLine(TestContext.CurrentContext.Test.Properties["TestCaseVersion"]);
}
更新顺便说一句,如果您do想使用测试用例版本的多值表示,您可以定义多个属性,如下所示:
[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()
大家好,我对解决方案有疑问,如果像我的示例中那样做,我会得到 null 返回
[SetUp]
public async Task SetUp()
{
var test = TestContext.CurrentContext.Test.Properties["Testkey"];
checkVersionAndTestName.CheckVersionAndTest(name, page, browser);
}
[Test, Property("Testkey", "MES-39432")]
[Retry(3)]
[TestCase("TA-Playwright", "Vf7e92ljKanZ", "//*[@id='__toolbar13']",
TestName = "MES-39432-TestCase1",