liangx85

利用NUnit测试异常

     作为VS.net现在常用的单体测试工具NUnit也可以对程序设定的异常进行测试,以下是测试实例。

     1.测试对象抛出一个系统异常。

    public virtual void Sample()

    {

        throw new  System.AggregateException();

    }

     2.测试程序

        测试发生异常 

     [Test]

     public void TestDeleteFile()

     {

         System.AggregateException exception = null;

         try

         {

             DeleteFile();

         }

         catch(System.AggregateException ex)

         {

             exception = ex;

         }

         Assert.AreNotEqual(null, exception);

         Assert.AreEqual(typeof(System.AggregateException),exception.GetType());

      }

        测试未发生异常

     public void TestDeleteFile()

     {

         System.AggregateException exception2 = null;

         try

         {

 

             DeleteFile();

         }

         catch(System.AggregateException ex)

         {

             exception2 = ex;

         }

         Assert.AreNotEqual(null, exception);

      }      

posted on 2012-05-30 15:25  liangx85  阅读(599)  评论(1编辑  收藏  举报

导航