有没有办法用c#读取像[Owner="Tester"]这样的测试属性值

我想知道是否有办法读取测试属性值?例如

[TestMethod , TestCategory ("Smoke Test"), Priority (1), Owner ("Tester")]

如果有办法使用 c# 将测试所有者属性的值作为字符串获取


慕森卡
浏览 99回答 2
2回答

翻阅古今

我认为TestContext可以帮助你。var category = (string) TestContext.CurrentContext.Test.Properties.Get("Category");我说的是运行时,否则,您可以使用它(tnx to Mark Benningfield)

RISEBY

public class Helper&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public static TValue GetOwnerAttributeValue<TValue>(MethodBase method, Func<OwnerAttribute, TValue> valueSelector)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return method.GetCustomAttributes(typeof(OwnerAttribute), true).FirstOrDefault() is OwnerAttribute attr ? valueSelector(attr) : default(TValue);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }这样调用var testMethod = new StackTrace().GetFrame(1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GetMethod();&nbsp; &nbsp; &nbsp; &nbsp; var testAuthor = Helper.GetOwnerAttributeValue(testMethod, x => x.Owner);
打开App,查看更多内容
随时随地看视频慕课网APP