- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当您使用 Eclipse TestRunner 运行 JUnit 4 ParameterizedTest 时,图形表示相当笨拙:对于每个测试,您都有一个名为 [0]
、[1]
等是否可以给测试 [0]
、[1]
等明确的名称?为测试实现 toString
方法似乎没有帮助。
(这是 JUnit test with dynamic number of tests 的后续问题。)
最佳答案
我认为 jUnit 4 中内置没有任何东西可以做到这一点。
我已经实现了一个解决方案。我在现有的基础上构建了自己的 Parameterized
类:
public class MyParameterized extends TestClassRunner {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public static @interface Parameters {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public static @interface Name {
}
public static Collection<Object[]> eachOne(Object... params) {
List<Object[]> results = new ArrayList<Object[]>();
for (Object param : params)
results.add(new Object[] { param });
return results;
}
// TODO: single-class this extension
private static class TestClassRunnerForParameters extends TestClassMethodsRunner {
private final Object[] fParameters;
private final Class<?> fTestClass;
private Object instance;
private final int fParameterSetNumber;
private final Constructor<?> fConstructor;
private TestClassRunnerForParameters(Class<?> klass, Object[] parameters, int i) throws Exception {
super(klass);
fTestClass = klass;
fParameters = parameters;
fParameterSetNumber = i;
fConstructor = getOnlyConstructor();
instance = fConstructor.newInstance(fParameters);
}
@Override
protected Object createTest() throws Exception {
return instance;
}
@Override
protected String getName() {
String name = null;
try {
Method m = getNameMethod();
if (m != null)
name = (String) m.invoke(instance);
} catch (Exception e) {
}
return String.format("[%s]", (name == null ? fParameterSetNumber : name));
}
@Override
protected String testName(final Method method) {
String name = null;
try {
Method m = getNameMethod();
if (m != null)
name = (String) m.invoke(instance);
} catch (Exception e) {
}
return String.format("%s[%s]", method.getName(), (name == null ? fParameterSetNumber : name));
}
private Constructor<?> getOnlyConstructor() {
Constructor<?>[] constructors = getTestClass().getConstructors();
assertEquals(1, constructors.length);
return constructors[0];
}
private Method getNameMethod() throws Exception {
for (Method each : fTestClass.getMethods()) {
if (Modifier.isPublic((each.getModifiers()))) {
Annotation[] annotations = each.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation.annotationType() == Name.class) {
if (each.getReturnType().equals(String.class))
return each;
else
throw new Exception("Name annotated method doesn't return an object of type String.");
}
}
}
}
return null;
}
}
// TODO: I think this now eagerly reads parameters, which was never the
// point.
public static class RunAllParameterMethods extends CompositeRunner {
private final Class<?> fKlass;
public RunAllParameterMethods(Class<?> klass) throws Exception {
super(klass.getName());
fKlass = klass;
int i = 0;
for (final Object each : getParametersList()) {
if (each instanceof Object[])
super.add(new TestClassRunnerForParameters(klass, (Object[]) each, i++));
else
throw new Exception(String.format("%s.%s() must return a Collection of arrays.", fKlass.getName(), getParametersMethod().getName()));
}
}
private Collection<?> getParametersList() throws IllegalAccessException, InvocationTargetException, Exception {
return (Collection<?>) getParametersMethod().invoke(null);
}
private Method getParametersMethod() throws Exception {
for (Method each : fKlass.getMethods()) {
if (Modifier.isStatic(each.getModifiers())) {
Annotation[] annotations = each.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation.annotationType() == Parameters.class)
return each;
}
}
}
throw new Exception("No public static parameters method on class " + getName());
}
}
public MyParameterized(final Class<?> klass) throws Exception {
super(klass, new RunAllParameterMethods(klass));
}
@Override
protected void validate(MethodValidator methodValidator) {
methodValidator.validateStaticMethods();
methodValidator.validateInstanceMethods();
}
}
像这样使用:
@RunWith(MyParameterized.class)
public class ParameterizedTest {
private File file;
public ParameterizedTest(File file) {
this.file = file;
}
@Test
public void test1() throws Exception {}
@Test
public void test2() throws Exception {}
@Name
public String getName() {
return "coolFile:" + file.getName();
}
@Parameters
public static Collection<Object[]> data() {
// load the files as you want
Object[] fileArg1 = new Object[] { new File("path1") };
Object[] fileArg2 = new Object[] { new File("path2") };
Collection<Object[]> data = new ArrayList<Object[]>();
data.add(fileArg1);
data.add(fileArg2);
return data;
}
}
这意味着我更早地实例化了测试类。我希望这不会导致任何错误......我想我应该测试测试:)
关于java - 在 Eclipse Testrunner 中具有名称的 ParameterizedTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/385925/
我有一个测试,我想在其中传递三个参数: 字符串 枚举 字符串数组 例子: @ParameterizedTest @CsvSource({ "/path/to/first
我无法从之前提出的问题中找到错误的原因,因为它们与“@Test”(不允许自定义数据类型)有关。 我有一个程序,它接受字符串类型输入(通常是文本 block ),并以列表的形式返回输入的句子。为了正确测
我正在尝试在 JUnit 5 中开发参数化测试,如下例所示。 @ParameterizedTest @ArgumentsSource(ArgClassProvider.class) void test
我们如何使用Selenium-Jupiter's @TestTemplate (让它在不同的浏览器上运行: https://bonigarcia.github.io/selenium-jupiter/
当您使用 Eclipse TestRunner 运行 JUnit 4 ParameterizedTest 时,图形表示相当笨拙:对于每个测试,您都有一个名为 [0]、[1] 等是否可以给测试 [0]、
我发现 Junit 5 从 5.3 版开始支持并行性,但我没有找到任何关于如何使用 csv 源运行并行测试的引用资料。你有什么建议吗? 最佳答案 或者,您可以创建具有相同内容的 src/test/re
这是 JUnit 中的参数化测试: @ParameterizedTest @ValueSource(strings = {"Username", "User123", "Another use
考虑这个片段: @ParameterizedTest @ValueSource(strings = {"a", "b", "c"}) void test(final String line) {
我试图将来自JUnit4的参数化运行器的概念与JUnit5参数化测试结合起来。本质上,我想在同一组数据上测试两个单独的函数。 我知道我可以将函数作为另一个参数添加到参数化测试本身,但是我试图使更改或添
我有一堆 @ParameterizedTest 从 @MethodSource 接收参数,toString() 结果非常冗长(例如 Selenium 的 网络驱动程序)。这些默认用于组成相应的显示名称
我是 JUnit 测试的新手,我想在 IntelliJ IDEA 2017.3.3 中创建参数化测试。所以我添加了 JUnit 5: 然后 IntelliJ 下载 org.junit.jupiter:
我有使用 TestNG 的单元测试,我尝试转移到 JUnit Jupiter (JUnit 5),我想知道哪种方法最好: 测试NG : @DataProvider public Object[][]
我正在尝试借助带有 JUnit 5 的 FizzBuzz 应用程序来理解 JUnit 5。下面是测试用例之一 @DisplayName("Test for multiples of 5")
我有一个如下所示的 JUnit 4 测试,我正在尝试将 JUnit 升级到 JUnit 5。我做了一些关于如何将 JUnit 4 测试迁移到 JUnit 5 的研究,但找不到任何关于如何迁移以下案例的
目前,我正在(尝试)将现有的 Junit4 项目迁移到 Junit5。 我被困在必须同时使用@RepeatedTest 和@ParameterizedTest 的地方。尝试这样做会引发默认异常 - N
我在尝试使用 JUnit 5 的参数化功能时偶然发现了一个初始化错误。 @ExtendWith(MockitoExtension.class) @RunWith(Parameterized.class
我正在创建一个排序算法库。每个类都有相同的方法: public static > void sort(T[] array) 我会让所有类都实现一个接口(interface)或扩展一个抽象类,但 sor
我是一名优秀的程序员,十分优秀!