Junit参数化测试,不一样的思路
Junit parameterized testing ,a different thought
在这里,
我有一个自定义的独立组件,它接受作为 jar 文件的输入并从中识别 junit
测试 类 并使用下面给出的代码执行它们。
Result result = JUnitCore.runClasses(cls);//cls is Class<?>instance.
接下来我需要的是注入参数的规定(假设输入 jar 包含基于 selenium 的测试,它需要不同的浏览器作为参数)
所以我需要从我的独立组件中将不同的浏览器作为参数注入到 junit。
这可能吗?
您可以使用对 JUnitCore 的不同调用并实现您自己的自定义计算机。如果您的测试 class 使用您自己的注释进行注释,则自定义计算机可以生成参数化运行器:
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomParameterized {}
像这样:
@CustomParameterized
public class Example {
private String arg;
public Example(String arg){
this.arg = arg;
}
@org.junit.Test
public void test(){
assertEquals("a",arg);
}
}
JUnitCore 调用将变为:
String[] args = new String[]{"a","b"}
Request request = Request.classes(new CustomComputer(args), Example.class);
Result result = new JUnitCore().run(request);
自定义计算机如下所示:
public class CustomComputer extends Computer {
private final List<Object> args;
public CustomComputer(String[] args) {
this.args = Arrays.<Object>asList(args);
}
@Override
protected Runner getRunner(RunnerBuilder builder, final Class<?> testClass) throws Throwable {
if (testClass.isAnnotationPresent(Test.CustomParameterized.class)) {
final BlockJUnit4ClassRunnerWithParametersFactory factory = new BlockJUnit4ClassRunnerWithParametersFactory();
return new CustomParameterizedRunner(testClass, factory);
}
return builder.runnerForClass(testClass);
}
private class CustomParameterizedRunner extends Suite {
private final Class<?> testClass;
private final BlockJUnit4ClassRunnerWithParametersFactory factory;
public CustomParameterizedRunner(Class<?> testClass, BlockJUnit4ClassRunnerWithParametersFactory factory) throws InitializationError {
super(testClass, Collections.EMPTY_LIST);
this.testClass = testClass;
this.factory = factory;
}
@Override
protected List<Runner> getChildren() {
List<Runner> runners = new ArrayList<>();
for (Object arg : args) {
runners.add(runnerFor(arg, testClass, factory));
}
return runners;
}
}
private static Runner runnerFor(Object arg, Class<?> testClass, BlockJUnit4ClassRunnerWithParametersFactory factory) {
ArrayList<Object> parameters = new ArrayList<>(1);
parameters.add(arg);
String name = testClass.getSimpleName() + "[" + arg + "]";
TestWithParameters test = new TestWithParameters(name, new TestClass(testClass), parameters);
try {
return factory.createRunnerForTestWithParameters(test);
} catch (InitializationError initializationError) {
throw new RuntimeException(initializationError);
}
}
}
在这里,
我有一个自定义的独立组件,它接受作为 jar 文件的输入并从中识别 junit
测试 类 并使用下面给出的代码执行它们。
Result result = JUnitCore.runClasses(cls);//cls is Class<?>instance.
接下来我需要的是注入参数的规定(假设输入 jar 包含基于 selenium 的测试,它需要不同的浏览器作为参数) 所以我需要从我的独立组件中将不同的浏览器作为参数注入到 junit。 这可能吗?
您可以使用对 JUnitCore 的不同调用并实现您自己的自定义计算机。如果您的测试 class 使用您自己的注释进行注释,则自定义计算机可以生成参数化运行器:
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomParameterized {}
像这样:
@CustomParameterized
public class Example {
private String arg;
public Example(String arg){
this.arg = arg;
}
@org.junit.Test
public void test(){
assertEquals("a",arg);
}
}
JUnitCore 调用将变为:
String[] args = new String[]{"a","b"}
Request request = Request.classes(new CustomComputer(args), Example.class);
Result result = new JUnitCore().run(request);
自定义计算机如下所示:
public class CustomComputer extends Computer {
private final List<Object> args;
public CustomComputer(String[] args) {
this.args = Arrays.<Object>asList(args);
}
@Override
protected Runner getRunner(RunnerBuilder builder, final Class<?> testClass) throws Throwable {
if (testClass.isAnnotationPresent(Test.CustomParameterized.class)) {
final BlockJUnit4ClassRunnerWithParametersFactory factory = new BlockJUnit4ClassRunnerWithParametersFactory();
return new CustomParameterizedRunner(testClass, factory);
}
return builder.runnerForClass(testClass);
}
private class CustomParameterizedRunner extends Suite {
private final Class<?> testClass;
private final BlockJUnit4ClassRunnerWithParametersFactory factory;
public CustomParameterizedRunner(Class<?> testClass, BlockJUnit4ClassRunnerWithParametersFactory factory) throws InitializationError {
super(testClass, Collections.EMPTY_LIST);
this.testClass = testClass;
this.factory = factory;
}
@Override
protected List<Runner> getChildren() {
List<Runner> runners = new ArrayList<>();
for (Object arg : args) {
runners.add(runnerFor(arg, testClass, factory));
}
return runners;
}
}
private static Runner runnerFor(Object arg, Class<?> testClass, BlockJUnit4ClassRunnerWithParametersFactory factory) {
ArrayList<Object> parameters = new ArrayList<>(1);
parameters.add(arg);
String name = testClass.getSimpleName() + "[" + arg + "]";
TestWithParameters test = new TestWithParameters(name, new TestClass(testClass), parameters);
try {
return factory.createRunnerForTestWithParameters(test);
} catch (InitializationError initializationError) {
throw new RuntimeException(initializationError);
}
}
}