zoukankan      html  css  js  c++  java
  • junit4使用说明

    junit比起和main函数写的测试来说,有一下优点:

    1.测试和功能分开,比较明了。

    2.自动化,junit可以一次运行test包下面的所有测试,如果加上ant会更好。

    3.测试的配置比较多,exception和timeout等标签。

    junit的命名建议:

    放在test包中,类以classnameTest命名,方法以TestMethod命名。

    使用junit4,引入hamcrest包:一个core包,一个library包。

    如果有错的话,应该将junit4的包删除掉,使用从网上下载的junit包,使用all包。

    1. 使用hamcrest的匹配方法

      a) 更自然

    1. 示例

    a)assertThat( n, allOf( greaterThan(1), lessThan(15) ) );
    assertThat( n, anyOf( greaterThan(16), lessThan(8) ) );
    assertThat( n, anything() );
    assertThat( str, is( "bjsxt" ) );
    assertThat( str, not( "bjxxt" ) );

    b)        assertThat( str, containsString( "bjsxt" ) );
    assertThat( str, endsWith("bjsxt" ) );
    assertThat( str, startsWith( "bjsxt" ) );
    assertThat( n, equalTo( nExpected ) );
    assertThat( str, equalToIgnoringCase( "bjsxt" ) );
    assertThat( str, equalToIgnoringWhiteSpace( "bjsxt" ) );

    c)         assertThat( d, closeTo( 3.0, 0.3 ) );
    assertThat( d, greaterThan(3.0) );
    assertThat( d, lessThan (10.0) );
    assertThat( d, greaterThanOrEqualTo (5.0) );
    assertThat( d, lessThanOrEqualTo (16.0) );

    d)        assertThat( map, hasEntry( "bjsxt", "bjsxt" ) );
    assertThat( iterable, hasItem ( "bjsxt" ) );
    assertThat( map, hasKey ( "bjsxt" ) );
    assertThat( map, hasValue ( "bjsxt" ) );

    Failure和Error

    1. Failure是指测试失败
    2. Error是指测试程序本身出错

    运行多个测试

  • 相关阅读:
    VS2013 自动添加头部注释 -C#开发
    在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作
    React
    WebApi基础
    wcf
    memcached系列
    Ioc容器Autofac系列
    使用TortoiseSVN创建版本库
    使用libcurl 发送post请求
    值得推荐的C/C++框架和库
  • 原文地址:https://www.cnblogs.com/lxzh/p/2591806.html
Copyright © 2011-2022 走看看