胖在一方

出得厅堂入得厨房的胖子

导航

NUnit学习笔记(一)

Posted on 2006-12-21 11:38  胖在一方  阅读(467)  评论(0)    收藏  举报
 NUnit学习笔记(一)

一些简单的断言(Assert)可以用对象游览器查看
      略
[Test]
      
标记测试的方法(Method)
[TestFixture]
      
比较测试的类(Class)
[SetUp]
      
在测试的方法(Method)之前运行(可以建立一些测试的资源)
[TearDown]
      
在测试的方法(Method)之后运行(可以清理一些测试的资源)
[TestFixtureSetUp]
      
可以针对每个测试的类(Class)来设置开始运行的资源
[TestFixtureTearDown]
      可以针对每个测试的类(Class)来清理测试的资源

使用分类Category属性
   用Category 表示的测试方法可以在NUnit中在Category标签栏中显示出来      

 [Test,Category("myCategory")]
    
public void Test_Category()
   {
      
//略代码
   }

临时忽略测试代码 使用Ignore属性
   使用Ignore属性可以临时忽略测试的方法(比如你的测试方法还没有完成),而是其他的测试方法可以正确运行
   NUnit 中会自动标记进度条为黄色

[Test,Ignore("还没有完成的测试方法")]
public void Test_Ignore()
{
    
//代码略
}

ExpectedException从测试代码中抛出可预测的异常
使用ExpectedException属性,如果抛出了可预测的Excepion则测试通过,否则测试不通过

[Test,ExpectedException(typeof(NullReferenceException)]
public void Test_ExpectedException()
{
//代码略
}

示例代码

[TestFixture]
    
public class UnitTest_2
    {
        
        
public UnitTest_2()
        {}

        [Test]
        
public void Test_Method()
        {
            Assert.IsTrue(
true);
        }

        [SetUp]
        
public void Test_SetUp()
        {
            Console.WriteLine(
"SetUp");
        }
        [TearDown]
        
public void Test_TearDown()
        {
            Console.WriteLine(
"TearDown");
        }
        [TestFixtureSetUp]
        
public void Test_TestFixtureSetUp()
        {
            Console.WriteLine(
"TestFixtureSetUp");
        }
        [TestFixtureTearDown]
        
public void Test_TestFixtureTearDown()
        {
            Console.WriteLine(
"TestFixtureTearDown");
        }
    }

在运行NUnit中可以看到输出运行结果
***** UnitTest.UnitTest_1.Test_Category
Test_SetUp
Test_TearDown
***** UnitTest.UnitTest_1.Test_Category_2
Test_SetUp
Test_TearDown
***** UnitTest.UnitTest_1.Test_Largest
Test_SetUp
Test_TearDown
TestFixtureSetUp
***** UnitTest.UnitTest_2.Test_Method