在TestExecutionListener

时间:2019-05-27 04:55:36

标签: junit5

在我的测试框架中,我必须手动生成TestSuite.xml。在Junit4中,侦听器传递的“ Description”对象包含在参数化测试中传递的参数值,因此XML如下所示。

<testcase
  11     classname="com.workday.scramble.datagen.generator.regex.generator.RegexGenTest"
  12     name="testTransformAndGenerate[77: RegexGenTest: [0-9]{1,28}]"
  13     time="9.1E-4"/>
  14   <testcase
  15     classname="com.workday.scramble.datagen.generator.regex.generator.RegexGenTest"
  16     name="testTransformAndGenerate[123: RegexGenTest: [A-Za-z0-9 -]{1,64}]"
  17     time="0.0020299999999999997"/>
  18   <testcase
  19     classname="com.workday.scramble.datagen.generator.regex.generator.RegexGenTest"
  20     name="testTransformAndGenerate[112: RegexGenTest: [A-Za-z0-9 -]{1,31}]"
  21     time="0.00171"/>
  22   <testcase
  23     classname="com.workday.scramble.datagen.generator.regex.generator.RegexGenTest"
  24     name="testTransformAndGenerate[318: RegexGenTest: \d{37}]"
  25     time="7.3E-4"/>

在JUnit5中,我使用TestExecutionListener回调并获得如下的方法名称:

    @Override
    public void executionFinished(final TestIdentifier testIdentifier, final TestExecutionResult testExecutionResult) {
        super.executionFinished(testIdentifier, testExecutionResult);
        logWithThrowable("Execution Finished: %s - %s - %s", testExecutionResult.getThrowable().orElse(null),
                testIdentifier.getDisplayName(), testIdentifier.getUniqueId(), testExecutionResult);
        final Optional<TestSource> source = testIdentifier.getSource();
        if (source.isPresent()) {
            final TestSource testSource = source.get();
            if (testSource instanceof ClassSource) {
               ...
            }
            else if (testSource instanceof MethodSource) {
                final MethodSource methodSource = (MethodSource) testSource;
                LOG.info("MethodSource: executionFinished for class: " + methodSource.getClassName() + " and method: "
                        + methodSource.getMethodName());
                final OmsTestMethod testMethod = getOmsTestMethod(methodSource);
                if (testMethod == null) {
                    return;
                }
                testMethod.setResult(testExecutionResult);
            }
        }
    }

此方法名称不包含传入的参数,因此XML如下所示:

<testcase
 12     classname="com.workday.scramble.datagen.generator.regex.generator.RegexGenTest"
 13     name="testTransformAndGenerate"
 14     time="0.00366"/>

我将方法名称视为唯一的,因此只有一个条目。

TeamCity的XML文件中的数量为testcase,因此报告是错误的。 SummaryGeneratingListener包含大量测试信息,但我无法让TeamCity使用。

关于如何执行此操作的任何想法?

我已经尝试了MethodSource中的各种API。

    @Override
    public void executionFinished(final TestIdentifier testIdentifier, final TestExecutionResult testExecutionResult) {
        super.executionFinished(testIdentifier, testExecutionResult);
        logWithThrowable("Execution Finished: %s - %s - %s", testExecutionResult.getThrowable().orElse(null),
                testIdentifier.getDisplayName(), testIdentifier.getUniqueId(), testExecutionResult);
        final Optional<TestSource> source = testIdentifier.getSource();
        if (source.isPresent()) {
            final TestSource testSource = source.get();
            if (testSource instanceof ClassSource) {
               ...
            }
            else if (testSource instanceof MethodSource) {
                final MethodSource methodSource = (MethodSource) testSource;
                LOG.info("MethodSource: executionFinished for class: " + methodSource.getClassName() + " and method: "
                        + methodSource.getMethodName());
                final OmsTestMethod testMethod = getOmsTestMethod(methodSource);
                if (testMethod == null) {
                    return;
                }
                testMethod.setResult(testExecutionResult);
            }
        }
    }

我希望看到方法名称也包含参数化的值,因此我可以区分各种测试。

1 个答案:

答案 0 :(得分:1)

如果您尚未自定义显示名称,则@ParameterizedTest的显示名称应包含参数。

如果这对您来说还不够,请随时在这里提出问题:https://github.com/junit-team/junit5/issues/new/choose