我想知道是否有辦法讀取測試屬性值?例如[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 關注
- 87 瀏覽
添加回答
舉報
0/150
提交
取消