zoukankan      html  css  js  c++  java
  • JUnit学习之hamcrest、testSuite介绍及测试原则

    [转自] http://huihai.iteye.com/blog/1994270


    上一节说了junit的一些基本概念,主要使用assert做一些基本的判断。但很多时候使用assert做判断,并不方便,如果要判断某几个值是否为true或false,这时使用hamcrest来判断就会方便许多。hamcrest就是专门为增强junit来提供的框架。它可以有效的使用一些语义比较清楚的名字来做判断,一些常用的方法如下:

    1、下面使用hamcrest在上一节的例子上继续操作。使用junit中的assertThat进行断言,第一个参数为实际值,第二个参数为hamcrest的表达式。

    在上一节的测试类TestCalcuate中,先静态导入hamcrest包中的Matchers类,如下图:

    2、添加下图所示的测试方法,在测试方法testHamcrest方法中,把junit与hamcrest结合使用,方法assertThat方法是junit的方法,greaterThan方法是hamcrest的方法,greaterThan判断第一个参数是否>第二个参数,这里就是判断50是否大于20。

     

    3、如果要判断第一个参数是否大于20,并且小于60,这时可以使用正面的方式:

     
     

    4、这时使用junit进行测试,如果测试通过,代表50确实大于49,小于60,如果出现如下错误:

     

    5、其原因是因为资源路径里junit的jar包在hamcrest的jar包上面,java运行环境先从junit包中查找allOf方法,但是这里使用的是hamcrest的allOf方法。从资源路径里把两个包的路径换一下就可以了。如下图,选择hamcrest的jar包点击up,就可以反hamcrest的jar包放在junit的jar包的上面,java运行环境就会优先从hamcrest包中查allOf方法。

     

    6、使用hamcrest还可以判断一个字符串是否以某个字符串开始或结尾。

     
     

    7、在一个项目中,可能有很多的测试类,如果每个测试类都要点击运行,那么成百上千个类都需要测试,这会是个比较繁重的工作,这时可以使用可以使用junit的jar包中提供的Suite来解决这个问题,上面的例子中只有一个测试类名叫TestCalcuate,现在新建两个测试类,分别为TestA、TestB,如下所示:

        package cn.whp.util;  
          
        import org.junit.Test;  
          
        public class TestA {  
            @Test  
            public void testA(){  
                System.out.println("TestA");  
            }  
          
        }  
        package cn.whp.util;  
          
        import org.junit.Test;  
          
        public class TestB {  
            @Test  
            public void testB(){  
                System.out.println("TestB");  
            }  
          
        }  
     

     8、然后新建一个测试类TestSuite,这个类可以把以上三个类,TestCalcuate、TestA、TestB,这三个测试方法同事进行测试。如下所示,

    @RunWith(Suite.class)//代表以Suite来运行这个测试类
    @SuiteClasses({TestA.class,TestB.class,TestCalcuate.class})//代表要测试的类有哪些。 

        package cn.whp.util;  
          
        import org.junit.runner.RunWith;  
        import org.junit.runners.Suite;  
        import org.junit.runners.Suite.SuiteClasses;  
          
        @RunWith(Suite.class)  
        @SuiteClasses({TestA.class,TestB.class,TestCalcuate.class})  
        public class TestSuite {  
            public void testSuite(){  
                System.out.println("TestA");  
            }  
          
        }  
     

     如上操作完成后点击运行,就可以看到所有的测试方法全通过的信息,如下所示: 

     

    测试原则:

    1、建议创建一个专门的source folder--->test来编写测试类代码。上面例子就新建一个test的资源包。

    2、测试类的包应该保持和需要测试的类一致。上面的例子中Calcuate类在src/cn/whp/util包中,相应的测试类就在test/cn/whp/util中。

    3、测试单元中的每个测试方法都必须可以独立执行,不相互依赖,没有顺序。


  • 相关阅读:
    eclipse下c/cpp " undefined reference to " or "launch failed binary not found"问题
    blockdev 设置文件预读大小
    宝宝语录
    CentOS修改主机名(hostname)
    subprocess报No such file or directory
    用ldap方式访问AD域的的错误解释
    英特尔的VTd技术是什么?
    This virtual machine requires the VMware keyboard support driver which is not installed
    Linux内核的文件预读详细详解
    UNP总结 Chapter 26~29 线程、IP选项、原始套接字、数据链路访问
  • 原文地址:https://www.cnblogs.com/pekkle/p/6568754.html
Copyright © 2011-2022 走看看