NUnit单元测试整理之基本语法
TestFixtureSetUp:在所有当前选中的标记为Test的方法运行之前运行;TestFixtureTearDown:在所有当前选中的标记为Test的方法运行后运行;Ignore属性表示在NUnit中Run Test的时候忽略标记为Ignore的方法;标记为Explicity的测试方法,当前仅当在被单独选中时才Run。
1.TestFixtureSetUp与TestFixtureTearDown的用法
TestFixtureSetUp:在所有当前选中的标记为Test的方法运行之前运行
TestFixtureTearDown:在所有当前选中的标记为Test的方法运行后运行
Code
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);
}
}
}
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);
}
}
}
运行结果:
2.Ignore与Explicity:Ignore属性表示在NUnit中Run Test的时候忽略标记为Ignore的方法;标记为Explicity的测试方法,当前仅当在被单独选中时才Run。
Code
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);
}
}
}
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);
}
}
}
运行结果:
3.Category的用法:测试用例分组,看看下面的代码和结果就清楚了
Code
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]
[Category("GroupA")]
public void TestAdd()
{
int result = cac.Add(a, b);
Assert.AreEqual(12, result);
}
[Test]
[Category("GroupA")]
public void TestMinus()
{
int result = cac.Substract(a, b);
Assert.AreEqual(8, result);
}
[Test]
[Category("GroupB")]
public void TestMultip()
{
int result = cac.Multip(a, b);
Assert.AreEqual(20, result);
}
[Test]
[Category("GroupB")]
public void TestDivide()
{
int result = cac.Divide(a, b);
Assert.AreEqual(5, result);
}
}
}
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]
[Category("GroupA")]
public void TestAdd()
{
int result = cac.Add(a, b);
Assert.AreEqual(12, result);
}
[Test]
[Category("GroupA")]
public void TestMinus()
{
int result = cac.Substract(a, b);
Assert.AreEqual(8, result);
}
[Test]
[Category("GroupB")]
public void TestMultip()
{
int result = cac.Multip(a, b);
Assert.AreEqual(20, result);
}
[Test]
[Category("GroupB")]
public void TestDivide()
{
int result = cac.Divide(a, b);
Assert.AreEqual(5, result);
}
}
}
运行结果:
若选中Exclude these Category,运行结果