下面列出了怎么用org.junit.platform.testkit.engine.EngineTestKit的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
void checkParallelExecution() {
EngineTestKit
.engine("junit-jupiter")
.configurationParameter("junit.jupiter.conditions.deactivate", "org.junit.*DisabledCondition")
.configurationParameter("junit.jupiter.execution.parallel.enabled", "true")
.configurationParameter("junit.jupiter.execution.parallel.mode.default", "concurrent")
.selectors(selectClass(Test1.class),
selectClass(Test2.class),
selectClass(Test3.class),
selectClass(Test4.class))
.execute()
.testEvents()
.debug()
.assertStatistics(stats -> stats.succeeded(4));
}
@Test
void checkSuccessLogs() {
EngineTestKit.engine("junit-jupiter")
.selectors(selectMethod(FakeTest.class, "success"))
.execute()
.testEvents()
.assertStatistics(s -> s.aborted(0).failed(0).succeeded(1).skipped(0));
String logs = writer.toString();
assertThat(logs).contains(SUCCESS_HEADER);
assertThat(logs).contains(SUCCESS_FOOTER);
}
@Test
void checkFailureLogs() {
EngineTestKit.engine("junit-jupiter")
.selectors(selectMethod(FakeTest.class, "failure"))
.execute()
.testEvents()
.assertStatistics(s -> s.aborted(0).failed(1).succeeded(0).skipped(0));
String logs = writer.toString();
assertThat(logs).contains(FAILURE_HEADER);
assertThat(logs).contains(FAILURE_FOOTER);
}
@Test
void checkCaughtLogs() {
EngineTestKit.engine("junit-jupiter")
.selectors(selectMethod(FakeTest.class, "caught"))
.execute()
.testEvents()
.assertStatistics(s -> s.aborted(0).failed(0).succeeded(1).skipped(0));
String logs = writer.toString();
assertThat(logs).contains(CAUGHT_HEADER);
assertThat(logs).contains(CAUGHT_FOOTER);
}
@Test
public void checkSuccessLogs() {
EngineTestKit.engine("junit-vintage")
.selectors(selectMethod(FakeTest.class, "success"))
.execute()
.testEvents()
.assertStatistics(s -> s.aborted(0).failed(0).succeeded(1).skipped(0));
String logs = writer.toString();
assertThat(logs).contains(SUCCESS_HEADER);
assertThat(logs).contains(SUCCESS_FOOTER);
}
@Test
public void checkFailureLogs() {
EngineTestKit.engine("junit-vintage")
.selectors(selectMethod(FakeTest.class, "failure"))
.execute()
.testEvents()
.assertStatistics(s -> s.aborted(0).failed(1).succeeded(0).skipped(0));
String logs = writer.toString();
assertThat(logs).contains(FAILURE_HEADER);
assertThat(logs).contains(FAILURE_FOOTER);
}
@Test
public void checkCaughtLogs() {
EngineTestKit.engine("junit-vintage")
.selectors(selectMethod(FakeTest.class, "caught"))
.execute()
.testEvents()
.assertStatistics(s -> s.aborted(0).failed(0).succeeded(1).skipped(0));
String logs = writer.toString();
assertThat(logs).contains(CAUGHT_HEADER);
assertThat(logs).contains(CAUGHT_FOOTER);
}
@Test
public void checkCaughtByAnnotationLogs() {
EngineTestKit.engine("junit-vintage")
.selectors(selectMethod(FakeTest.class, "caughtByAnnotation"))
.execute()
.testEvents()
.assertStatistics(s -> s.aborted(0).failed(0).succeeded(1).skipped(0));
String logs = writer.toString();
assertThat(logs).contains(CAUGHT_ANNOTATION_HEADER);
assertThat(logs).contains(CAUGHT_ANNOTATION_FOOTER);
}
@Test
public void checkAnnotatedSuccessLogs() {
EngineTestKit.engine("junit-vintage")
.selectors(selectMethod(FakeTest.class, "success"))
.execute()
.testEvents()
.assertStatistics(s -> s.aborted(0).failed(0).succeeded(1).skipped(0));
String logs = writer.toString();
assertThat(logs).contains(SUCCESS_HEADER);
assertThat(logs).contains(SUCCESS_FOOTER);
}
@Test
public void checkAnnotatedFailureLogs() {
EngineTestKit.engine("junit-vintage")
.selectors(selectMethod(FakeTest.class, "failure"))
.execute()
.testEvents()
.assertStatistics(s -> s.aborted(0).failed(1).succeeded(0).skipped(0));
String logs = writer.toString();
assertThat(logs).contains(FAILURE_HEADER);
assertThat(logs).contains(FAILURE_FOOTER);
}
@Test
public void checkAnnotatedCaughtLogs() {
EngineTestKit.engine("junit-vintage")
.selectors(selectMethod(FakeTest.class, "caught"))
.execute()
.testEvents()
.assertStatistics(s -> s.aborted(0).failed(0).succeeded(1).skipped(0));
String logs = writer.toString();
assertThat(logs).contains(CAUGHT_HEADER);
assertThat(logs).contains(CAUGHT_FOOTER);
}
@Test
public void checkAnnotatedCaughtByAnnotationLogs() {
EngineTestKit.engine("junit-vintage")
.selectors(selectMethod(FakeTest.class, "caughtByAnnotation"))
.execute()
.testEvents()
.assertStatistics(s -> s.aborted(0).failed(0).succeeded(1).skipped(0));
String logs = writer.toString();
assertThat(logs).contains(CAUGHT_ANNOTATION_HEADER);
assertThat(logs).contains(CAUGHT_ANNOTATION_FOOTER);
}
@Test
void checkIncorrectFieldDetection() {
EngineTestKit
.engine("junit-jupiter")
.configurationParameter("junit.jupiter.conditions.deactivate", "org.junit.*DisabledCondition")
.selectors(selectClass(Test1.class))
.execute()
.testEvents()
.debug()
.assertStatistics(stats -> stats.succeeded(0));
}
@Test
void checkParallelExecution() {
EngineTestKit
.engine("junit-jupiter")
.selectors(selectClass(Test1.class))
.execute()
.testEvents()
.debug()
.assertStatistics(stats -> stats.succeeded(1));
Assertions.assertTrue(App.shutdown);
}
private EngineExecutionResults getTestCaseEngineExecutionResults(Class<?> testCaseClass) {
return EngineTestKit.engine("junit-jupiter")
.selectors(selectClass(testCaseClass))
.execute();
}