查找子包中的所有测试
Finding all tests in a subpackage
当我尝试 运行ning JUnit 测试时,我收到错误消息
No tests found with test runner 'JUnit 4'
因此我尝试了
的解决方案
- No tests found with test runner 'JUnit 4'
- junit: no tests found
- 'No JUnit tests found' in Eclipse
但是,在我的例子中,区别似乎是我在 包级别 而不是文件夹级别上将测试指定为 运行。
运行 如果我指定一个直接包含测试 类 的包(例如 com.example.tests.smoketests
),JUnit 测试会起作用,但如果指定了更高级别的包(例如 com.example.tests
).
如果我在 com.example.tests
中定义一个测试,它会被发现并且 运行。
有没有办法让Eclipse/JUnit在一个包和所有子包中找到测试?
开箱即用,没有什么可以为您做的。你可以在 JUnit4 中使用 Suite
注释来实现这个目标,尽管这仍然需要你为每个包手动定义一个 Suite 测试,然后将它们全部包含在一个聚合 Suite 测试中(这样它们递归地调用子 Suites ).
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import a.sub.package.AnotherSuiteTest.class;
@RunWith(Suite.class)
@Suite.SuiteClasses({
PackageLocalTestClass1.class,
PackageLocalTestClass2.class,
AnotherSuiteTest.class
})
public class JunitTestSuite {
}
我尝试构建自己的实用程序 class,然后创建整个系列的测试。 This article 提供了一个类似的实用程序。
当我尝试 运行ning JUnit 测试时,我收到错误消息
No tests found with test runner 'JUnit 4'
因此我尝试了
的解决方案- No tests found with test runner 'JUnit 4'
- junit: no tests found
- 'No JUnit tests found' in Eclipse
但是,在我的例子中,区别似乎是我在 包级别 而不是文件夹级别上将测试指定为 运行。
运行 如果我指定一个直接包含测试 类 的包(例如 com.example.tests.smoketests
),JUnit 测试会起作用,但如果指定了更高级别的包(例如 com.example.tests
).
如果我在 com.example.tests
中定义一个测试,它会被发现并且 运行。
有没有办法让Eclipse/JUnit在一个包和所有子包中找到测试?
开箱即用,没有什么可以为您做的。你可以在 JUnit4 中使用 Suite
注释来实现这个目标,尽管这仍然需要你为每个包手动定义一个 Suite 测试,然后将它们全部包含在一个聚合 Suite 测试中(这样它们递归地调用子 Suites ).
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import a.sub.package.AnotherSuiteTest.class;
@RunWith(Suite.class)
@Suite.SuiteClasses({
PackageLocalTestClass1.class,
PackageLocalTestClass2.class,
AnotherSuiteTest.class
})
public class JunitTestSuite {
}
我尝试构建自己的实用程序 class,然后创建整个系列的测试。 This article 提供了一个类似的实用程序。