首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取TestInitialize中测试方法的MethodBase

获取TestInitialize中测试方法的MethodBase
EN

Stack Overflow用户
提问于 2015-03-10 23:15:01
回答 1查看 253关注 0票数 0

在测试之前的设置中,我希望获得实际的测试方法,这样就可以获得附加到它的属性。我不想在每次测试开始时调用MethodBase.GetCurrentMethod()。我该如何解决这个问题呢?

代码语言:javascript
复制
[TestInitialize]
public void setUp()
{
    MethodBase _Method = (MethodBase.GetCurrentMethod());
    CurrentTestCaseId = GetAttribute(_Method);
    Log.Info("Starting to execute Test Case ID: " +CurrentTestCaseId);        
}

[TestMethod()]
[TestCategory("UserLogin")] 
[TestId("TC26828")]
public void TestUserLogin()
{
    GetPageFactory().GetLoginPage(Driver).logUserIntoApplication("bob", "bob");
}
EN

回答 1

Stack Overflow用户

发布于 2015-03-10 23:43:45

如果您使用的是.NET 4.5+,则可以使用CallerMemberNameAttribute来帮助获取方法的名称。您可以使用它来获取MethodInfo,它可以让您访问该方法的属性。

代码语言:javascript
复制
public void Setup([CallerMemberName] string method = null)
{
     Type type = typeof(MyTestClass);
     MethodInfo info = type.GetMethod(name);
     CurrentTestCaseId = GetAttribute(info);
     Log.Info("Starting to execute Test Case ID: " +CurrentTestCaseId); 
}

[TestId("someID")]
public void MyTest()
{
    Setup(); //"method" should be automatically provided as "MyTest"
}

public TestIdAttribute GetAttribute(MethodInfo info)
{
    return (TestIdAttribute)info.GetCustomAttributes(typeof(TestIdAttribute), false).First();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28967409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档