2017-03-28 72 views
0

常常我必須發生在對諸如如何對必須成對出現的函數進行單元測試?

//Example 
startSomething() 
... 
stopSomething() 

//Example 
openSomething() 
... 
closeSomething() 

而單元測試startSomething()openSomething()是容易的,因爲它們不需要現有條件/設置的某些功能,應該怎麼單元測試它們的對應需要在現有被稱爲?

回答

0

大多數單元測試框架都具有安裝測試,這些安裝測試在測試用例之前/之後和/或每個測試用例中調用。以NUnit爲例:

[TestFixture] 
public class MyTest 
{ 
    [OneTimeSetup] 
    public void GeneralSetup() 
    { ... } //Called before starts all test cases 

    [OneTimeTearDown] 
    public void GeneralTearDown() 
    { ... } //Called after all test cases are finished 

    [Setup] 
    public void Setup() 
    { ... } //Called before each test case 

    [TearDown] 
    public void TearDown() 
    { ... } //Called after each test case 

    [Test] 
    public void Test1() 
    { ... } 

    [Test] 
    public void Test2() 
    { ... } 
} 
相關問題