junit4的简单使用

测试套件的使用

测试类1

package com.westward;

import static org.junit.Assert.*;

import org.junit.Test;

public class TaskTest1 {

    @Test
    public void test() {
        System.out.println("tasktest1");
    }

}

测试类2

package com.westward;

import static org.junit.Assert.*;

import org.junit.Test;

public class TaskTest2 {

    @Test
    public void test() {
        System.out.println("tasktest2");
    }

}

测试类3

package com.westward;

import static org.junit.Assert.*;

import org.junit.Test;

public class TaskTest3 {

    @Test
    public void test() {
        System.out.println("tasktest3");
    }

}

测试套件:
注意:测试套件类里,不能有任何代码

package com.westward;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({TaskTest1.class,TaskTest2.class,TaskTest3.class})
public class SuiteTest {
    
}

 

参数化测试

测试类

package com.westward;

public class Calculator {
    public int add(int a,int b){
        return a+ b;
    }
    public int subtract(int a,int b){
        return a- b;
    }
    public int multiply(int a,int b){
        return a* b;
    }
    public int divide(int a,int b){
        return a/ b;
    }
}

参数化测试类;

package com.westward;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ParameterTest {
    int expected= 0;
    int input1= 0;
    int input2= 0;
    /**
     * 必须是static方法
     * */
    @Parameters
    public static Collection<Object[]> t(){
        return Arrays.asList(new Object[][]{
            {3,1,2},{6,2,4},{7,3,4}
        });
    }
    public ParameterTest(int expected,int input1,int input2) {
        this.expected= expected;
        this.input1= input1;
        this.input2= input2;
    }
    @Test
    public void testAdd(){
        assertEquals(expected, new Calculator().add(input1, input2));
    }
}

 

posted on 2016-04-28 00:04  WesTward  阅读(234)  评论(0编辑  收藏  举报