在测试之前的设置中,我希望获得实际的测试方法,这样就可以获得附加到它的属性。我不想在每次测试开始时调用MethodBase.GetCurrentMethod()。我该如何解决这个问题呢?
[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");
}发布于 2015-03-10 23:43:45
如果您使用的是.NET 4.5+,则可以使用CallerMemberNameAttribute来帮助获取方法的名称。您可以使用它来获取MethodInfo,它可以让您访问该方法的属性。
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();
}https://stackoverflow.com/questions/28967409
复制相似问题