Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

output the name of the current test via use of code in [Setup] method

Tags:

c#

nunit

I've current got a line at the top of all my tests that looks like this:

Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); 

Seems like it'd be nice if I could just put this in my Init method (the one tagged with [Setup]). I thought this would work but no matter what number I put in for the stack frame the closest I can get is Init. Not what I want, but very close:

string methodName = new StackFrame(0).GetMethod().Name;
Console.WriteLine(methodName);

I think this just might not be possible, given the way that Nunit runs tests.

Why do this, you say? Because in my console output it'd be nice to see where a new test started so if they both hit the same code and output different values, I'll know which one did what, without having to debug.

like image 400
jcollum Avatar asked May 12 '09 22:05

jcollum


1 Answers

The following could help me:

string testName = NUnit.Framework.TestContext.CurrentContext.Test.Name;
string testFullName = NUnit.Framework.TestContext.CurrentContext.Test.FullName;
like image 78
Miwil Avatar answered Sep 28 '22 04:09

Miwil