|
1 | 1 | package cucumber.runtime.formatter;
|
2 | 2 |
|
| 3 | +import static java.util.Arrays.asList; |
3 | 4 | import static org.hamcrest.CoreMatchers.containsString;
|
4 | 5 | import static org.hamcrest.CoreMatchers.equalTo;
|
5 | 6 | import static org.junit.Assert.assertThat;
|
6 | 7 | import static org.mockito.Matchers.any;
|
7 | 8 | import static org.mockito.Matchers.eq;
|
8 | 9 | import static org.mockito.Mockito.inOrder;
|
9 | 10 | import static org.mockito.Mockito.mock;
|
10 |
| -import static org.mockito.Mockito.times; |
11 | 11 | import static org.mockito.Mockito.verify;
|
12 | 12 | import static org.mockito.Mockito.when;
|
13 | 13 |
|
|
30 | 30 | import org.robolectric.RobolectricTestRunner;
|
31 | 31 | import org.robolectric.annotation.Config;
|
32 | 32 |
|
33 |
| -import java.util.ArrayList; |
34 | 33 | import java.util.List;
|
35 | 34 |
|
36 | 35 | @Config(manifest = Config.NONE)
|
@@ -464,81 +463,117 @@ public void step_result_contains_only_the_current_scenarios_severest_result() {
|
464 | 463 | }
|
465 | 464 |
|
466 | 465 | @Test
|
467 |
| - public void test_case_names_are_unique_on_equal_scenario_names() { |
| 466 | + public void test_names_within_feature_are_made_unique_by_appending_blank_and_number() { |
| 467 | + |
468 | 468 | // given
|
469 |
| - String[] featureNames = new String[] {"Addition", "Subtraction", "Multiplication", "Division"}; |
470 |
| - String[] scenarioNames = new String[] {"Enter one number", "Enter two numbers"}; |
471 |
| - List<TestCase> testCases = new ArrayList<TestCase>(); |
472 | 469 | final AndroidInstrumentationReporter formatter = new AndroidInstrumentationReporter(runtime, instrumentation);
|
473 |
| - mockResultStatus(firstResult, Result.Type.PASSED); |
474 |
| - // We are using multiple assertions and for-loops in this method, which is a code smell, |
475 |
| - // here it is okay, since we are just answering the question if we got unique test case names |
476 |
| - for (String featureName : featureNames) { |
477 |
| - for (String scenarioName : scenarioNames) { |
478 |
| - for (int exampleIndex = 0; exampleIndex < 3; exampleIndex++) { |
479 |
| - // We use the same scenario name three times in every feature. |
480 |
| - // In practise that happens on scenario outlines. |
481 |
| - testCases.add(newMockedTestCase(featureName, scenarioName)); |
482 |
| - } |
483 |
| - } |
484 |
| - } |
485 |
| - // Use scenario name once again for feature one |
486 |
| - testCases.add(newMockedTestCase(featureNames[0], scenarioNames[0])); |
487 |
| - // Use scenario names with underscore |
488 |
| - testCases.add(newMockedTestCase(featureNames[0], "new_scenario")); |
489 |
| - testCases.add(newMockedTestCase(featureNames[0], "new_scenario")); |
| 470 | + TestCase testCase1 = mockTestCase(testCaseName("not unique name")); |
| 471 | + TestCase testCase2 = mockTestCase(testCaseName("not unique name")); |
| 472 | + TestCase testCase3 = mockTestCase(testCaseName("not unique name")); |
490 | 473 |
|
491 | 474 | // when
|
492 |
| - for (TestCase testCase : testCases) { |
493 |
| - formatter.startTestCase(testCase); |
494 |
| - formatter.finishTestStep(firstResult); |
495 |
| - formatter.finishTestCase(); |
496 |
| - } |
| 475 | + simulateRunningTestCases(formatter, asList(testCase1, testCase2, testCase3)); |
497 | 476 |
|
498 | 477 | // then
|
499 |
| - final int expectedCount = 27; |
500 |
| - final ArgumentCaptor<Bundle> captor1 = ArgumentCaptor.forClass(Bundle.class); |
501 |
| - verify(instrumentation, times(expectedCount)).sendStatus(eq(StatusCodes.START), captor1.capture()); |
502 |
| - final List<Bundle> startBundles = captor1.getAllValues(); |
503 |
| - final ArgumentCaptor<Bundle> captor2 = ArgumentCaptor.forClass(Bundle.class); |
504 |
| - verify(instrumentation, times(expectedCount)).sendStatus(eq(StatusCodes.OK), captor2.capture()); |
505 |
| - final List<Bundle> resultBundles = captor2.getAllValues(); |
506 |
| - final String[] expectedUniqueNames = { |
507 |
| - // Check default behavior |
508 |
| - "Enter one number", "Enter one number 2", "Enter one number 3", |
509 |
| - "Enter two numbers", "Enter two numbers 2", "Enter two numbers 3", |
510 |
| - "Enter one number", "Enter one number 2", "Enter one number 3", |
511 |
| - "Enter two numbers", "Enter two numbers 2", "Enter two numbers 3", |
512 |
| - "Enter one number", "Enter one number 2", "Enter one number 3", |
513 |
| - "Enter two numbers", "Enter two numbers 2", "Enter two numbers 3", |
514 |
| - "Enter one number", "Enter one number 2", "Enter one number 3", |
515 |
| - "Enter two numbers", "Enter two numbers 2", "Enter two numbers 3", |
516 |
| - // Check that order of test cases does not matter |
517 |
| - "Enter one number 4", |
518 |
| - // Check naming behavior on underscores |
519 |
| - "new_scenario", |
520 |
| - "new_scenario_2" |
521 |
| - }; |
522 |
| - for (int i = 0; i < expectedCount; i++) { |
523 |
| - String expectedUniqueName = expectedUniqueNames[i]; |
524 |
| - |
525 |
| - String testMethodNameOnStartTestCase = startBundles.get(i).getString(AndroidInstrumentationReporter.StatusKeys.TEST); |
526 |
| - assertThat(testMethodNameOnStartTestCase, equalTo(expectedUniqueName)); |
527 |
| - |
528 |
| - String testMethodNameOnFinishTestCase = resultBundles.get(i).getString(AndroidInstrumentationReporter.StatusKeys.TEST); |
529 |
| - assertThat(testMethodNameOnFinishTestCase, equalTo(expectedUniqueName)); |
530 |
| - } |
| 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")); |
531 | 537 | }
|
532 | 538 |
|
533 | 539 | private void mockResultStatus(Result result, Result.Type status) {
|
534 | 540 | when(result.getStatus()).thenReturn(status);
|
535 | 541 | when(result.is(Result.Type.PASSED)).thenReturn(status == Result.Type.PASSED);
|
536 | 542 | }
|
537 | 543 |
|
538 |
| - private static TestCase newMockedTestCase(String featureName, String scenarioName) { |
| 544 | + private TestCase mockTestCase(String testCaseName) { |
| 545 | + return mockTestCase(featureUri("path/file.feature"), testCaseName); |
| 546 | + } |
| 547 | + |
| 548 | + private TestCase mockTestCase(String featureUri, String testCaseName) { |
539 | 549 | TestCase testCase = mock(TestCase.class);
|
540 |
| - when(testCase.getUri()).thenReturn("path/" + featureName + ".feature"); |
541 |
| - when(testCase.getName()).thenReturn(scenarioName); |
| 550 | + when(testCase.getUri()).thenReturn(featureUri); |
| 551 | + when(testCase.getName()).thenReturn(testCaseName); |
542 | 552 | return testCase;
|
543 | 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 | + } |
544 | 579 | }
|
0 commit comments