我想知道是否有办法读取测试属性值?例如[TestMethod , TestCategory ("Smoke Test"), Priority (1), Owner ("Tester")]如果有办法使用 c# 将测试所有者属性的值作为字符串获取
2 回答

翻阅古今
TA贡献1780条经验 获得超5个赞
我认为TestContext可以帮助你。
var category = (string) TestContext.CurrentContext.Test.Properties.Get("Category");
我说的是运行时,否则,您可以使用它(tnx to Mark Benningfield)

RISEBY
TA贡献1856条经验 获得超5个赞
public class Helper
{
public static TValue GetOwnerAttributeValue<TValue>(MethodBase method, Func<OwnerAttribute, TValue> valueSelector)
{
return method.GetCustomAttributes(typeof(OwnerAttribute), true).FirstOrDefault() is OwnerAttribute attr ? valueSelector(attr) : default(TValue);
}
}
这样调用
var testMethod = new StackTrace().GetFrame(1)
.GetMethod();
var testAuthor = Helper.GetOwnerAttributeValue(testMethod, x => x.Owner);
- 2 回答
- 0 关注
- 97 浏览
添加回答
举报
0/150
提交
取消