如何从单元测试中获得在运行单元测试方法的名字吗?

编程入门 行业动态 更新时间:2024-10-24 02:00:49
本文介绍了如何从单元测试中获得在运行单元测试方法的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何从单元测试中获得的单元测试的名字吗?

How to get the unit test name from the within unit test?

我有一个BaseTestFixture类里面的方法如下:

I have the below method inside a BaseTestFixture Class:

public string GetCallerMethodName() { var stackTrace = new StackTrace(); StackFrame stackFrame = stackTrace.GetFrame(1); MethodBase methodBase = stackFrame.GetMethod(); return methodBase.Name; }

我的测试夹具类从基地之一继承:

My Test Fixture class inherits from the base one:

[TestFixture] public class WhenRegisteringUser : BaseTestFixture { }

和我有以下系统测试:

[Test] public void ShouldRegisterThenVerifyEmailThenSignInSuccessfully_WithValidUsersAndSites() { string testMethodName = this.GetCallerMethodName(); // }

当我运行这个从Visual Studio中,它返回预期我的测试方法的名称。

When I run this from within the Visual Studio, it returns my test method name as expected.

在此运行由TeamCity的,而不是 _InvokeMethodFast()返回这似乎是TeamCity的生成在运行时供自己使用的方法。

When this runs by TeamCity, instead _InvokeMethodFast() is returned which seems to be a method that TeamCity generates at runtime for its own use.

所以,我怎么能在运行时得到的测试方法的名字吗?

So how could I get the test method name at runtime?

推荐答案

如果您正在使用的 NUnit的2.5.7 / 2.6 您可以使用的的TestContext类:

If you are using NUnit 2.5.7 / 2.6 you can use the TestContext class:

[Test] public void ShouldRegisterThenVerifyEmailThenSignInSuccessfully() { string testMethodName = TestContext.CurrentContext.Test.Name; }

更多推荐

如何从单元测试中获得在运行单元测试方法的名字吗?

本文发布于:2023-10-31 04:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元   单元测试   名字   方法   测试中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!