2013-05-10 9 views
7

Xunitを使用して、現在実行中のテストの名前を取得するにはどうすればよいですか?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); 
    } 
    } 

編集:特に
、私はNUnits TestContext.CurrentContext.Test.Nameプロパティの代替を探しています。

答えて

8

あなたはあなたのケースを解決するためにBeforeAfterTestAttributeを使用することができます。 TestClassCommand、またはFactAttributeとTestCommandのサブクラスを作ることになるXunitを使って問題を解決する方法がいくつかありますが、私はBeforeAfterTestAttributeが最も簡単な方法だと思います。以下のコードをチェックしてください。

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と話すことができません...しかし、これは私のVSテストではうまくいきました。ショットに値するかもしれません。

参考: How to get the name of the current method from code

例:

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

ありがとうございました。私はこのオプションを知っています(今使っています)、別のオプションを探しています。 –

関連する問題