如何从单元测试中获取单元测试名称?如何从单元测试中得到运行时的单元测试方法名称?
我有一个BaseTestFixture类内的以下方法:
public string GetCallerMethodName()
{
var stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();
return methodBase.Name;
}
我的测试夹具类从基地一个继承:
[TestFixture]
public class WhenRegisteringUser : BaseTestFixture
{
}
和我有以下系统测试:
[Test]
public void ShouldRegisterThenVerifyEmailThenSignInSuccessfully_WithValidUsersAndSites()
{
string testMethodName = this.GetCallerMethodName();
//
}
当我在Visual Studio中运行它时,它会按照预期返回我的测试方法名称。
当它由TeamCity运行时,将返回_InvokeMethodFast()
,这似乎是TeamCity在运行时为其自己使用而生成的一种方法。
那么我怎么能在运行时获得测试方法名?
即使从VS启动时它是否以发布模式编译? – 2012-03-12 11:57:54
你是否获得堆栈中调用的每个方法的汇编信息?也许你可以走上堆栈寻找你的测试组件。 – 2012-03-12 12:00:45
堆栈跟踪会帮助您查看实际方法名称是否在调用堆栈的更上方列出。 – 2012-03-12 12:01:03