|
| 1 | +package io.cucumber.core.plugin; |
| 2 | + |
| 3 | +import io.cucumber.core.feature.TestFeatureParser; |
| 4 | +import io.cucumber.core.gherkin.Feature; |
| 5 | +import io.cucumber.core.runner.TestHelper; |
| 6 | +import io.cucumber.plugin.event.Result; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.mockito.stubbing.Answer; |
| 9 | + |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.File; |
| 12 | +import java.io.PrintStream; |
| 13 | +import java.util.AbstractMap.SimpleEntry; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import static io.cucumber.core.runner.TestHelper.createWriteHookAction; |
| 20 | +import static io.cucumber.core.runner.TestHelper.result; |
| 21 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 22 | +import static org.hamcrest.CoreMatchers.containsString; |
| 23 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 24 | + |
| 25 | +class TeamCityPluginTest { |
| 26 | + |
| 27 | + private final List<Feature> features = new ArrayList<>(); |
| 28 | + private final Map<String, Result> stepsToResult = new HashMap<>(); |
| 29 | + private final Map<String, String> stepsToLocation = new HashMap<>(); |
| 30 | + private final List<SimpleEntry<String, Result>> hooks = new ArrayList<>(); |
| 31 | + private final List<String> hookLocations = new ArrayList<>(); |
| 32 | + private final List<Answer<Object>> hookActions = new ArrayList<>(); |
| 33 | + private final String location = new File("").toURI().toString(); |
| 34 | + |
| 35 | + @Test |
| 36 | + void should_handle_scenario_outline() { |
| 37 | + Feature feature = TestFeatureParser.parse("path/test.feature", "" + |
| 38 | + "Feature: feature name\n" + |
| 39 | + " Scenario Outline: <name>\n" + |
| 40 | + " Given first step\n" + |
| 41 | + " Then <arg> step\n" + |
| 42 | + " Examples: examples name\n" + |
| 43 | + " | name | arg |\n" + |
| 44 | + " | name 1 | second |\n" + |
| 45 | + " | name 2 | third |\n"); |
| 46 | + features.add(feature); |
| 47 | + stepsToLocation.put("first step", "path/step_definitions.java:3"); |
| 48 | + stepsToLocation.put("second step", "path/step_definitions.java:7"); |
| 49 | + stepsToLocation.put("third step", "path/step_definitions.java:11"); |
| 50 | + |
| 51 | + String formatterOutput = runFeaturesWithFormatter(); |
| 52 | + |
| 53 | + assertThat(formatterOutput, containsString("" + |
| 54 | + "##teamcity[enteredTheMatrix timestamp = '1970-01-01T12:00:00.000+0000']\n" + |
| 55 | + "##teamcity[testSuiteStarted timestamp = '1970-01-01T12:00:00.000+0000' name = 'Cucumber']\n" + |
| 56 | + "##teamcity[customProgressStatus testsCategory = 'Scenarios' count = '0' timestamp = '1970-01-01T12:00:00.000+0000']\n" + |
| 57 | + "##teamcity[testSuiteStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = '" + location + "path/test.feature:1' name = 'feature name']\n" + |
| 58 | + "##teamcity[testSuiteStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = '" + location + "path/test.feature:2' name = '<name>']\n" + |
| 59 | + "##teamcity[testSuiteStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = '" + location + "path/test.feature:5' name = 'examples name']\n" + |
| 60 | + "##teamcity[testSuiteStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = '" + location + "path/test.feature:7' name = 'Example #1']\n" + |
| 61 | + "##teamcity[customProgressStatus type = 'testStarted' timestamp = '1970-01-01T12:00:00.000+0000']\n" + |
| 62 | + "##teamcity[testStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = '" + location + "path/test.feature:3' captureStandardOutput = 'true' name = 'first step']\n" + |
| 63 | + "##teamcity[testFinished timestamp = '1970-01-01T12:00:00.000+0000' duration = '0' name = 'first step']\n" + |
| 64 | + "##teamcity[testStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = '" + location + "path/test.feature:4' captureStandardOutput = 'true' name = 'second step']\n" + |
| 65 | + "##teamcity[testFinished timestamp = '1970-01-01T12:00:00.000+0000' duration = '0' name = 'second step']\n" + |
| 66 | + "##teamcity[customProgressStatus type = 'testFinished' timestamp = '1970-01-01T12:00:00.000+0000']\n" + |
| 67 | + "##teamcity[testSuiteFinished timestamp = '1970-01-01T12:00:00.000+0000' name = 'Example #1']\n" + |
| 68 | + "##teamcity[testSuiteStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = '" + location + "path/test.feature:8' name = 'Example #2']\n" + |
| 69 | + "##teamcity[customProgressStatus type = 'testStarted' timestamp = '1970-01-01T12:00:00.000+0000']\n" + |
| 70 | + "##teamcity[testStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = '" + location + "path/test.feature:3' captureStandardOutput = 'true' name = 'first step']\n" + |
| 71 | + "##teamcity[testFinished timestamp = '1970-01-01T12:00:00.000+0000' duration = '0' name = 'first step']\n" + |
| 72 | + "##teamcity[testStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = '" + location + "path/test.feature:4' captureStandardOutput = 'true' name = 'third step']\n" + |
| 73 | + "##teamcity[testFinished timestamp = '1970-01-01T12:00:00.000+0000' duration = '0' name = 'third step']\n" + |
| 74 | + "##teamcity[customProgressStatus type = 'testFinished' timestamp = '1970-01-01T12:00:00.000+0000']\n" + |
| 75 | + "##teamcity[testSuiteFinished timestamp = '1970-01-01T12:00:00.000+0000' name = 'Example #2']\n" + |
| 76 | + "##teamcity[customProgressStatus testsCategory = '' count = '0' timestamp = '1970-01-01T12:00:00.000+0000']\n" + |
| 77 | + "##teamcity[testSuiteFinished timestamp = '1970-01-01T12:00:00.000+0000' name = 'examples name']\n" + |
| 78 | + "##teamcity[testSuiteFinished timestamp = '1970-01-01T12:00:00.000+0000' name = '<name>']\n" + |
| 79 | + "##teamcity[testSuiteFinished timestamp = '1970-01-01T12:00:00.000+0000' name = 'feature name']\n" + |
| 80 | + "##teamcity[testSuiteFinished timestamp = '1970-01-01T12:00:00.000+0000' name = 'Cucumber']\n" |
| 81 | + )); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void should_print_error_message_for_failed_steps() { |
| 86 | + Feature feature = TestFeatureParser.parse("path/test.feature", "" + |
| 87 | + "Feature: feature name\n" + |
| 88 | + " Scenario: scenario name\n" + |
| 89 | + " Given first step\n"); |
| 90 | + features.add(feature); |
| 91 | + stepsToLocation.put("first step", "path/step_definitions.java:3"); |
| 92 | + stepsToResult.put("first step", result("failed")); |
| 93 | + |
| 94 | + String formatterOutput = runFeaturesWithFormatter(); |
| 95 | + |
| 96 | + assertThat(formatterOutput, containsString("" + |
| 97 | + "##teamcity[testFailed timestamp = '1970-01-01T12:00:00.000+0000' duration = '0' message = 'Step failed' details = 'the stack trace' name = 'first step']\n" |
| 98 | + )); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void should_print_error_message_for_before_hooks() { |
| 103 | + Feature feature = TestFeatureParser.parse("path/test.feature", "" + |
| 104 | + "Feature: feature name\n" + |
| 105 | + " Scenario: scenario name\n" + |
| 106 | + " Given first step\n"); |
| 107 | + features.add(feature); |
| 108 | + stepsToLocation.put("first step", "path/step_definitions.java:3"); |
| 109 | + stepsToResult.put("first step", result("passed")); |
| 110 | + hooks.add(TestHelper.hookEntry("before", result("failed"))); |
| 111 | + hookLocations.add("HookDefinition.java:3"); |
| 112 | + |
| 113 | + String formatterOutput = runFeaturesWithFormatter(); |
| 114 | + |
| 115 | + assertThat(formatterOutput, containsString("" + |
| 116 | + "##teamcity[testStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = 'java:test://HookDefinition.java:3' captureStandardOutput = 'true' name = 'Before']\n" + |
| 117 | + "##teamcity[testFailed timestamp = '1970-01-01T12:00:00.000+0000' duration = '0' message = 'Step failed' details = 'the stack trace' name = 'Before']\n" |
| 118 | + )); |
| 119 | + } |
| 120 | + |
| 121 | + private String runFeaturesWithFormatter() { |
| 122 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 123 | + PrintStream printStream = new PrintStream(byteArrayOutputStream); |
| 124 | + final TeamCityPlugin formatter = new TeamCityPlugin(printStream); |
| 125 | + |
| 126 | + TestHelper.builder() |
| 127 | + .withFormatterUnderTest(formatter) |
| 128 | + .withFeatures(features) |
| 129 | + .withStepsToResult(stepsToResult) |
| 130 | + .withStepsToLocation(stepsToLocation) |
| 131 | + .withHooks(hooks) |
| 132 | + .withHookLocations(hookLocations) |
| 133 | + .withHookActions(hookActions) |
| 134 | + .build() |
| 135 | + .run(); |
| 136 | + |
| 137 | + return new String(byteArrayOutputStream.toByteArray(), UTF_8); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments