[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void Test1()
{
// Do something that throws an InvalidOperationException
}
}
}
如果上面的程序被執行的時候,如果一旦exception發生,而且這個exception的type(類型信息)是InvalidOperationException 的話,這個test就會順利通過驗證。如果你預期你的程序代碼會產生多個exception的話,你也可以一次使用多個ExpectedException attribute。但是,一個test method應該只測試一件事情,一次測試多個功能是不好的做法,你應該盡量避免之。另外,這個attributes并不會檢查inheirtance的關系,也就是說,如果你的程序代碼產生的exception是繼承自InvalidOperationException 的subclass(子類化)的話,這個test執行的時候將不會通過驗證。簡而言之,當你使用這個attribute的時候,你要明確的指明所預期的exception是哪個type(類型信息)的。
Ignore Attributes簡介
這個attribute你大概不會經常用的,但是一旦需要的時候,這個attribute是很方便使用的。你可以使用這個attribute來標示某個test method,叫Test Runner在執行的時候,略過這個method不要執行。使用這個Ignore attribute的方法如下:
namespace UnitTestingExamples
{
using System;
using NUnit.Framework;
[TestFixture]
public class SomeTests
{
文章來源于領測軟件測試網 http://www.kjueaiud.com/