Eclipse 一直说“未找到测试运行程序 JUnit 5 的测试”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50119187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:33:07  来源:igfitidea点击:

Eclipse keep saying "No tests found with test runner JUnit 5"

eclipsejunitjunit5

提问by Leem

I am using Eclipse Oxygen.3 Release (4.7.3). The following is my JUnit test class:

我正在使用 Eclipse Oxygen.3 Release (4.7.3)。以下是我的 JUnit 测试类:

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

class MyMathTest {
    MyMath myMath = new MyMath();

    @Before
    public void before() {
        System.out.println("Before");
    }

    @After
    public void after() {
        System.out.println("After");
    }

    @Test
    public void testSum_with3numbers() {
        System.out.println("Test1");
        int result = myMath.sum(new int[] {1,2,3});
        int expected = 6;
        assertEquals(expected, result);
    }

    @Test
    public void testSum_with1numbers() {
        System.out.println("Test2");
        int result = myMath.sum(new int[] {3});
        int expected = 3;
        assertEquals(expected, result);
    }

    @BeforeClass
    public static void beforeClass() {
        System.out.println("Before class");
    }

    @AfterClass
    public static void afterClass() {
        System.out.println("After class");
    }

}

When I run this Junit test, eclipse keeps popping up dialog telling "No tests found with test runner 'JUnit 5'". Why?

当我运行这个 Junit 测试时,eclipse 不断弹出对话框,告诉"No testing found with test runner 'JUnit 5'"。为什么?

enter image description here

在此处输入图片说明

回答by Sam Brannen

Your test class is currently based on JUnit 4 since it uses annotations from the org.junitpackage.

您的测试类当前基于 JUnit 4,因为它使用org.junit包中的注释。

Thus, to get it to run as a JUnit 4 test class in Eclipse, you need to select the JUnit 4 Runner in the "Run Configuration". Click on the tiny arrow (pointing down) next to the green circle with a white arrow in it (at the top of the Eclipse IDE). There you should see your test class, and by selecting that you can switch between the JUnit 4 and JUnit 5 runners.

因此,要使其在 Eclipse 中作为 JUnit 4 测试类运行,您需要在“运行配置”中选择 JUnit 4 Runner。单击带有白色箭头的绿色圆圈旁边的小箭头(指向下方)(在 Eclipse IDE 的顶部)。在那里您应该会看到您的测试类,通过选择您可以在 JUnit 4 和 JUnit 5 运行程序之间切换。

On the other hand, if your goal is to write a test using JUnit Jupiter (i.e., the programming model for JUnit 5), you'll need to switch from annotations in org.junitto org.junit.jupiter.api.

另一方面,如果您的目标是使用 JUnit Jupiter(即 JUnit 5 的编程模型)编写测试,则需要从注解切换org.junitorg.junit.jupiter.api.

回答by Markus Hettich

In Eclipse 2019-09 (4.13) is a bug that can cause the "No tests found with test runner 'JUnit 5'" error: https://bugs.eclipse.org/bugs/show_bug.cgi?id=551298

在 Eclipse 2019-09 (4.13) 中,有一个错误可能导致“No testing found with test runner 'JUnit 5'”错误:https://bugs.eclipse.org/bugs/show_bug.cgi?id=551298

I added the following dependency to my project and it worked for me:

我在我的项目中添加了以下依赖项,它对我有用:

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-commons</artifactId>
    <version>1.5.2</version><!--$NO-MVN-MAN-VER$-->
    <scope>test</scope>
</dependency>

回答by Cosmin Mavrichi

This happened to me because my test method was declared as private and JUnit could not detect it. After I made it public it worked as expected, of course with @Test annotation.

这发生在我身上,因为我的测试方法被声明为私有并且 JUnit 无法检测到它。在我公开之后,它按预期工作,当然还有@Test 注释。

回答by salerokada

You are using JUnit 4 annotations with JUnit 5 dependencies.
If you want to use JUnit 5 you should replace:

您正在将 JUnit 4 注释与 JUnit 5 依赖项一起使用。
如果你想使用 JUnit 5,你应该替换:

@Before with @BeforeEach
@After with @AfterEach
@BeforeClass with @BeforeAll
@AfterClass with @AfterAll
@Ignore with @Disabled

Here is more about JUnit 5 annotations https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations/

这是有关 JUnit 5 注释的更多信息https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations/

回答by Ravirajsinh Vaghela

Take a look back to your imports.

回顾一下您的进口。

You imports import org.junit.Test;(Used for run test cases with JUnit 4)

您导入import org.junit.Test;(用于使用 JUnit 4 运行测试用例)

You need to import import org.junit.jupiter.api.Test;to run tast case with JUnit5.

您需要导入import org.junit.jupiter.api.Test;以使用 JUnit5 运行 tast case。

回答by S. Stupar

I had the same error, after trying everything, I have realized that the JUnit library wasn't added. So after adding it, tests worked as intended.

我遇到了同样的错误,在尝试了一切之后,我意识到没有添加 JUnit 库。所以在添加它之后,测试按预期工作。

回答by Karsankaka

the test class is not public, make it public and it should work

测试类不是公开的,让它公开,它应该可以工作

回答by Arun Kumar T

right click and run as -> config-> make this change and run it will works cheers!!. enter image description here

右键单击并以身份运行 -> 配置 -> 进行此更改并运行它会奏效! 在此处输入图片说明

回答by Catalin Pirvu

My 2 cents:

我的 2 美分:

Right click on project -> Configure -> Add module-info -> Give some random name. It should automatically add there a requires in the JUnit package, if not, then manually add this:

右键单击项目 -> 配置 -> 添加模块信息 -> 给一些随机名称。它应该会自动在 JUnit 包中添加一个 requires ,如果没有,则手动添加:

requires org.junit.jupiter.api;

And it will magically work!

它会神奇地起作用!

* tested on Eclipse 2018-12

* 在 Eclipse 2018-12 上测试