单元测试
针对某个类中的所有方法进行测试和验证
工具<Nunit和TestDriven>
C#代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingNUnit.Framework;
namespaceNUnitCS
{
[TestFixture]
publicclass BookTest
{
Bookbook = null;
[SetUp]
publicvoid Init()
{
Console.WriteLine("测试开始!");
book =new Book();
Console.WriteLine("book对象被初始化!");
}
[Test] //测试方法不能带有参数,否则无法对其进行测试
publicvoid testId()
{
book.id= "001"; //设置id属性的值为
//使用Assert查看id属性的值是否为
Assert.AreEqual("001",book.id);
Console.WriteLine("id属性被测试!");
}
[Test]
publicvoid testName()
{
book.name= "ASP"; //设置name属性的值为ASP
//使用Assert查看name属性的值是否为JSP,这是个必然出现错误的测试
Assert.AreEqual("JSP",book.name);
Console.WriteLine("name属性被测试!");
}
[TearDown]
publicvoid Dispose()
{
Console.WriteLine("book对象将被清理!");
book =null;
Console.WriteLine("测试结束!");
}
}
}