1.TestFixtureSetUp与TestFixtureTearDown的用法
TestFixtureSetUp:在所有当前选中的标记为Test的方法运行之前运行
TestFixtureTearDown:在所有当前选中的标记为Test的方法运行后运行
using System;
using System.Text;
using NUnit.Framework;
namespace NUnitTest
{
[TestFixture]
public class CaculatorTest
{
private Caculator cac;
private int a;
private int b;
/// <summary>
/// 声明为TestFixtureSetUp的方法将在所有选中的TestCase调用之前调用,通常用来初始化数据库连接
/// </summary>
[TestFixtureSetUp]
public void InitUtility()
{
Console.Write("Caculator Invoked!");
}
/// <summary>
/// 声明为TestFixtureTearDown的方法将在所有选中的TestCase调用之后调用,通常用来销毁数据库连接
/// </summary>
[TestFixtureTearDown]
public void DestroyUtility()
{
Console.Write("Caculator Destroy!");
}
[SetUp]
public void Init()
{
cac = new Caculator();
a = 10;
b = 2;
Console.Write("Data Init!");
}
[TearDown]
public void Destroy()
{
Console.Write("TestCase Destroy!");
}
[Test]
public void TestAdd()
{
int result = cac.Add(a, b);
Assert.AreEqual(12, result);
}
[Test]
public void TestMinus()
{
int result = cac.Substract(a, b);
Assert.AreEqual(8, result);
}
[Test]
public void TestMultip()
{
int result = cac.Multip(a, b);
Assert.AreEqual(20, result);
}
[Test]
public void TestDivide()
{
int result = cac.Divide(a, b);
Assert.AreEqual(5, result);
}
}
}
运行结果: