gradle / gradle

Adaptable, fast automation for all
https://gradle.org
Apache License 2.0
16.89k stars 4.73k forks source link

Cannot run JUnit 4 parameterized methods via IDEA when using Vintage #13304

Open marcphilipp opened 4 years ago

marcphilipp commented 4 years ago

Expected Behavior

Given the following JUnit Parameterized test class:

package junit4;

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

import java.util.Arrays;

import static org.junit.Assert.assertTrue;

@RunWith(Parameterized.class)
public class ParameterTest {
    @Parameters(name = "{index}: test({0})={1}")
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {0, true}, {1, false}
        });
    }

    private int input;
    private boolean expected;

    public ParameterTest(int input, boolean expected) {
        this.input = input;
        this.expected = expected;
    }

    @Test
    public void test() {
        assertTrue(expected);
    }
}

Original issue description by @ldaley:

It seems that IDEA has special knowledge of @RunWith(Parameterized.class). If you try to run ParameterTest.test via IDEA, it uses this Gradle command:

./gradlew test --tests "junit4.ParameterTest.test[*]"

This actually works when using JUnit4, because it seems you can target individual parameterised instances. This also works:

./gradlew test --tests "junit4.ParameterTest.test[*1: test(1)=false*]"

However, if you are using vintage then all of this breaks down.

You can however target the base method, which runs all parameterisations with vintage:

./gradlew test --tests "junit4.ParameterTest.test"

Current Behavior

> Task :test FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [junit4.ParameterTest.test[*]](--tests filter)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
3 actionable tasks: 1 executed, 2 up-to-date

Context

Gradle only matches against the raw method name (test), not against the "display name" (e.g. test[0: test(0)=true]): https://github.com/gradle/gradle/blob/01b4d16ab8ca7669599078cf71f455b129932bbd/subprojects/testing-junit-platform/src/main/java/org/gradle/api/internal/tasks/testing/junitplatform/JUnitPlatformTestClassProcessor.java#L179

To make this more consistent with JUnit 4 support, the display name should be matched against as a fallback.

Steps to Reproduce

  1. Extract junit-vintage-examples.zip
  2. Run ./gradlew test --tests "junit4.ParameterTest.test[*]"

Your Environment

Build scan URL: https://scans.gradle.com/s/5seok2ygcjh2a

TomBillietKlarrio commented 3 years ago

Too bad. Ran into it too. No progress on this?