点击菜单“项目”->“添加类”,打开“添加新项”对话框,将名称改为BookTest.vb或BookTest.cs、BookTest.jsl,然后点击“添加”按钮创建该类并修改代码。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" '设置id属性的值为
'使用Assert查看id属性的值是否为
Assert.AreEqual("001", bo.id)
Console.WriteLine("id属性被测试!")
End Sub
<Test()> _
Public Sub testName()
bo.name = "ASP" '设置name属性的值为ASP
'使用Assert查看name属性的值是否为JSP,这是个必然出现错误的测试
Assert.AreEqual("JSP", bo.name)
Console.WriteLine("name属性被测试!")
End Sub
<TearDown()> _
Public Sub Dispose()
Console.WriteLine("book对象将被清理!")
bo = Nothing
Console.WriteLine("测试结束!")
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"; //设置id属性的值为
//使用Assert查看id属性的值是否为
Assert.AreEqual("001", book.id);
Console.WriteLine("id属性被测试!");
}
[Test]
public void testName()
{
book.name = "ASP"; //设置name属性的值为ASP
//使用Assert查看name属性的值是否为JSP,这是个必然出现错误的测试
Assert.AreEqual("JSP", book.name);
Console.WriteLine("name属性被测试!");
}
[TearDown]
public void Dispose()
{
Console.WriteLine("book对象将被清理!");
book = null;
Console.WriteLine("测试结束!");
}
}
}
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"); //设置id属性的值为001
//使用Assert查看id属性的值是否为001
Assert.AreEqual("001", book.get_id());
Console.WriteLine("id属性被测试!");
}
/** @attribute Test() */
public void testName()
{
book.set_id("ASP"); //设置name属性的值为ASP
//使用Assert查看name属性的值是否为JSP,这是个必然出现错误的测试
Assert.AreEqual("JSP", book.get_name());
Console.WriteLine("name属性被测试!");
}
/** @attribute TearDown() */
public void Dispose()
{
Console.WriteLine("book对象将被清理!");
book = null;
Console.WriteLine("测试结束!");
}
}