测试套件的无根测试
Unrooted Tests for TestSuites
我想为我们公司的一些常见用例创建一个测试套件构建器(例如 Google Guave 为集合所做的)。
所以我创建了我的构建器,并且一些 class 扩展了 TestSuite
或 TestCase
,如果需要的话可以添加。但是 运行 Eclipse(Mars 4.5.1 - 不要判断)中的所有内容都将测试显示为 "Unrooted Tests" 并且视图无法将它们标记为成功或失败:
Maven 带来以下异常(但它是 Maven,当我到达它时我可以烧掉那个桥,我不相信它与上述有任何关系):
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project org.acme.project Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: There was an error in the forked process
Other questions 声明这是由于将 JUnit 4 用于 JUnit 3 class TestCase
(未弃用,所以...),在这种情况下,问题将be: 如何将此代码转换为 JUnit 4?
@RunWith(Suite.class)
@Suite.SuiteClasses({ MyTest.Suite.class })
public class MyTest {
public static class Suite {
public static TestSuite suite() {
final TestSuite suite = new TestSuite("My Test Suite");
suite.addTest(new CompoundTester(value -> new MyTester(value), "A", "B"));
return suite;
}
}
public static class CompoundTester extends TestSuite {
public CompoundTester(final Function<String, TestCase> testFactory, final String... values) {
String name = "unknown";
for (final String value : values) {
final TestCase testCase = testFactory.apply(value);
name = testCase.getName();
addTest(new TestCase(value.toString()) {
@Override
public void run(final TestResult result) {
testCase.run(result);
}
});
}
setName(name);
}
}
public static class MyTester extends TestCase {
private final String object;
public MyTester(final String object) {
super("testSomething");
this.object = object;
}
public void testSomething() {
Assert.assertNotNull(this.object);
}
}
}
如果匿名 TestCase
class 的 run
方法看起来像这样,它就有效:
@Override
public final void run(final TestResult result) {
result.startTest(this);
result.runProtected(this, () -> testCase.run());
result.endTest(this);
}
虽然不知道为什么。
我想为我们公司的一些常见用例创建一个测试套件构建器(例如 Google Guave 为集合所做的)。
所以我创建了我的构建器,并且一些 class 扩展了 TestSuite
或 TestCase
,如果需要的话可以添加。但是 运行 Eclipse(Mars 4.5.1 - 不要判断)中的所有内容都将测试显示为 "Unrooted Tests" 并且视图无法将它们标记为成功或失败:
Maven 带来以下异常(但它是 Maven,当我到达它时我可以烧掉那个桥,我不相信它与上述有任何关系):
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project org.acme.project Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: There was an error in the forked process
Other questions 声明这是由于将 JUnit 4 用于 JUnit 3 class TestCase
(未弃用,所以...),在这种情况下,问题将be: 如何将此代码转换为 JUnit 4?
@RunWith(Suite.class)
@Suite.SuiteClasses({ MyTest.Suite.class })
public class MyTest {
public static class Suite {
public static TestSuite suite() {
final TestSuite suite = new TestSuite("My Test Suite");
suite.addTest(new CompoundTester(value -> new MyTester(value), "A", "B"));
return suite;
}
}
public static class CompoundTester extends TestSuite {
public CompoundTester(final Function<String, TestCase> testFactory, final String... values) {
String name = "unknown";
for (final String value : values) {
final TestCase testCase = testFactory.apply(value);
name = testCase.getName();
addTest(new TestCase(value.toString()) {
@Override
public void run(final TestResult result) {
testCase.run(result);
}
});
}
setName(name);
}
}
public static class MyTester extends TestCase {
private final String object;
public MyTester(final String object) {
super("testSomething");
this.object = object;
}
public void testSomething() {
Assert.assertNotNull(this.object);
}
}
}
如果匿名 TestCase
class 的 run
方法看起来像这样,它就有效:
@Override
public final void run(final TestResult result) {
result.startTest(this);
result.runProtected(this, () -> testCase.run());
result.endTest(this);
}
虽然不知道为什么。