2.Ignore與Explicity:Ignore屬性表示在NUnit中Run Test的時候忽略標記為Ignore的方法;標記為Explicity的測試方法,當前僅當在被單獨選中時才Run。
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!");
}
/// <summary>
/// 聲明為Ignore的TestCase將在NUnit運行時被忽略
/// </summary>
[Test]
[Ignore("TestAdd were ignored")]
public void TestAdd()
{
int result = cac.Add(a, b);
Assert.AreEqual(12, result);
}
/// <summary>
/// 在NUnit當中聲明為Explicit的方法,當且僅當在NUnit中被選中的時候才運行
/// </summary>
[Test,Explicit]
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é)果: