【发布时间】:2012-03-28 19:44:34
【问题描述】:
如何从单元测试内部获取单元测试名称?
我在 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 启动,它是否在发布模式下编译?
-
您是否获得堆栈中调用的每个方法的程序集信息?也许你可以在堆栈中寻找你的测试程序集。
-
堆栈跟踪会有所帮助,以查看实际方法名称是否列在调用堆栈的更远位置。
-
您有 60 多个问题没有被接受。对帮助你的人感到尊重吗?
标签: c# unit-testing automated-tests teamcity