• 断言(Assert)与异常(Exception)


      断言是被用来检查非法情况而不是错误情况,即在该程序正常工作时绝不应该发生的非法情况,用来帮助开发人员对问题的快速定位。异常处理用于对程序发生异常情况的处理,增强程序的健壮性、容错性,减少程序使用中对用户不有好的行为,不让(通常也不必)用户知道发生了什么错误。

      实际开发中,我们通常将Assert与异常混淆, 不知道什么时候使用Assert,什么时候使用异常处理。或者不用Assert,将一切情况都归为异常。这样一来,就掩盖了问题,当问题发生的时候,很难进行定位,而这些问题本该是在开发的时候就解决掉的。同时,也增加了开销(在c#中,debug.Assert()编译成release版本时,不会产生任何代码,而try/catch在debug/release版本中都是有代码产生,运行时需要开销)。

     

      Assert类位于Microsoft.VisualStudio.TestTools.UnitTesting 命名空间下,该命名空间提供支持单元测试的类。此命名空间包含许多属性,这些属性为测试引擎标识有关数据源、方法执行顺序、程序管理、代理/主机信息以及部署数据的测试信息。Microsoft.VisualStudio.TestTools.UnitTesting 命名空间还包含自定义单元测试异常。

      MSDN中,有详细的Assert类的公共方法:http://msdn.microsoft.com/zh-cn/library/Microsoft.VisualStudio.TestTools.UnitTesting.Assert(v=vs.100).aspx

      本文简单归类一些简单的使用方法。掌握Assert类,一个BestPractice就是,了解Assert的几个主要方法,然后熟悉其重写方法即可,整理表格如下:

    Name

    Description

    AreEqual(+18)

    AreNotEqual(+18)

    Verifies that specified values are equal(or not equal). The assertion fails if the values are not equal(or equal).

    Displays a message if the assertion fails, and applies the specified formatting to it.(if available)

    AreSame(+3)

    Verifies that specified object variables refer to the same object. The assertion fails if they refer to different objects.

    Displays a message if the assertion fails, and applies the specified formatting to it.(if available)

    Equal

    Determines whether two objects are equal.

    Fail(+3)

    Fails an assertion without checking any conditions.

    Displays a message, and applies the specified formatting to it.(if available)

    InConclusive(+3)

    Indicates that an assertion cannot be proven true or false. Also used to indicate an assertion that has not yet been implemented.  

    Displays a message, and applies the specified formatting to it.(if available)

    IsFalse(+3)

    IsTrue(+3)

    Verifies that a specified condition is false(or true). The assertion fails if the condition is true(not true).

    Displays a message, and applies the specified formatting to it.(if available)

    IsInstanceOfType(+3)

    IsNotInstanceOfType(+3)

    Verifies that a specified object is(or is not) an instance of the specified type. The assertion fails if the type is not(or is) found in the inheritance hierarchy of the object.

    Displays a message, and applies the specified formatting to it.(if available)

    IsNull(+3)

    IsNotNull(+3)

    Verifies that a specified object is null(or not null) . The assertion fails if it is not null(or is null).

    Displays a message if the assertion fails, and applies the specified formatting to it.(if available)

    ReplaceNullChars

    In a string, replaces null characters ('') with "\0".

      

      关于异常处理, 在编写代码的时候,应充分考虑各种具体异常,而不简单的catch到Exception,写出更健壮的代码。

      通常来说,能够用Assert的地方,都可以用try/catch处理 。但这不是好习惯。We need to "Writing Clean Code".

      文中相关扩展连接,  

      Microsoft.VisualStudio.TestTools.UnitTesting Namespace

      Assert Class

      A Unit Testing Walkthrough with Visual Studio Team Test

    Lionden 2015年1月18日

    E-mail:hsdlionden@hotmail.com

    转载请注明原文地址和博客园Liondenhttp://www.cnblogs.com/lionden/

  • 相关阅读:
    UVA10340
    声明顺序 (Bootstrap 编码规范)
    使用SVN小结
    通过LINQ TO SQL类显示数据库表的数据
    大学初进团队感想
    51NOD:1639-绑鞋带
    Codeforces Round #464 (Div. 2) E. Maximize!
    Codeforces Round #464 (Div. 2) D. Love Rescue
    Codeforces Round #464 (Div. 2) C. Convenient For Everybody
    Codeforces Round #464 (Div. 2) B. Hamster Farm
  • 原文地址:https://www.cnblogs.com/lionden/p/AssertAndException.html
Copyright © 2020-2023  润新知