Eclipse一直说“在测试运行器JUnit 5中未找到测试". [英] Eclipse keep saying "No tests found with test runner JUnit 5"
问题描述
我正在使用Eclipse Oxygen.3版本(4.7.3).以下是我的JUnit测试类:
I am using Eclipse Oxygen.3 Release (4.7.3). The following is my JUnit test class:
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");
}
}
当我运行此Junit测试时,eclipse会不断弹出对话框,告诉未找到测试运行器'JUnit 5'进行的测试" .为什么?
When I run this Junit test, eclipse keeps popping up dialog telling "No tests found with test runner 'JUnit 5'". Why?
推荐答案
您的测试类当前基于JUnit 4,因为它使用了org.junit
包中的注释.
Your test class is currently based on JUnit 4 since it uses annotations from the org.junit
package.
因此,要使其在Eclipse中作为JUnit 4测试类运行,您需要在运行配置"中选择JUnit 4 Runner.单击绿色圆圈旁边的小箭头(指向下方),其中带有白色箭头(在Eclipse IDE的顶部).在此应该可以看到测试类,并选择可以在JUnit 4和JUnit 5运行器之间切换.
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.
另一方面,如果您的目标是使用JUnit Jupiter(即JUnit 5的编程模型)编写测试,则需要从org.junit
中的注释切换为org.junit.jupiter.api
.
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.junit
to org.junit.jupiter.api
.
这篇关于Eclipse一直说“在测试运行器JUnit 5中未找到测试".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!