套件运行器
它可以运行多个测试。
package test;
public class Calculator {
public double add(double number1, double number2) {
return number1 + number2;
}
}
package test;
import org.junit.Assert;
import org.junit.Test;
public class CalculatorTest {
@Test
public void a() throws Exception {
Calculator c = new Calculator();
double result = c.add(0, 1);
Assert.assertEquals("结果不正确", result,1, 0);
}
@Test
public void a1() throws Exception {
Calculator c = new Calculator();
double result = c.add(1, 2);
Assert.assertEquals("结果不正确", result,3, 0);
}
}
package test;
import org.junit.Assert;
import org.junit.Test;
public class B {
@Test
public void b() throws Exception {
Calculator c = new Calculator();
double result = c.add(1, 1);
Assert.assertEquals("结果不正确", result, 2, 0);
}
}
package test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(value = Suite.class)
@SuiteClasses(value = { CalculatorTest.class, B.class })
public class A1 {
}
这里有两个测试类,以及一个设置为了套件运行器的测试类。
运行A1,会讲两个测试都运行了。
另外测试套件可以去调用测试套件。
增加我们的测试
package test;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(value = Parameterized.class)
public class ParameterTest {
private double expected;
private double valueOne;
private double valueTwo;
public ParameterTest(double expected, double valueOne, double valueTwo) {
this.expected = expected;
this.valueOne = valueOne;
this.valueTwo = valueTwo;
}
@Parameters
public static Collection<Integer[]> getTestParameters() {
return Arrays.asList(new Integer[][] { { 2, 1, 1 }, // expected,
// valueOne,
// valueTwo
{ 3, 2, 1 }, // expected, valueOne, valueTwo
{ 4, 3, 1 }, // expected, valueOne, valueTwo
});
}
@Test
public void sum() {
Calculator calc = new Calculator();
Assert.assertEquals(expected, calc.add(valueOne, valueTwo), 0);
}
}
package test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(value = Suite.class)
@SuiteClasses(value = { ParameterTest.class })
public class A2 {
}
package test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(value = Suite.class)
@SuiteClasses(value = { A1.class, A2.class })
public class AllTest {
}
在AllTest 设置的SuiteClasses里面的类本身就是测试套件。
它可以运行多个测试。
package test;
public class Calculator {
public double add(double number1, double number2) {
return number1 + number2;
}
}
package test;
import org.junit.Assert;
import org.junit.Test;
public class CalculatorTest {
@Test
public void a() throws Exception {
Calculator c = new Calculator();
double result = c.add(0, 1);
Assert.assertEquals("结果不正确", result,1, 0);
}
@Test
public void a1() throws Exception {
Calculator c = new Calculator();
double result = c.add(1, 2);
Assert.assertEquals("结果不正确", result,3, 0);
}
}
package test;
import org.junit.Assert;
import org.junit.Test;
public class B {
@Test
public void b() throws Exception {
Calculator c = new Calculator();
double result = c.add(1, 1);
Assert.assertEquals("结果不正确", result, 2, 0);
}
}
package test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(value = Suite.class)
@SuiteClasses(value = { CalculatorTest.class, B.class })
public class A1 {
}
这里有两个测试类,以及一个设置为了套件运行器的测试类。
运行A1,会讲两个测试都运行了。
另外测试套件可以去调用测试套件。
增加我们的测试
package test;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(value = Parameterized.class)
public class ParameterTest {
private double expected;
private double valueOne;
private double valueTwo;
public ParameterTest(double expected, double valueOne, double valueTwo) {
this.expected = expected;
this.valueOne = valueOne;
this.valueTwo = valueTwo;
}
@Parameters
public static Collection<Integer[]> getTestParameters() {
return Arrays.asList(new Integer[][] { { 2, 1, 1 }, // expected,
// valueOne,
// valueTwo
{ 3, 2, 1 }, // expected, valueOne, valueTwo
{ 4, 3, 1 }, // expected, valueOne, valueTwo
});
}
@Test
public void sum() {
Calculator calc = new Calculator();
Assert.assertEquals(expected, calc.add(valueOne, valueTwo), 0);
}
}
package test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(value = Suite.class)
@SuiteClasses(value = { ParameterTest.class })
public class A2 {
}
package test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(value = Suite.class)
@SuiteClasses(value = { A1.class, A2.class })
public class AllTest {
}
在AllTest 设置的SuiteClasses里面的类本身就是测试套件。