點擊菜單“項目”->“添加類”,打開“添加新項”對話框,將名稱改為BookTest.vb或BookTest.cs、BookTest.jsl,然后點擊“添加”按鈕創(chuàng)建該類并修改代碼。VB代碼如下:
Imports NUnit.Framework
<TestFixture()> _
Public Class BookTest
Dim bo As Book = Nothing
<SetUp()> _
Public Sub Init()
Console.WriteLine("測試開始!")
bo = New Book
Console.WriteLine("book對象被初始化!")
End Sub
<Test()> _
Public Sub testId()
bo.id = "001" '設(shè)置id屬性的值為
'使用Assert查看id屬性的值是否為
Assert.AreEqual("001", bo.id)
Console.WriteLine("id屬性被測試!")
End Sub
<Test()> _
Public Sub testName()
bo.name = "ASP" '設(shè)置name屬性的值為ASP
'使用Assert查看name屬性的值是否為JSP,這是個必然出現(xiàn)錯誤的測試
Assert.AreEqual("JSP", bo.name)
Console.WriteLine("name屬性被測試!")
End Sub
<TearDown()> _
Public Sub Dispose()
Console.WriteLine("book對象將被清理!")
bo = Nothing
Console.WriteLine("測試結(jié)束!")
End Sub
End Class
C#代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
namespace NUnitCS
{
[TestFixture]
public class BookTest
{
private Book book = null;
[SetUp]
public void Init()
{
Console.WriteLine("測試開始!");
book = new Book();
Console.WriteLine("book對象被初始化!");
}
[Test]
public void testId()
{
book.id = "001"; //設(shè)置id屬性的值為
//使用Assert查看id屬性的值是否為
Assert.AreEqual("001", book.id);
Console.WriteLine("id屬性被測試!");
}
[Test]
public void testName()
{
book.name = "ASP"; //設(shè)置name屬性的值為ASP
//使用Assert查看name屬性的值是否為JSP,這是個必然出現(xiàn)錯誤的測試
Assert.AreEqual("JSP", book.name);
Console.WriteLine("name屬性被測試!");
}
[TearDown]
public void Dispose()
{
Console.WriteLine("book對象將被清理!");
book = null;
Console.WriteLine("測試結(jié)束!");
}
}
}
J#代碼如下:
package NUnitJS;
import System.*;
import NUnit.Framework.*;
/** @attribute TestFixture() */
public class BookTest
{
private Book book = null;
/** @attribute SetUp() */
public void Init()
{
Console.WriteLine("測試開始!");
book = new Book();
Console.WriteLine("book對象被初始化!");
}
/** @attribute Test() */
public void testId()
{
book.set_id("001"); //設(shè)置id屬性的值為001
//使用Assert查看id屬性的值是否為001
Assert.AreEqual("001", book.get_id());
Console.WriteLine("id屬性被測試!");
}
/** @attribute Test() */
public void testName()
{
book.set_id("ASP"); //設(shè)置name屬性的值為ASP
//使用Assert查看name屬性的值是否為JSP,這是個必然出現(xiàn)錯誤的測試
Assert.AreEqual("JSP", book.get_name());
Console.WriteLine("name屬性被測試!");
}
/** @attribute TearDown() */
public void Dispose()
{
Console.WriteLine("book對象將被清理!");
book = null;
Console.WriteLine("測試結(jié)束!");
}
}