|
1 | 1 | package cucumber.runtime.formatter;
|
2 | 2 |
|
| 3 | +import static java.util.Arrays.asList; |
3 | 4 | import static org.hamcrest.CoreMatchers.containsString;
|
| 5 | +import static org.hamcrest.CoreMatchers.equalTo; |
4 | 6 | import static org.junit.Assert.assertThat;
|
5 | 7 | import static org.mockito.Matchers.any;
|
6 | 8 | import static org.mockito.Matchers.eq;
|
|
28 | 30 | import org.robolectric.RobolectricTestRunner;
|
29 | 31 | import org.robolectric.annotation.Config;
|
30 | 32 |
|
| 33 | +import java.util.List; |
| 34 | + |
31 | 35 | @Config(manifest = Config.NONE)
|
32 | 36 | @RunWith(RobolectricTestRunner.class)
|
33 | 37 | public class AndroidInstrumentationReporterTest {
|
@@ -458,8 +462,118 @@ public void step_result_contains_only_the_current_scenarios_severest_result() {
|
458 | 462 | inOrder.verify(instrumentation).sendStatus(eq(StatusCodes.OK), secondCaptor.capture());
|
459 | 463 | }
|
460 | 464 |
|
| 465 | + @Test |
| 466 | + public void test_names_within_feature_are_made_unique_by_appending_blank_and_number() { |
| 467 | + |
| 468 | + // given |
| 469 | + final AndroidInstrumentationReporter formatter = new AndroidInstrumentationReporter(runtime, instrumentation); |
| 470 | + TestCase testCase1 = mockTestCase(testCaseName("not unique name")); |
| 471 | + TestCase testCase2 = mockTestCase(testCaseName("not unique name")); |
| 472 | + TestCase testCase3 = mockTestCase(testCaseName("not unique name")); |
| 473 | + |
| 474 | + // when |
| 475 | + simulateRunningTestCases(formatter, asList(testCase1, testCase2, testCase3)); |
| 476 | + |
| 477 | + // then |
| 478 | + final InOrder inOrder = inOrder(instrumentation); |
| 479 | + assertThat(captureTestName(inOrder, instrumentation), equalTo("not unique name")); |
| 480 | + assertThat(captureTestName(inOrder, instrumentation), equalTo("not unique name 2")); |
| 481 | + assertThat(captureTestName(inOrder, instrumentation), equalTo("not unique name 3")); |
| 482 | + } |
| 483 | + |
| 484 | + @Test |
| 485 | + public void test_names_within_are_made_unique_by_appending_underscore_and_number_when_no_blank_in_name() { |
| 486 | + |
| 487 | + // given |
| 488 | + final AndroidInstrumentationReporter formatter = new AndroidInstrumentationReporter(runtime, instrumentation); |
| 489 | + TestCase testCase1 = mockTestCase(testCaseName("not_unique_name")); |
| 490 | + TestCase testCase2 = mockTestCase(testCaseName("not_unique_name")); |
| 491 | + TestCase testCase3 = mockTestCase(testCaseName("not_unique_name")); |
| 492 | + |
| 493 | + // when |
| 494 | + simulateRunningTestCases(formatter, asList(testCase1, testCase2, testCase3)); |
| 495 | + |
| 496 | + // then |
| 497 | + final InOrder inOrder = inOrder(instrumentation); |
| 498 | + assertThat(captureTestName(inOrder, instrumentation), equalTo("not_unique_name")); |
| 499 | + assertThat(captureTestName(inOrder, instrumentation), equalTo("not_unique_name_2")); |
| 500 | + assertThat(captureTestName(inOrder, instrumentation), equalTo("not_unique_name_3")); |
| 501 | + } |
| 502 | + |
| 503 | + @Test |
| 504 | + public void test_names_in_different_features_can_be_the_same() { |
| 505 | + |
| 506 | + // given |
| 507 | + final AndroidInstrumentationReporter formatter = new AndroidInstrumentationReporter(runtime, instrumentation); |
| 508 | + TestCase testCase1 = mockTestCase(featureUri("path/file1.feature"), testCaseName("not unique name")); |
| 509 | + TestCase testCase2 = mockTestCase(featureUri("path/file2.feature"), testCaseName("not unique name")); |
| 510 | + |
| 511 | + // when |
| 512 | + simulateRunningTestCases(formatter, asList(testCase1, testCase2)); |
| 513 | + |
| 514 | + // then |
| 515 | + final InOrder inOrder = inOrder(instrumentation); |
| 516 | + assertThat(captureTestName(inOrder, instrumentation), equalTo("not unique name")); |
| 517 | + assertThat(captureTestName(inOrder, instrumentation), equalTo("not unique name")); |
| 518 | + } |
| 519 | + |
| 520 | + @Test |
| 521 | + public void test_names_are_made_unique_also_when_not_consecutive() { |
| 522 | + |
| 523 | + // given |
| 524 | + final AndroidInstrumentationReporter formatter = new AndroidInstrumentationReporter(runtime, instrumentation); |
| 525 | + TestCase testCase1 = mockTestCase(testCaseName("not unique name")); |
| 526 | + TestCase testCase2 = mockTestCase(testCaseName("unique name")); |
| 527 | + TestCase testCase3 = mockTestCase(testCaseName("not unique name")); |
| 528 | + |
| 529 | + // when |
| 530 | + simulateRunningTestCases(formatter, asList(testCase1, testCase2, testCase3)); |
| 531 | + |
| 532 | + // then |
| 533 | + final InOrder inOrder = inOrder(instrumentation); |
| 534 | + assertThat(captureTestName(inOrder, instrumentation), equalTo("not unique name")); |
| 535 | + assertThat(captureTestName(inOrder, instrumentation), equalTo("unique name")); |
| 536 | + assertThat(captureTestName(inOrder, instrumentation), equalTo("not unique name 2")); |
| 537 | + } |
| 538 | + |
461 | 539 | private void mockResultStatus(Result result, Result.Type status) {
|
462 | 540 | when(result.getStatus()).thenReturn(status);
|
463 | 541 | when(result.is(Result.Type.PASSED)).thenReturn(status == Result.Type.PASSED);
|
464 | 542 | }
|
| 543 | + |
| 544 | + private TestCase mockTestCase(String testCaseName) { |
| 545 | + return mockTestCase(featureUri("path/file.feature"), testCaseName); |
| 546 | + } |
| 547 | + |
| 548 | + private TestCase mockTestCase(String featureUri, String testCaseName) { |
| 549 | + TestCase testCase = mock(TestCase.class); |
| 550 | + when(testCase.getUri()).thenReturn(featureUri); |
| 551 | + when(testCase.getName()).thenReturn(testCaseName); |
| 552 | + return testCase; |
| 553 | + } |
| 554 | + |
| 555 | + private String testCaseName(String name) { |
| 556 | + return name; |
| 557 | + } |
| 558 | + |
| 559 | + private String featureUri(String uri) { |
| 560 | + return uri; |
| 561 | + } |
| 562 | + |
| 563 | + private void simulateRunningTestCases(AndroidInstrumentationReporter formatter, List<TestCase> testCases) { |
| 564 | + mockResultStatus(firstResult, Result.Type.PASSED); |
| 565 | + formatter.setNumberOfTests(testCases.size()); |
| 566 | + for (TestCase testCase : testCases) { |
| 567 | + formatter.startTestCase(testCase); |
| 568 | + formatter.finishTestStep(firstResult); |
| 569 | + formatter.finishTestCase(); |
| 570 | + } |
| 571 | + } |
| 572 | + |
| 573 | + private String captureTestName(InOrder inOrder, Instrumentation intrumentation) { |
| 574 | + final ArgumentCaptor<Bundle> captor = ArgumentCaptor.forClass(Bundle.class); |
| 575 | + inOrder.verify(instrumentation).sendStatus(eq(StatusCodes.START), captor.capture()); |
| 576 | + final Bundle actualBundle = captor.getValue(); |
| 577 | + return actualBundle.getString(AndroidInstrumentationReporter.StatusKeys.TEST); |
| 578 | + } |
465 | 579 | }
|
0 commit comments