Nunit学习笔记
http://www.36sign.com/nunit/index.html
Nunit文档学习笔记
断言
如果一个断言失败,方法的调用不会返回值,并且会报告一个错误。如果一个测试包含多个断言,那些紧跟失败断言的断言都不会执行,因为此原因,通常每个测试方法最好只有一个断言。
同等断言:
Assert.AreEqual( x, y )
Assert.AreNotEqual( x, y )
一致性断言:
Assert.AreSame( x, y )和Assert.AreNotSame( x, y )测试2个参数引用的对象是否是同一个对象。
Assert.Contains( object anObject, IList collection );用来测试在一个数组或列表里是否包含该对象。
比较断言:
Assert.Greater( x, y )
Assert.Less( x, y )
类型断言:
Assert.IsInstanceOfType( Type expected, object actual );
Assert.IsNotInstanceOfType( Type expected, object actual );
Assert.IsAssignableFrom( Type expected, object actual );
Assert.IsNotAssignableFrom( Type expected, object actual );
条件测试:
Assert.IsTrue( bool condition );
Assert.IsFalse( bool condition);
Assert.IsNull( object anObject );
Assert.IsNotNull( object anObject );
Assert.IsNaN( double aDouble );
Assert.IsEmpty( string aString );
Assert.IsNotEmpty( string aString );
Assert.IsEmpty( ICollection collection );
Assert.IsNotEmpty( ICollection collection );
实用方法:
Assert.Fail();
Assert.Ignore();
字符串断言:
StringAssert.Contains( string expected, string actual );
StringAssert.StartsWith( string expected, string actual );
StringAssert.EndsWith( string expected, string actual );
StringAssert.AreEqualIgnoringCase( string expected, string actual );
Assert.Fail方法为你提供了创建一个失败测试的能力,这个失败是基于其他方法没有封装的测试。对于开发你自己的特定项目的断言,它也很有用。这里有一个使用它的例子,这个例子创建一个私有的断言来测试字符串是否包含一个期望的值。
public void AssertStringContains( string expected, string actual )
{
AssertStringContains( expected, actual, string.Empty );
}
public void AssertStringContains( string expected, string actual,
string message )
{
if ( actual.IndexOf( expected ) < 0 )
Assert.Fail( message );
}
Assert.Ignore方法为你提供在运行时动态忽略一个测试或者一个测试套件(suite)的能力。它可以在一个测试,一个setup或fixture setup的方法中调用。我们建议你只在无效的案例中使用。它也为更多扩展的测试包含或排斥提供了目录能力,或者你可以简单将不同情况下运行的测试运行分解到不同的程序集。
TestFixtureAttribute
这个属性标记一个类包含了测试,而且可选的还有setup或teardown方法。
作为一个测试fixture的类有一些限制:
必须是一个公共的导出类型,否则NUnit不会识别它。
它必须有一个缺省的构造子,否则Nunit不能构建他。
构造子不应该有任何方面的负面影响,因为在一个对话的过程中,NUnit可能构造类多次。
TestAttribute
一个测试方法的签名定义如下:
public void MethodName()
注意这里必须没有参数。如果程序员将测试方法标记为不正确的签名,它不会运行,而且会出现在运行程
SetUpAttribute
本属性在一个TestFixture里使用,并提供一组常用的功能,这些功能是在每个测试方法被调用之前来完成
SetUp继承
SetUp属性是从任何基类继承而来。因此,如果基类已经定义了一个SetUp方法,那么这个方法会在每个派
TearDownAttribute
本属性在一个TestFixture里使用,并提供一组常用的功能,这些功能是每个测试方法被调用之前来完成的
TearDown继承
TearDown属性是从任何的基类继承而来。因此,如果基类已经定义了一个TearDown方法,那么这个方法
ExpectedExceptionAttribute
[ExpectedException(typeof(InvalidOperationException))]
[ExpectedException("System.InvalidOperationException")]
PlatformAttribute
平台属性用来指定一个测试或fixture运行的平台。使用无大小写之分的字符串来指定平台,也通过单独使用
[Platform("NET-2.0")]
[Platform(Exclude="Win98,WinME")]
CategoryAttribute
Category提供了一个套件的其他方法来处理测试的分组。不管是单个测试案例还是测试fixture,都可以将它们指定为属于某个特定的分类。当使用分类时,仅有可选目录的测试可以运行。这些没有选择的组的测试是不会报告的。
ExplicitAttribute
Explicit属性会忽略一个测试或测试Fixture,直到它被显式的选择运行。
[Explicit]
SuiteAttribute
Suite属性用来定义基于用户偏好的套件(suite)的子集。
{
using System;
using NUnit.Framework;
{
[Suite]
public static TestSuite Suite
{
get
{
TestSuite suite = new TestSuite("All Tests");
suite.Add(new OneTestCase());
suite.Add(new Assemblies.AssemblyTests());
suite.Add(new AssertionTest());
return suite;
}
}
}
}
IgnoreAttribute
Ignore属性一段时间内不会运行一个测试或者测试fixture。
[Ignore("Ignore a fixture")]
[Ignore("Ignore a test")]
using System;
using System.Collections.Generic;
using System.Text;
namespace bank
{
public class InsufficientFundsException : ApplicationException
{
}
public class Account
{
private float balance;
public float Balance
{
get { return balance; }
}
private float minimunBalance = 10.00F;
public float MinimumBalance
{
get { return minimunBalance; }
}
public void Deposit(float amount)
{
balance += amount;
}
public void Withdraw(float amount)
{
balance -= amount;
}
public void TransferFunds(Account destination, float amount)
{
if (balance - amount < minimunBalance)
{
throw new InsufficientFundsException();
}
destination.Deposit(amount);
Withdraw(amount);
}
}
}
namespace bank
{
using NUnit.Framework;
[TestFixture]
public class AccountTest
{
Account source;
Account destination;
[SetUp]
public void Init()
{
source = new Account();
source.Deposit(200.00F);
destination = new Account();
destination.Deposit(150.00F);
}
[Test]
//[Category("LongRunning")]
[Explicit]
public void TransferFunds()
{
source.TransferFunds(destination, 100.00F);
Assert.AreEqual(250.00F, destination.Balance);
Assert.AreEqual(100.00F, source.Balance);
}
[Test]
[ExpectedException(typeof(InsufficientFundsException))]
public void TransferWithInsufficientFunds()
{
source.TransferFunds(destination, 300.00F);
}
[Test]
[Ignore("Decide how to implement transaction management")]
public void TransferWithInsufficientFundsAtomicity()
{
try
{
source.TransferFunds(destination, 300.00F);
}
catch (InsufficientFundsException expected)
{
}
Assert.AreEqual(200.00F, source.Balance);
Assert.AreEqual(150.00F, destination.Balance);
}
}
}