gpt4 book ai didi

java - Junit参数化测试,不一样的想法

转载 作者:行者123 更新时间:2023-11-30 08:06:45 25 4
gpt4 key购买 nike

在这里,我有一个自定义的独立组件,它接受 jar 文件形式的输入,并从中识别 junit 测试类,并使用下面给出的代码执行它们。

Result result = JUnitCore.runClasses(cls);//cls is Class<?>instance.

接下来我需要的是注入(inject)参数的规定(假设输入 jar 包含基于 selenium 的测试,它需要不同的浏览器作为参数)所以我需要从我的独立组件将不同的浏览器作为参数注入(inject)到 junit 中。这可能吗?

最佳答案

您可以使用对 JUnitCore 的不同调用并实现您自己的自定义计算机。如果您的测试类使用您自己的注释进行注释,则自定义计算机可以生成参数化运行程序:

@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);
}
}
}

关于java - Junit参数化测试,不一样的想法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30994146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com