javavscode icon indicating copy to clipboard operation
javavscode copied to clipboard

Test runner should recognize the Gradle test task which is needed to run the particular JUnit test

Open piotr-rachwalik opened this issue 1 year ago • 1 comments

Description

I have a multi subprojects Gradle Java project. I'm using custom Gradle test tasks to execute only particular test suites. Currently, I'm not able to run these tests using VS Code with Oracle Java platform extension, because the command always looks like this:

../../gradlew --configure-on-demand -x check cleanTest test --tests

I see two solutions:

  1. The extension should automatically resolve what is the proper test task that can run the test
  2. Provide the option to manually set the Gradle command / configuration that could selectable in this view: Screenshot 2024-11-18 at 14 19 05

piotr-rachwalik avatar Nov 18 '24 13:11 piotr-rachwalik

Hi @piotr-rachwalik. Thanks for reaching out and outlining the issue you are facing.

Currently, the following options are possible for performing selective tests runs:

  1. Run tests with multi-selection:
    1. Filter the tests based on names, if needed.
    2. Multi-select test suites or items in the Testing panel.
    3. Click on the Run Test button next to any of the selected items.
  2. Modify the gradle configuration of the top-level test task to filter test tags/categories, names, packages etc.
    1. You may refer to Gradle's docs on test filtering and test grouping for this.
    2. When Run Tests is invoked, you will notice in the Testing panel that only the included tests are run and the excluded tests/modules are skipped.
    3. An example of a gradle config which includes JUnit5 tests tagged "fast" and excludes those tagged "slow":
    tasks.test {
        // Use JUnit Platform for unit tests.
        useJUnitPlatform() {
            includeTags("fast")
            excludeTags("slow")
        }
    }
    
    1. This approach would work for Maven projects too for test filtering and test grouping, along with maven profiles.
  3. Add a VS Code task config to create one or more custom Test tasks which can be launched via the command Tasks: Run Test Task.
    1. This allows for complete customisation of the gradle command that you would like to launch.
    2. You may refer to VS Code docs to create a custom test task
    3. An example test task defined in tasks.json is:
        {
            "label": "test foo",
            "type": "process",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "isTestCommand": true,
            "command": "${workspaceFolder}/gradlew",
            "args": [
                "-x",
                "check",
                "test",
                "--tests",
                "*Foo*"
            ]
        }    
    
    1. Note: Since this mechanism is a completely independent task, it does not provide an integration with the Testing panel nor visually depict the Test Results.

Let us know if this helps in running tests for your project. Thanks.

In the future, we may enhance the extension to support configuring Test Run Profiles, although it appears that VS Code does not support creating a new test profile.

sid-srini avatar Nov 28 '24 09:11 sid-srini