我想知道是否有办法读取测试属性值?例如
[TestMethod , TestCategory ("Smoke Test"), Priority (1), Owner ("Tester")]
如果有办法使用c#将test owner属性的值作为字符串获取
我认为TestContext可以帮助你。
var category = (string) TestContext.CurrentContext.Test.Properties.Get("Category");
我说的是运行时,否则,你可以使用this(tnx到Mark Benningfield)
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);