Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of running test in Xunit

Tags:

Using Xunit, how can I get the name of the currently running test?

  public class TestWithCommonSetupAndTearDown : IDisposable   {     public TestWithCommonSetupAndTearDown ()     {       var nameOfRunningTest = "TODO";       Console.WriteLine ("Setup for test '{0}.'", nameOfRunningTest);     }      [Fact]     public void Blub ()     {     }      public void Dispose ()     {       var nameOfRunningTest = "TODO";       Console.WriteLine ("TearDown for test '{0}.'", nameOfRunningTest);     }   } 

Edit:
In particular, I am looking for a replacement for NUnits TestContext.CurrentContext.Test.Name property.

like image 337
Julian Lettner Avatar asked May 10 '13 14:05

Julian Lettner


People also ask

What is IClassFixture in xUnit?

Important note: xUnit.net uses the presence of the interface IClassFixture<> to know that you want a class fixture to be created and cleaned up. It will do this whether you take the instance of the class as a constructor argument or not.

What is Fact attribute in xUnit?

Fact Attribute [Fact] attribute is used by the xUnit.net test runner to identify a 'normal' unit test - a test method that takes no method arguments.

What is xUnit report?

About XUnit xUnit is a testing framework for . net languages (e.g. C#, F#, VB.NET) mostly focused in unit testing but that can also be used for integration testing. xUnit works with ReSharper, CodeRush, TestDriven.NET and Xamarin.

What is xUnit testing in C #?

Besides the InlineData attribute, xUnit provides you with other ways to define data for theories, like ClassData , where the data source is a class implementing the IEnumerable interface, and MemberData , where the data source is a property or a method.


2 Answers

You can use BeforeAfterTestAttribute to resolve your case. There are some ways to address your issue using Xunit, which would be to make sub-class of TestClassCommand, or FactAttribute and TestCommand, but I think that BeforeAfterTestAttribute is the simplest way. Check out the code below.

public class TestWithCommonSetupAndTearDown {     [Fact]     [DisplayTestMethodName]     public void Blub()     {     }      private class DisplayTestMethodNameAttribute : BeforeAfterTestAttribute     {         public override void Before(MethodInfo methodUnderTest)         {             var nameOfRunningTest = "TODO";             Console.WriteLine("Setup for test '{0}.'", methodUnderTest.Name);         }          public override void After(MethodInfo methodUnderTest)         {             var nameOfRunningTest = "TODO";             Console.WriteLine("TearDown for test '{0}.'", methodUnderTest.Name);         }     } } 
like image 110
Jin-Wook Chung Avatar answered Oct 26 '22 23:10

Jin-Wook Chung


See a similar question in Github where the answer/workaround is to use some injection and reflection in the constructor.

public class Tests   {   public Tests(ITestOutputHelper output)     {     var type = output.GetType();     var testMember = type.GetField("test", BindingFlags.Instance | BindingFlags.NonPublic);     var test = (ITest)testMember.GetValue(output);     } <...>   } 
like image 28
LosManos Avatar answered Oct 27 '22 00:10

LosManos