2013-05-10 16 views
7

Jak korzystać z Xunit, aby uzyskać nazwę aktualnie uruchomionego testu?Uzyskaj nazwę testu bieżącego w Xunit

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:
W szczególności szukam zamiennika NUnits TestContext.CurrentContext.Test.Name nieruchomości.

Odpowiedz

8

Można użyć BeforeAfterTestAttribute rozwiązać sprawę. Jest kilka sposobów na zajęcie się twoim problemem przy użyciu Xunit, co oznaczałoby utworzenie podklasy TestClassCommand lub FactAttribute i TestCommand, ale myślę, że jest to najprostszy sposób na BeforeAfterTestAttribute. Sprawdź kod poniżej.

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); 
     } 
    } 
} 
0

Nie mogę rozmawiać z xUnit ... ale to działało dla mnie w VS Testing. może być wart strzały.

referencyjny: How to get the name of the current method from code

Przykład:

[TestMethod] 
public void TestGetMethod() 
{ 
    StackTrace st = new StackTrace(); 
    StackFrame sf = st.GetFrame(0); 
    MethodBase currentMethodName = sf.GetMethod(); 
    Assert.IsTrue(currentMethodName.ToString().Contains("TestGetMethod")); 
} 
+0

Dziękuję za odpowiedź. Zdaję sobie sprawę z tej opcji (używając jej teraz) i szukam innej opcji. –

Powiązane problemy