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調(diào)用之前調(diào)用,通常用來初始化數(shù)據(jù)庫連接
/// </summary>
[TestFixtureSetUp]
public void InitUtility()
{
Console.Write("Caculator Invoked!");
}
/// <summary>
/// 聲明為TestFixtureTearDown的方法將在所有選中的TestCase調(diào)用之后調(diào)用,通常用來銷毀數(shù)據(jù)庫連接
/// </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);
}
}
}
運行結(jié)果: