2010-07-02 37 views
3

我想在我使用的幫助器方法中獲得當前正在執行的NUnit測試。我們實際上在這裏使用NUnit進行集成測試 - 而不是單元測試。測試結束後,我們希望測試完成後清理一些日誌文件。目前,我已經解決這個使用StackFrame類黑客:使用NUnit - 我如何獲得當前正在執行的測試夾具和名稱?

class TestHelper 
{ 
    string CurrentTestFixture; 
    string CurrentTest; 
    public TestHelper() 
    { 
     var callingFrame = new StackFrame(1); 
     var method = callingFrame.GetMethod(); 
     CurrentTest = method.Name; 
     var type = method.DeclaringType; 
     CurrentTestFixture = type.Name; 
    } 
    public void HelperMethod() 
    { 
     var relativePath = Path.Combine(CurrentTestFixture, CurrentTest); 
     Directory.Delete(Path.Combine(Configurator.LogPath, relativePath)); 
    } 
} 

[TestFixture] 
class Fix 
{ 
    [Test] 
    public void MyTest() 
    { 
     var helper = new TestHelper(); 
     //Do other testing stuff 
     helper.HelperMethod(); 
    } 
    [Test] 
    public void MyTest2() 
    { 
     var helper = new TestHelper(); 
     //Do some more testing stuff 
     helper.HelperMethod(); 
    } 
} 

這只是正常,但也有在那裏我想使我的燈具的TestHelper類部分的情況下,像這樣:

[TestFixture] 
class Fix 
{ 
    private TestHelper helper; 

    [Setup] 
    public void Setup() 
    { 
     helper = new TestHelper(); 
    } 

    [TearDown] 
    public void TearDown() 
    { 
     helper.HelperMethod(); 
    } 

    [Test] 
    public void MyTest() 
    { 
     //Do other testing stuff 
    } 
    [Test] 
    public void MyTest2() 
    { 
     //Do some more testing stuff 
    } 
} 

我不能簡單地把這個類放到全局夾具中,因爲有時單個測試會多次使用它,有時一個測試根本不需要使用它。有時候一個測試需要將特定的屬性附加到TestHelper ....類似的東西。

因此,我希望能夠以某種方式獲得當前正在執行的測試,而無需手動重複夾具的名稱並在我正在查看的數千個測試案例中進行測試。

有沒有辦法獲得這樣的信息?

回答

3

NUnit 2.5.7已添加「實驗」TestContext類。它包含的其中一個屬性是TestName。我沒有嘗試過,所以我不知道信息是否可用在TearDown方法中。

+0

作爲一個參考,我測試了這個,TestName和其他屬性在TearDown方法中可用:'if(TestContext.CurrentContext.Test.Name ==「MyTestName」)' – Mike 2017-10-11 14:07:36

0

將您在TestHelper構造函數中的代碼移動到HelperMethod會爲您解決問題嗎?

class TestHelper 
{ 
    public void HelperMethod() 
    { 
     string CurrentTestFixture; 
     string CurrentTest; 

     var callingFrame = new StackFrame(1); 
     var method = callingFrame.GetMethod(); 
     CurrentTest = method.Name; 
     var type = method.DeclaringType; 
     CurrentTestFixture = type.Name; 

     var relativePath = Path.Combine(CurrentTestFixture, CurrentTest); 
     Directory.Delete(Path.Combine(Configurator.LogPath, relativePath)); 
    } 
} 
+0

不,因爲在我的示例中TearDown中正在調用幫助器方法。這就是說,我在這裏大大簡化了事情。真正的幫助器類可以執行諸如在輸出中指定斷言等等,並且需要在其中調用多個方法。 – 2010-07-02 20:53:22

0

通過看起來是,你不能因爲安裝和拆卸點堆棧框架不包括測試方法。

至於清理日誌文件,它看起來像你想每個測試方法有一個日誌文件。也許在這種情況下,它可能會更好:

  1. 使用一個隨機ID作爲日誌文件名的一部分,您可以在輔助類的拆卸中進行清理。然而,如果需要測試方法的名稱是日誌文件的一部分,你可以做..
  2. 可以實現IDisposable待辦事項清理

    [Test] 
    public void MyTest() 
    { 
        using(new TestHelper()) 
        { 
         ... test goes here ... 
        } 
    } 
    
  3. 或者使用PostSharp織上面的代碼爲測試方法中屬性的一部分。

    [Test, TestHelper] 
    public void MyTest() 
    { 
        ... 
    } 
    

[編輯]

固定格式。已添加IDisposable

+0

P.S.the code formatting is screwing with me – aqwert 2010-07-02 21:48:34

+0

這就是我在這裏拼湊起來的簡單例子。真正的測試類不能這樣做,因爲我需要能夠從測試中訪問它來添加內容。目標是消除我目前使用的堆棧幀破解,而不是修復它。 – 2010-07-03 00:18:34