我常常有一些必须成对出现的功能,例如
//Example
startSomething()
...
stopSomething()
//Example
openSomething()
...
closeSomething()
虽然单元测试startSomething()
和openSomething()
很容易,因为它们不需要先前的条件/设置,我应该如何对需要事先调用的对应单元进行单元测试?
答案 0 :(得分: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()
{ ... }
}