JUnit使用入门

来源:互联网 发布:大使馆 知乎 编辑:程序博客网 时间:2024/05/10 18:19

JUnit使用入门

JUnit是Java单元测试框架,已经在Eclipse中默认安装。目前主流的有JUnit3和JUnit4。JUnit3中,测试用例需要继承TestCase类。JUnit4中,测试用例无需继承TestCase类,只需要使用@Test等注解。

Junit3

先看一个Junit3的样例


    // 测试java.lang.Math  
    // 必须继承TestCase  
    public class Junit3TestCase extends TestCase {  
        public Junit3TestCase() {  
            super();  
        }  
        
            // 传入测试用例名称  
        public Junit3TestCase(String name) {  
            super(name);  
        }  
       
            // 在每个Test运行之前运行  
        @Override  
        protected void setUp() throws Exception {  
            System.out.println("Set up");  
        }  
            // 测试方法。  
            // 方法名称必须以test开头,没有参数,无返回值,是公开的,可以抛出异常  
            // 也即类似public void testXXX() throws Exception {}  
        public void testMathPow() {  
            System.out.println("Test Math.pow");  
            Assert.assertEquals(4.0, Math.pow(2.0, 2.0));  
        }  
       
        public void testMathMin() {  
            System.out.println("Test Math.min");  
            Assert.assertEquals(2.0, Math.min(2.0, 4.0));  
        }  
       
            // 在每个Test运行之后运行  
        @Override  
        protected void tearDown() throws Exception {  
            System.out.println("Tear down");  
        }  
    }  


 

 

如果采用默认的TestSuite,则测试方法必须是public void testXXX() [throws Exception] {}的形式,并且不能存在依赖关系,因为测试方法的调用顺序是不可预知的。
上例执行后,控制台会输出

 
    Set up  
    Test Math.pow  
    Tear down  
    Set up  
    Test Math.min  
    Tear down  

从中,可以猜测到,对于每个测试方法,调用的形式是:

    testCase.setUp();  
    testCase.testXXX();  
    testCase.tearDown();    


运行测试方法

在Eclipse中,可以直接在类名或测试方法上右击,在弹出的右击菜单中选择Run As -> JUnit Test。
在Mvn中,可以直接通过mvn test命令运行测试用例。
也可以通过Java方式调用,创建一个TestCase实例,然后重载runTest()方法,在其方法内调用测试方法(可以多个)。

    TestCase test = new Junit3TestCase("mathPow") {  
            // 重载  
        protected void runTest() throws Throwable {  
            testMathPow();  
        };  
    };  
    test.run(); 

 

更加便捷地,可以在创建TestCase实例时直接传入测试方法名称,JUnit会自动调用此测试方法,如:

    TestCase test = new Junit3TestCase("testMathPow");  
    test.run();  


 

Junit TestSuite

TestSuite是测试用例套件,能够运行过个测试方法。如果不指定TestSuite,会创建一个默认的TestSuite。默认TestSuite会扫描当前内中的所有测试方法,然后运行。
如果不想采用默认的TestSuite,则可以自定义TestSuite。在TestCase中,可以通过静态方法suite()返回自定义的suite。

 

    import junit.framework.Assert;  
    import junit.framework.Test;  
    import junit.framework.TestCase;  
    import junit.framework.TestSuite;  
       
    public class Junit3TestCase extends TestCase {  
            //...  
        public static Test suite() {  
            System.out.println("create suite");  
            TestSuite suite = new TestSuite();  
            suite.addTest(new Junit3TestCase("testMathPow"));  
            return suite;  
        }  
    }  
 

允许上述方法,控制台输出

create suite
Set up
Test Math.pow
Tear down

 

 

并且只运行了testMathPow测试方法,而没有运行testMathMin测试方法。通过显式指定测试方法,可以控制测试执行的顺序。

也可以通过Java的方式创建TestSuite,然后调用TestCase,如

 

     // 先创建TestSuite,再添加测试方法  
    TestSuite testSuite = new TestSuite();  
    testSuite.addTest(new Junit3TestCase("testMathPow"));  
       
    // 或者 传入Class,TestSuite会扫描其中的测试方法。  
    TestSuite testSuite = new TestSuite(Junit3TestCase.class,Junit3TestCase2.class,Junit3TestCase3.class);  
       
    // 运行testSuite  
    TestResult testResult = new TestResult();  
    testSuite.run(testResult); 

 

testResult中保存了很多测试数据,包括运行测试方法数目(runCount)等。

0 0