简体   繁体   English

无法在JUnit套件中添加测试类

[英]Can't add the test classes in JUnit suite

I use Eclipse Oxygen and JUnit 5. I have a class with a method and 3 test cases for the method that work fine. 我使用Eclipse Oxygen和JUnit5。我有一个带有方法的类和该方法正常工作的3个测试用例。 When I try to create a JUnit test suite to group all the cases by new/other/Java/JUnit/Junit test case in the window for test suite nothing appears in "Test classes to include in suite" despite that everything is in one packet and even set to public. 当我尝试创建一个JUnit测试套件以按测试套件窗口中的new / other / Java / JUnit / Junit测试用例对所有用例进行分组时,尽管“所有要包含在套件中的测试类”都没有出现甚至公开。 I create the test suite and manually type in the classes I want to include in the test suit. 我创建测试套件,然后手动键入要包含在测试服中的类。

package testing;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({oddNumberOfLettersPalindromeTest.class, 
evenNumberOfLettersPalindromeTest.class, notAPalindromeTest.class})
public class AllTests {
}

When I run the AllTest suite I get only 1/1 runs for the same AllTest suite class. 当我运行AllTest套件时,对于同一个AllTest套件类,我只会得到1/1个运行。 All my JUnit tests have @Test as well 我所有的JUnit测试也都具有@Test

Correct me if I wrong but do you want to add new classes every time foreach test? 如果我做错了,请纠正我,但是您是否希望每次测试都添加新的类? In that case you can use the @Before tag in JUnit. 在这种情况下,您可以在JUnit中使用@Before标记。 What it does is it generates a new class every time a @Test is hit. 它的作用是每次单击@Test都会生成一个新类。

private TestClass test;

@Before
public void setUp()
{
test = new TestClass();
//You can declare other classes right here
}

did you tried to follow this example ? 您是否尝试遵循此示例?

JUnit - Suite Test - Tutorials Point JUnit-套件测试-教程点

It has to do with versions of JUnit or something, on my laptop it was JUnit 5 and eclipse oxygen, now I am on JUnit4 and eclipse Mars and everything is working fine. 它与JUnit的版本有关,在我的笔记本电脑上是JUnit 5和Eclipse氧气,现在我在JUnit4和Eclipse Mars上运行,一切正常。 I just wanted to group few testcases in one suite 我只是想将几个测试用例组合在一个套件中

It looks like your test suite is not importing the test cases you want to trigger. 看来您的测试套件没有导入要触发的测试用例。 Also, apparently your test cases are not following the class naming pattern (starting with lower case and so on). 同样,您的测试用例显然不遵循类命名模式(从小写字母开始,依此类推)。

package testing;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import OddNumberOfLettersPalindromeTest;
import EvenNumberOfLettersPalindromeTest;
import NotAPalindromeTest;

@RunWith(Suite.class)
@SuiteClasses({OddNumberOfLettersPalindromeTest.class, 
EvenNumberOfLettersPalindromeTest.class, NotAPalindromeTest.class})
public class AllTests {
}

I believe by doing this it has no reason to work. 我相信这样做没有任何理由。

尝试在构建路径->库中将Junit版本更改为Junit4。

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

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