1

I am working on porting a JUnit4 based test framework that uses JUnitCore to launch unit tests with JUnit5 launcher, junit-vintage-engine and a LauncherDiscoveryRequest with ClassSelector.

It is working for all cases except @ParameterizedTest. Launcher is not honoring parameterized tests.

How do I get Launcher to honor JUnit4 parameterized tests?

I have tried adding testCompile("org.junit.jupiter:junit-jupiter-params:${junit_jupiter_version}")

package ...;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import ...TestCategory;
import ...TestType;
import ...DataScramblingRegexTest;

@TestType(...TestCategory.UNIT)
@RunWith(Parameterized.class)
public class RegexGenTest {

    private static final int LOOKAROUND_ATTEMPTS = 100;

    @Parameterized.Parameter(0)
    public String regex;

    @Parameterized.Parameters(name = "{index}: RegexGenTest: {0}")
    public static Iterable<String[]> data() {
        return DataScramblingRegexTest.data();
    }

    @Test
    public void testTransformAndGenerate() {
        //even though Data Scramble Random Text doesn't transform every regex,
        //even if we do, and run it through Generex, it should still work.
        final String transformedRegex = RegexGen.transform(this.regex);
        String randomStr = RegexGen.generate(transformedRegex);
        if (RegexGen.containsLookArounds(this.regex)) {
            for (int i = 0; i < LOOKAROUND_ATTEMPTS; i++) {
                randomStr = RegexGen.generate(transformedRegex);
                if (randomStr.matches(this.regex)) {
                    return;
                }
            }
            Assert.fail("Lookaround failed");
        }
        Assert.assertTrue("generated string does not match regex: " + randomStr, randomStr.matches(this.regex));
    }
}

build.gradle

    compile("junit:junit:${junit_version}")
    testCompile("org.junit.vintage:junit-vintage-engine:${junit_jupiter_version}")
    testCompile("org.junit.jupiter:junit-jupiter-api:${junit_jupiter_version}")
    testCompile("org.junit.jupiter:junit-jupiter-params:${junit_jupiter_version}")
    testCompile("org.junit.jupiter:junit-jupiter-engine:${junit_jupiter_version}")

where

junit_version=4.12
junit_jupiter_version=5.4.2

Note the proprietary TestCategory.UNIT annotation, we use this in our test discovery code and create a Launcher and a LauncherdiscoveryRequest with ClassSelector.

I expect JUnit 4 parameters to be supported by JUnit5 launcher with junit-jupiter-vintage engine and it is not supported.

Anand S
  • 87
  • 1
  • 10
  • Please complete your code example with the full test class including import statements and test methods. Your build file might also contain valuable information due to versions of included libraries. – johanneslink May 26 '19 at 12:20
  • @johanneslink just did. I removed just a bit of code that had company identification, but you can see the general idea. There is an annotation that is read by the test framework to create a Launcher. – Anand S May 26 '19 at 14:51
  • Update: I converted the test to JUnit5 and the test framework still did not execute it with parameters. I then wrote a simple unit with Launcher running JUnit4 and JUnit5 parameterized tests and it worked fine. There is something in my test framework that is disabling parameters. Any ideas of where I should start looking? – Anand S May 26 '19 at 16:26
  • The direct dependency on junit4 is probably unnecessary (comes transitively through vintage) and maybe harmful. Just a guess without possibility to try it. – johanneslink May 26 '19 at 16:35
  • I will try that. Thanks. I think I have found out what is going on. Programmatically, I listen for events by using a TestExecutionListener and create TestSuite XML files. Looks like for parameterized tests, there is only one event fired per test (and not one per parameter). I need to look at the summary generating listener for that information. Testing it. – Anand S May 26 '19 at 19:35
  • Here is another curious side effect of parameterized tests.https://stackoverflow.com/questions/56319857/getting-the-values-of-parameters-for-parameterizedtests-in-testexecutionlistener – Anand S May 27 '19 at 04:56

0 Answers0