2013-05-10 20 views
7

Xunit'i kullanarak, şu anda yürütülen testin adını nasıl alabilirim?Xunit'te test çalıştırmanın adını öğrenin

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

Düzenleme:
Özellikle, NUnits için bir yedek TestContext.CurrentContext.Test.Name özellik arıyorum.

cevap

8

Sen soruna çözüm bulma BeforeAfterTestAttribute kullanabilirsiniz. Sorununuzu Xunit kullanarak, TestClassCommand veya FactAttribute ve TestCommand alt sınıfını yapmak için kullanmanın bazı yolları vardır, ancak BeforeAfterTestAttribute'un en basit yol olduğunu düşünüyorum. Aşağıdaki kodu kontrol edin.

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

xUnit ile konuşamıyorum ... ama bu VS Testi'nde benim için işe yaradı. Denemeye değebilir.

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

Örnek:

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

Cevabınız için teşekkür ederiz. Bu seçeneğin farkındayım (hemen onu kullanarak) ve başka bir seçenek arıyorum. –

İlgili konular