有没有办法用c#读取测试属性值,如[Owner =“Tester”]

问题描述 投票:-2回答:2

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

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

如果有办法使用c#将test owner属性的值作为字符串获取

c# unit-testing mstest coded-ui-tests
2个回答
1
投票

我认为TestContext可以帮助你。

var category = (string) TestContext.CurrentContext.Test.Properties.Get("Category");

我说的是运行时,否则,你可以使用this(tnx到Mark Benningfield


0
投票
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);
© www.soinside.com 2019 - 2024. All rights reserved.