为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
慕森卡 2022-12-04 13:02:42
我想知道是否有办法读取测试属性值?例如[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


查看完整回答
反对 回复 2022-12-04
?
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);


查看完整回答
反对 回复 2022-12-04
  • 2 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号