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调用之前调用,通常用来初始化数据库连接
/// </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!");
}
/// <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);
}
}
}
运行结果: