提问人:Rufus 提问时间:3/28/2017 更新时间:3/28/2017 访问量:20
如何对必须成对出现的函数进行单元测试?
How to unit test functions that must occur in pairs?
问:
很多时候,我有一些函数必须成对出现,例如
//Example
startSomething()
...
stopSomething()
//Example
openSomething()
...
closeSomething()
虽然单元测试很容易,因为它们不需要先验条件/设置,但我应该如何对需要先验调用的对应物进行单元测试?startSomething()
openSomething()
答:
0赞
Arthur Landim
3/28/2017
#1
大多数单元测试框架都有设置测试,这些测试在测试用例之前/之后和/或每个测试用例之前/之后都调用。以 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()
{ ... }
}
评论