0

我尝试编写自己的junit runner,目前我被困在返回正确的测试描述上。

public class ParameterizedWrapper extends Suite {
    private List<Runner> fRunners;

    /**
     * @throws Throwable 
     * 
     */
    public ParameterizedWrapper(final Class<?> clazz) throws Throwable {
        super(clazz, Collections.<Runner>emptyList());
        fRunners = constructRunners(getParametersMethod());
    }

    protected List<Runner> constructRunners(final FrameworkMethod method) throws Exception, Throwable {
        @SuppressWarnings("unchecked")
        Iterable<Object[]> parameters = (Iterable<Object[]>) getParametersMethod().invokeExplosively(null);
        ArrayList<Runner> runners = new ArrayList<Runner>();
        int index = 0;
        for (Object[] parameter : parameters) {
            Class<?> testClass = getTestClass().getJavaClass();
            WrappedRunner wrappedRunner = testClass.getAnnotation(WrappedRunner.class);
            Runner runner = wrappedRunner.value().getConstructor(Class.class).newInstance(getTestClass().getJavaClass());
            runners.add(new WrappingRunner(runner, parameter, testClass, index++));
        }
        return runners;
    }

    private FrameworkMethod getParametersMethod() throws Exception {
        List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(Parameters.class);
        for (FrameworkMethod each : methods) {
            if (each.isStatic() && each.isPublic()) {
                return each;
            }
        }

        throw new Exception("No public static parameters method on class " + getTestClass().getName());
    }

    @Override
    protected List<Runner> getChildren() {
        return fRunners;
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE})
    public static @interface WrappedRunner {
        Class<? extends Runner> value();
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public static @interface ParameterSetter {
    }

}

class WrappingRunner extends Runner {
    private Runner wrappedRunner;

    private Object[] parameters;

    private Class<?> testClass;

    private int testPosition;

    public WrappingRunner(final Runner runner, final Object[] params, final Class<?> clazz, final int position) {
        wrappedRunner = runner;
        parameters = params;
        testClass = clazz;
        testPosition = position;
    }

    @Override
    public Description getDescription() {
        Description originalDescription = wrappedRunner.getDescription();
        Description newDescription = Description.createSuiteDescription(nameFor(""), new Annotation[0]);
        for (Description child : originalDescription.getChildren()) {
            newDescription.addChild(decorateChildDescription(child));
        }

        return newDescription;
    }

    private String nameFor(String name) {
        return String.format("%1$s[%2$s]", name, testPosition);
    }

    protected Description decorateChildDescription(final Description originalChildDescription) {
        Description d = Description.createTestDescription(originalChildDescription.getTestClass(),
                nameFor(originalChildDescription.getMethodName()),
                originalChildDescription.getAnnotations().toArray(new Annotation[0]));
        return d;
    }

    @Override
    public void run(final RunNotifier notifier) {
        try {
            ParameterStorage.storeParameters(testClass, parameters);
            wrappedRunner.run(notifier);
        } finally {
            ParameterStorage.clearParameters(testClass);
        }
    }
}

我有一些测试类来检查跑步者是否有效。Runner 工作正常,只是测试的名称很奇怪。在 Eclipse 中,它显示所有测试并添加无根测试类别

在此处输入图像描述

并且surefire根本不使用我的命名:

在此处输入图像描述

我比较了我的跑步者和Parameterized跑步者中生成的描述对象,似乎没有区别。

4

2 回答 2

1

这有点难看,但将子跑步者列表传递给父构造函数更安全:

public ParameterizedWrapper(final Class<?> clazz) throws Throwable {
  super(clazz, constructRunners(getParametersMethod());
}

private static List<Runner> constructRunners(final FrameworkMethod method)
    throws Throwable {
  ...

你不应该需要覆盖Suite.getChildren()

于 2013-02-13T22:42:00.310 回答
0

我检查了更多,发现我的跑步者生成的描述没问题。但这并不重要,因为它与实际测试执行期间使用的描述不一致。这就是为什么 eclipse 将条目显示为未执行,这就是为什么 surefire 不显示我的名字的原因。

目前我认为使用我自己的通知程序来捕获测试起点并在该点替换配置。

如果有人有更好的解决方案,我想知道它:)。

于 2013-02-13T00:22:44.247 回答