Getting the values of parameters for ParameterizedTests in TestExecutionListener

无人久伴 提交于 2019-12-11 14:23:14

问题


In my test framework, I have to manually generate the TestSuite.xml. In Junit4, the "Description" object passed by the listener contains the value of the parameters passed in a parameterized test, so the XML looks like this.

<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"/>

In JUnit5, I use the TestExecutionListener callback and I get the method name like this:

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

This method name does not contain the parameters passed in, so the XML looks like this:

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

I treat the method names as unique, so there is only one entry.

TeamCity goes by the number of testcases in the XML file, so the reporting is wrong. SummaryGeneratingListener has the number of tests information, but I can't get TeamCity to use that.

Any idea of how to do this?

I have tried the various APIs in MethodSource.

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

I expect to see the method name contain the parameterized values as well so I can differentiate between various tests.


回答1:


If you have not customized the display name, the display name for a @ParameterizedTest should contain the arguments.

If that is not sufficient for you, feel free to raise an issue here: https://github.com/junit-team/junit5/issues/new/choose



来源:https://stackoverflow.com/questions/56319857/getting-the-values-of-parameters-for-parameterizedtests-in-testexecutionlistener

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!