zoukankan      html  css  js  c++  java
  • NUnit笔记

    注意:单元测试中,Case 与 Case 之间不能有任何关系

            测试方法不能有返回值,不能有参数,测试方法必须声明为 public

    [TestFixture]   

    //声明测试类

    [SetUp]

    //建立,初始化。被声明为 SetUp 的方法为初始化方法。在测试类中有多少个方法就会被执行多少次

    [TearDown]

    //销毁,回收。一般对应 SetUp ,同样是在测试类中有多少个方法,TearDown 就会被执行多少次

    [TestFixtureSetUp] 

    //初始化整个类。在 NUnit 执行一次

    [TestFixtureTearDown] 

    //销毁,回收掉整个类。在NUnit 执行一次

    [Test]  

    //声明该方法为测试方法

    [Ignore(“参数字符串”)]  

    //忽略,标记该测试方法不会在 NUnit 中执行,在 NUnit 运行时会执行该字符串,

    说明:不执行测试的原因等。

    [Explict]  

    //显示的运行,在 NUnit 中需手动指定单独运行该测试方法

    [Cateory(“分组名称”)] 

     //在NUnit 中,Categoryies 选项卡中显示,Case 分组管理,分组执行 Case

    [ExpectedException(typeof(DivideByZeroException))]  

    //定义抛出异常,该异常为除数不能为0

    基础知识:

          [TestFixture]表示:类包含了测试代码(这个特性可以被继承)。这个类必须是公有的,这个类还必须有一个默认构造函数。

      [Test]表示它是一个测试方法。测试方法的返回值必须为void并且不能带有参数

      [SetUp]属性:用来标识方法,在开始所有测试之前执行,用来在测试前初始化一些资源,比如初始化类。

          [TearDown]属性:用来标识方法,在所有测试完成之后执行,用来释放一些资源。

      [Ignore]属性:用来标识方法,指示这个方法由于某些原因暂时不需要测试(比如没有完成相关代码)

    Nunit常用类和方法

    Assert(断言):

      如果断言失败,方法将没有返回,并且报告一个错误。

      如果一个方法中包括了多个断言,在失败的断言之后的所有断言将不会被执行。基于这个原因,最好是为每个测试的断言使用try语句。

      1、测试二个参数是否相等

    Assert.AreEqual( int expected, int actual );
    Assert.AreEqual( decimal expected, decimal actual );
    。。。。

      2、测试二个参数是否引用同一个对象

    Assert.AreSame( object expected, object actual );
    Assert.AreNotSame( object expected, object actual );

      3、测试一个对象是否被一个数组或列表所包含

    Assert.Contains( object anObject, IList collection );

      比较断言:

      4、测试一个对象是否大于另一个对象

    Assert.Greater( int arg1, int arg2 );

      5、测试一个对象是否小于另一个对象

    Assert.Less( int arg1, int arg2 );

      类型断言:

    Assert.IsInstanceOfType( Type expected, object actual );

      条件测试:

    Assert.IsTrue( bool condition );
    Assert.IsFalse( bool condition);
    Assert.IsNull( object anObject );
    Assert.IsNotNull( object anObject );
    Assert.IsNaN( double aDouble );
    Assert.IsEmpty( string aString );
    Assert.IsNotEmpty( string aString );
    Assert.IsEmpty( ICollection collection );
    Assert.IsNotEmpty( ICollection collection );

     字符串断言(StringAssert):提供了许多检验字符串值的有用的方法

    StringAssert.Contains( string expected, string actual );
    StringAssert.StartsWith( string expected, string actual );
    StringAssert.EndsWith( string expected, string actual );
    StringAssert.AreEqualIgnoringCase( string expected, string actual );

  • 相关阅读:
    第三天 moyax
    mkfs.ext3 option
    write file to stroage trigger kernel warning
    download fomat install rootfs script
    custom usb-seriel udev relus for compatible usb-seriel devices using kermit
    Wifi Troughput Test using iperf
    learning uboot switch to standby system using button
    learning uboot support web http function in qca4531 cpu
    learngin uboot design parameter recovery mechanism
    learning uboot auto switch to stanbdy system in qca4531 cpu
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/6042321.html
Copyright © 2011-2022 走看看