运行参数化
package demo.junit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.*;
/**
* nutzdemo
* Created by wangxun on 2018/1/30下午2:09.
*/
@RunWith(value = Parameterized.class)
public class ParamerTest {
private Integer rs ;
private Integer a ;
private Integer b ;
@Parameters(name = "{index} : test ({0})={1}+{2}")
public static Collection<Integer[]> getParamerTest() {
return Arrays.asList(new Integer[][]{
{3,1,2},
{67,56,12},
{77,76,1}
}
);
}
public ParamerTest(Integer rs,Integer a,Integer b ) {
this.rs = rs;
this.a = a;
this.b = b;
}
@Test
public void sum(){
Calculator calculator = new Calculator();
assertEquals(rs,calculator.add(a,b),0);
}
}
-@Parameterized.Parameters(name = "{index} : test ({0})={1}+{2}") name为每次的用例的名字
3.png
Junit 生命周期
- 代码
package demo.junit;
import org.junit.*;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 命名规则 classNameTest
* Created by wangxun on 2018/1/30下午3:54.
*/
public class AppTest {
static Integer count = 0;
// 测试的List
private ArrayList testList = new ArrayList();
/**
* 每次运行@Test 都会创建一个类的实例
*/
public AppTest() {
System.out.println(String.format("AppTest CALL %d ",++count));
}
/**
* 在所有的@Test执行之前执行一次
*/
@BeforeClass
public static void StaticBeFore(){
System.out.println("@BeforeClass: StaticBeFore");
}
/**
* 在所有的@Test执行之后执行一次
*/
@AfterClass
public static void StaticAfter(){
System.out.println("@AfterClass: StaticAfter");
}
/**
* 在每次执行Test前都会执行
*/
@Before
public void TestBefore(){
System.out.println("@Before:TestBefore");
}
/**
* 在每次执行Test后都会执行
*/
@After
public void TestAfter(){
System.out.println("@After:TestAfter");
}
/**
* 测试用例,命令规则methodTest
*/
@Test
public void M1Test(){
Assert.assertEquals(0,testList.size());
}
/**
* 测试用例,命令规则methodTest
*/
@Test
public void M2Test(){
testList.add(1);
Assert.assertEquals(1,testList.size());
}
/**
* 测试用例,命令规则methodTest
* 忽略这个测试用例
*/
@Ignore
@Test
public void M3Test(){
testList.add(1);
Assert.assertEquals(2,testList.size());
}
}
- 运行结果
@BeforeClass: StaticBeFore
AppTest CALL 1
@Before:TestBefore
@After:TestAfter
AppTest CALL 2
@Before:TestBefore
@After:TestAfter
Test ignored.
@AfterClass: StaticAfter
网友评论