Junit 使用html
一、忽略测试方法。在使用@Test的方法上使用@Ignore,将不会对此方法进行测试java
二、测试套件数组
解决的问题:函数
一、对测试类进行统一测试,而没必要在单独测试类上一个一个进行测试。测试
使用JUnit的@RunWith以及@SuiteClassses注解,@SuiteClassses后面为待测试类的数组ui
示例:this
@RunWith(Suite.class) @Suite.SuiteClasses({UserTest.class}) --指定要测试的类 public class TestAll { }
三、参数化测试spa
解决问题:对同一个方法使用不一样的参数进行测试。.net
示例:code
package com.vrvwh.wh01.testSuit; import com.vrvwh.wh01.controller.Calculator; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.util.Arrays; import java.util.Collection; /** * Created by Administrator on 2015/1/22. */ @RunWith(Parameterized.class) public class ParameterTest { private long expected; private long input1; private long input2; public ParameterTest(int expected, int x, int y){ this.expected = expected; this.input1 = x; this.input2 = y; } @Parameterized.Parameters public static Collection getData(){ Object[][] object = {{3,1,2}, {0,0,0}, {-4,-1,-3}, {6,-3,9}}; return Arrays.asList(object); } @Test public void testAdd(){ Calculator calculator=new Calculator(); long result=calculator.add(input1,input2); Assert.assertTrue(expected == result); } }
注意:getData中object 数组数据顺序必须与构造函数顺序匹配
参考:http://www.ibm.com/developerworks/cn/java/j-lo-junit4/index.html