在TestExecutionListener中获取ParameterizedTests的参数值 [英] Getting the values of parameters for ParameterizedTests in TestExecutionListener

查看:273
本文介绍了在TestExecutionListener中获取ParameterizedTests的参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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乘XML文件中的testcase数,因此报告是错误的. SummaryGeneratingListener包含大量测试信息,但我无法让TeamCity使用它.

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?

我已经尝试过MethodSource中的各种API.

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.

推荐答案

如果您尚未自定义显示名称,则@ParameterizedTest的显示名称应包含自变量.

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

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

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

这篇关于在TestExecutionListener中获取ParameterizedTests的参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆