简体   繁体   English

运行所有 JUnit 测试 VSCode

[英]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.我已经能够将 JUnit 5 添加到 VSCode 并在单个文件上运行测试,但我希望能够使用测试资源管理器窗格中的“运行所有测试”按钮,以便我可以运行所有测试一次。 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.我想我需要添加某种启动配置来将参数传递给 JUnit,但我找不到任何关于如何执行此操作的信息。 Does anyone have any insight into getting this to work?有没有人有任何见解让这个工作?

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.对于发现这一点的任何人,诀窍是确保在类声明上方添加@Testable注释。 This will allow JUnit to find the tests when you click the run all tests button in the test explorer.这将允许 JUnit 在您单击测试资源管理器中的运行所有测试按钮时找到测试。 Previously I was only using the @Test annotation above method declarations which is not sufficient for auto discovery of tests.以前我只使用上面方法声明的@Test注释,这不足以自动发现测试。

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

}

I came across the same situation.我遇到了同样的情况。 And the existing answer didn't work for m situation.现有的答案不适用于 m 情况。 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.至于我的情况,是 Junit 在 VScode 中突然没有工作,它显示了一些错误。

The advice given in Error: Failed to parse the JUnit launch arguments #936 seems to be useful, but didn't work for me. Error: Failed to parse the JUnit launch arguments #936 中给出的建议似乎很有用,但对我不起作用。

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.我在 Junit 再次工作之前做的最后两步是我首先构建工作空间构建工作空间两次,分别选择完整和增量。 And then I create a new file through VS code menu and copy the code.然后我通过 VS 代码菜单创建一个新文件并复制代码。 And it suddenly works...它突然起作用了......

Hope it can help.希望它能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM