テッド
テッド

Reputation: 906

Run All JUnit Tests VSCode

I've been able to add JUnit 5 to VSCode and run tests on single files, but I'd like to be able to use the 'Run all tests' button in the test explorer pane so that I can get all tests to run at once. However, i'm encountering a problem where when I press this button and I get the following error in output:

Error: Failed to parse the JUnit launch arguments

I think I need to add some kind of launch configuration to pass in arguments to JUnit, but I've not been able to find anything about how to do this. Does anyone have any insight into getting this to work?

Upvotes: 1

Views: 1977

Answers (2)

Asahi92
Asahi92

Reputation: 1

I came across the same situation. And the existing answer didn't work for m situation. Although it has been sovled for same reason, I am still wondering how this happen...

As for my situation, it was that the Junit didn't work suddenly in VScode and it showed the some Error.

The advice given in Error: Failed to parse the JUnit launch arguments #936 seems to be useful, but didn't work for me.

The final two step I did before Junit works again are that I first build workspace build workspace for two times, choosing full and incremental respectively. And then I create a new file through VS code menu and copy the code. And it suddenly works...

Hope it can help.

Upvotes: 0

テッド
テッド

Reputation: 906

I worked this out eventually. For anyone who finds this, the trick is to make sure you add the @Testable annotation above the class declaration. This will allow JUnit to find the tests when you click the run all tests button in the test explorer. Previously I was only using the @Test annotation above method declarations which is not sufficient for auto discovery of tests.

Example code:

import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Test;
import org.junit.platform.commons.annotation.Testable;

@Testable
public class FooTest {
    
    @Test
    public void testIfTrue() {
        assertTrue(false);
    }

}

Upvotes: 2

Related Questions