如何从单元测试中获取单元测试名称?
我在BaseTestFixture类中有以下方法:
我在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 在运行时为自己生成的一个方法。
那么,我该如何在运行时获取测试方法的名称呢?