How to get the unit test name from the within unit test?
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 { }
and I have the below system test:
[Test] public void ShouldRegisterThenVerifyEmailThenSignInSuccessfully_WithValidUsersAndSites() { string testMethodName = this.GetCallerMethodName(); // }
When I run this from within the Visual Studio, it returns my test method name as expected.
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?
JUnit 4.9. @Rule public TestRule watcher = new TestWatcher() { protected void starting(Description description) { System. out. println("Starting test: " + description. getMethodName()); } };
You can just use testResult. getMethod(). getMethodName().
A unit test method name is the first thing, anyone, trying to understand your code will read. It is important to write descriptive method names that help readers quickly identify the purpose of a test case. These method names communicate what the code does. A name should be concise, unambiguous, and consistent.
Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With