Search in sources :

Example 6 with StubHookDefinition

use of io.cucumber.core.backend.StubHookDefinition in project cucumber-jvm by cucumber.

the class JsonFormatterTest method should_handle_write_from_a_hook.

@Test
void should_handle_write_from_a_hook() {
    Feature feature = TestFeatureParser.parse("path/test.feature", "" + "Feature: Banana party\n" + "\n" + "  Scenario: Monkey eats bananas\n" + "    Given there are bananas\n");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StepDurationTimeService timeService = new StepDurationTimeService(ofMillis(1));
    Runtime.builder().withFeatureSupplier(new StubFeatureSupplier(feature)).withAdditionalPlugins(timeService, new JsonFormatter(out)).withEventBus(new TimeServiceEventBus(timeService, UUID::randomUUID)).withBackendSupplier(new StubBackendSupplier(singletonList(new StubHookDefinition("Hooks.before_hook_1()", testCaseState -> testCaseState.log("printed from hook"))), singletonList(new StubStepDefinition("there are bananas", "StepDefs.there_are_bananas()")), emptyList())).build().run();
    String expected = "" + "[\n" + "  {\n" + "    \"id\": \"banana-party\",\n" + "    \"uri\": \"file:path/test.feature\",\n" + "    \"keyword\": \"Feature\",\n" + "    \"name\": \"Banana party\",\n" + "    \"line\": 1,\n" + "    \"description\": \"\",\n" + "    \"elements\": [\n" + "      {\n" + "        \"id\": \"banana-party;monkey-eats-bananas\",\n" + "        \"keyword\": \"Scenario\",\n" + "        \"start_timestamp\": \"1970-01-01T00:00:00.000Z\",\n" + "        \"name\": \"Monkey eats bananas\",\n" + "        \"line\": 3,\n" + "        \"description\": \"\",\n" + "        \"type\": \"scenario\",\n" + "        \"before\": [\n" + "          {\n" + "            \"match\": {\n" + "              \"location\": \"Hooks.before_hook_1()\"\n" + "            },\n" + "            \"output\": [\n" + "              \"printed from hook\"\n" + "            ],\n" + "            \"result\": {\n" + "              \"status\": \"passed\",\n" + "              \"duration\": 1000000\n" + "            }\n" + "          }\n" + "        ],\n" + "        \"steps\": [\n" + "          {\n" + "            \"keyword\": \"Given \",\n" + "            \"name\": \"there are bananas\",\n" + "            \"line\": 4,\n" + "            \"match\": {\n" + "              \"location\": \"StepDefs.there_are_bananas()\"\n" + "            },\n" + "            \"result\": {\n" + "              \"status\": \"passed\",\n" + "              \"duration\": 1000000\n" + "            }\n" + "          }\n" + "        ]\n" + "      }\n" + "    ],\n" + "    \"tags\": []\n" + "  }\n" + "]";
    assertJsonEquals(expected, out);
}
Also used : TimeServiceEventBus(io.cucumber.core.runtime.TimeServiceEventBus) StubBackendSupplier(io.cucumber.core.runtime.StubBackendSupplier) Builder(io.cucumber.core.runtime.Runtime.Builder) ZoneId.of(java.time.ZoneId.of) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Runtime(io.cucumber.core.runtime.Runtime) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) DocString(io.cucumber.docstring.DocString) Scanner(java.util.Scanner) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DataTable(io.cucumber.datatable.DataTable) StubHookDefinition(io.cucumber.core.backend.StubHookDefinition) Clock.fixed(java.time.Clock.fixed) TestFeatureParser(io.cucumber.core.feature.TestFeatureParser) StubFeatureSupplier(io.cucumber.core.runtime.StubFeatureSupplier) StepDurationTimeService(io.cucumber.core.runner.StepDurationTimeService) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Collections.emptyList(java.util.Collections.emptyList) TimeServiceEventBus(io.cucumber.core.runtime.TimeServiceEventBus) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) RuntimeOptionsBuilder(io.cucumber.core.options.RuntimeOptionsBuilder) Feature(io.cucumber.core.gherkin.Feature) StubBackendSupplier(io.cucumber.core.runtime.StubBackendSupplier) SameJSONAs.sameJSONAs(uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs) EPOCH(java.time.Instant.EPOCH) Duration.ofMillis(java.time.Duration.ofMillis) InputStream(java.io.InputStream) StubFeatureSupplier(io.cucumber.core.runtime.StubFeatureSupplier) StubHookDefinition(io.cucumber.core.backend.StubHookDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DocString(io.cucumber.docstring.DocString) UUID(java.util.UUID) Feature(io.cucumber.core.gherkin.Feature) StepDurationTimeService(io.cucumber.core.runner.StepDurationTimeService) Test(org.junit.jupiter.api.Test)

Example 7 with StubHookDefinition

use of io.cucumber.core.backend.StubHookDefinition in project cucumber-jvm by cucumber.

the class JsonFormatterTest method should_handle_embed_from_a_hook.

@Test
void should_handle_embed_from_a_hook() {
    Feature feature = TestFeatureParser.parse("path/test.feature", "" + "Feature: Banana party\n" + "\n" + "  Scenario: Monkey eats bananas\n" + "    Given there are bananas\n");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StepDurationTimeService timeService = new StepDurationTimeService(ofMillis(1));
    Runtime.builder().withFeatureSupplier(new StubFeatureSupplier(feature)).withAdditionalPlugins(timeService, new JsonFormatter(out)).withEventBus(new TimeServiceEventBus(timeService, UUID::randomUUID)).withBackendSupplier(new StubBackendSupplier(singletonList(new StubHookDefinition("Hooks.before_hook_1()", testCaseState -> testCaseState.attach(new byte[] { 1, 2, 3 }, "mime-type;base64", null))), singletonList(new StubStepDefinition("there are bananas", "StepDefs.there_are_bananas()")), emptyList())).build().run();
    String expected = "" + "[\n" + "  {\n" + "    \"id\": \"banana-party\",\n" + "    \"uri\": \"file:path/test.feature\",\n" + "    \"keyword\": \"Feature\",\n" + "    \"name\": \"Banana party\",\n" + "    \"line\": 1,\n" + "    \"description\": \"\",\n" + "    \"elements\": [\n" + "      {\n" + "        \"id\": \"banana-party;monkey-eats-bananas\",\n" + "        \"keyword\": \"Scenario\",\n" + "        \"start_timestamp\": \"1970-01-01T00:00:00.000Z\",\n" + "        \"name\": \"Monkey eats bananas\",\n" + "        \"line\": 3,\n" + "        \"description\": \"\",\n" + "        \"type\": \"scenario\",\n" + "        \"before\": [\n" + "          {\n" + "            \"match\": {\n" + "              \"location\": \"Hooks.before_hook_1()\"\n" + "            },\n" + "            \"embeddings\": [\n" + "              {\n" + "                \"mime_type\": \"mime-type;base64\",\n" + "                \"data\": \"AQID\"\n" + "              }\n" + "            ],\n" + "            \"result\": {\n" + "              \"status\": \"passed\",\n" + "              \"duration\": 1000000\n" + "            }\n" + "          }\n" + "        ],\n" + "        \"steps\": [\n" + "          {\n" + "            \"keyword\": \"Given \",\n" + "            \"name\": \"there are bananas\",\n" + "            \"line\": 4,\n" + "            \"match\": {\n" + "              \"location\": \"StepDefs.there_are_bananas()\"\n" + "            },\n" + "            \"result\": {\n" + "              \"status\": \"passed\",\n" + "              \"duration\": 1000000\n" + "            }\n" + "          }\n" + "        ]\n" + "      }\n" + "    ],\n" + "    \"tags\": []\n" + "  }\n" + "]";
    assertJsonEquals(expected, out);
}
Also used : TimeServiceEventBus(io.cucumber.core.runtime.TimeServiceEventBus) StubBackendSupplier(io.cucumber.core.runtime.StubBackendSupplier) Builder(io.cucumber.core.runtime.Runtime.Builder) ZoneId.of(java.time.ZoneId.of) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Runtime(io.cucumber.core.runtime.Runtime) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) DocString(io.cucumber.docstring.DocString) Scanner(java.util.Scanner) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DataTable(io.cucumber.datatable.DataTable) StubHookDefinition(io.cucumber.core.backend.StubHookDefinition) Clock.fixed(java.time.Clock.fixed) TestFeatureParser(io.cucumber.core.feature.TestFeatureParser) StubFeatureSupplier(io.cucumber.core.runtime.StubFeatureSupplier) StepDurationTimeService(io.cucumber.core.runner.StepDurationTimeService) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Collections.emptyList(java.util.Collections.emptyList) TimeServiceEventBus(io.cucumber.core.runtime.TimeServiceEventBus) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) RuntimeOptionsBuilder(io.cucumber.core.options.RuntimeOptionsBuilder) Feature(io.cucumber.core.gherkin.Feature) StubBackendSupplier(io.cucumber.core.runtime.StubBackendSupplier) SameJSONAs.sameJSONAs(uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs) EPOCH(java.time.Instant.EPOCH) Duration.ofMillis(java.time.Duration.ofMillis) InputStream(java.io.InputStream) StubFeatureSupplier(io.cucumber.core.runtime.StubFeatureSupplier) StubHookDefinition(io.cucumber.core.backend.StubHookDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DocString(io.cucumber.docstring.DocString) UUID(java.util.UUID) Feature(io.cucumber.core.gherkin.Feature) StepDurationTimeService(io.cucumber.core.runner.StepDurationTimeService) Test(org.junit.jupiter.api.Test)

Example 8 with StubHookDefinition

use of io.cucumber.core.backend.StubHookDefinition in project cucumber-jvm by cucumber.

the class JUnitFormatterTest method should_handle_failure_in_before_hook_with_background.

@Test
void should_handle_failure_in_before_hook_with_background() {
    Feature feature = TestFeatureParser.parse("path/test.feature", "Feature: feature name\n" + "  Background: background name\n" + "    Given first step\n" + "  Scenario: scenario name\n" + "    When second step\n" + "    Then third step\n");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Runtime.builder().withFeatureSupplier(new StubFeatureSupplier(feature)).withAdditionalPlugins(new JUnitFormatter(out)).withEventBus(new TimeServiceEventBus(fixed(EPOCH, of("UTC")), UUID::randomUUID)).withBackendSupplier(new StubBackendSupplier(singletonList(new StubHookDefinition(new StubException("the message", "the stack trace"))), Arrays.asList(new StubStepDefinition("first step"), new StubStepDefinition("second step"), new StubStepDefinition("third step")), singletonList(new StubHookDefinition()))).build().run();
    String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + "<testsuite failures=\"1\" name=\"io.cucumber.core.plugin.JUnitFormatter\" skipped=\"0\" errors=\"0\" tests=\"1\" time=\"0\">\n" + "    <testcase classname=\"feature name\" name=\"scenario name\" time=\"0\">\n" + "        <failure message=\"the message\" type=\"io.cucumber.core.plugin.StubException\"><![CDATA[" + "Given first step............................................................skipped\n" + "When second step............................................................skipped\n" + "Then third step.............................................................skipped\n" + "\n" + "StackTrace:\n" + "the stack trace" + "]]></failure>\n" + "    </testcase>\n" + "</testsuite>\n";
    assertXmlEqual(expected, out);
}
Also used : TimeServiceEventBus(io.cucumber.core.runtime.TimeServiceEventBus) StubBackendSupplier(io.cucumber.core.runtime.StubBackendSupplier) StubFeatureSupplier(io.cucumber.core.runtime.StubFeatureSupplier) StubHookDefinition(io.cucumber.core.backend.StubHookDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UUID(java.util.UUID) Feature(io.cucumber.core.gherkin.Feature) Test(org.junit.jupiter.api.Test)

Example 9 with StubHookDefinition

use of io.cucumber.core.backend.StubHookDefinition in project cucumber-jvm by cucumber.

the class RerunFormatterTest method should_use_scenario_location_when_before_hook_fails.

@Test
void should_use_scenario_location_when_before_hook_fails() {
    Feature feature = TestFeatureParser.parse("classpath:path/test.feature", "" + "Feature: feature name\n" + "  Scenario: scenario name\n" + "    Given first step\n" + "    When second step\n" + "    Then third step\n");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Runtime.builder().withFeatureSupplier(new StubFeatureSupplier(feature)).withAdditionalPlugins(new RerunFormatter(out)).withBackendSupplier(new StubBackendSupplier(singletonList(new StubHookDefinition(new StubException())), asList(new StubStepDefinition("first step"), new StubStepDefinition("second step"), new StubStepDefinition("third step")), emptyList())).build().run();
    assertThat(out, isBytesEqualTo("classpath:path/test.feature:2\n"));
}
Also used : StubBackendSupplier(io.cucumber.core.runtime.StubBackendSupplier) StubFeatureSupplier(io.cucumber.core.runtime.StubFeatureSupplier) StubHookDefinition(io.cucumber.core.backend.StubHookDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Feature(io.cucumber.core.gherkin.Feature) Test(org.junit.jupiter.api.Test)

Example 10 with StubHookDefinition

use of io.cucumber.core.backend.StubHookDefinition in project cucumber-jvm by cucumber.

the class TeamCityPluginTest method should_handle_write_events.

@Test
void should_handle_write_events() {
    Feature feature = TestFeatureParser.parse("path/test.feature", "" + "Feature: feature name\n" + "  Scenario: scenario name\n" + "    Given first step\n");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Runtime.builder().withFeatureSupplier(new StubFeatureSupplier(feature)).withAdditionalPlugins(new TeamCityPlugin(new PrintStream(out))).withEventBus(new TimeServiceEventBus(fixed(EPOCH, of("UTC")), UUID::randomUUID)).withBackendSupplier(new StubBackendSupplier(singletonList(new StubHookDefinition((TestCaseState state) -> state.log("A message"))), singletonList(new StubStepDefinition("first step")), emptyList())).build().run();
    assertThat(out, bytesContainsString("" + "##teamcity[message text='Write event:|nA message|n' status='NORMAL']\n"));
}
Also used : TimeServiceEventBus(io.cucumber.core.runtime.TimeServiceEventBus) StubBackendSupplier(io.cucumber.core.runtime.StubBackendSupplier) StubFeatureSupplier(io.cucumber.core.runtime.StubFeatureSupplier) PrintStream(java.io.PrintStream) StubHookDefinition(io.cucumber.core.backend.StubHookDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UUID(java.util.UUID) Feature(io.cucumber.core.gherkin.Feature) TestCaseState(io.cucumber.core.backend.TestCaseState) Test(org.junit.jupiter.api.Test)

Aggregations

StubHookDefinition (io.cucumber.core.backend.StubHookDefinition)26 StubStepDefinition (io.cucumber.core.backend.StubStepDefinition)26 Feature (io.cucumber.core.gherkin.Feature)26 StubBackendSupplier (io.cucumber.core.runtime.StubBackendSupplier)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)26 Test (org.junit.jupiter.api.Test)26 StubFeatureSupplier (io.cucumber.core.runtime.StubFeatureSupplier)25 TimeServiceEventBus (io.cucumber.core.runtime.TimeServiceEventBus)22 UUID (java.util.UUID)22 RuntimeOptionsBuilder (io.cucumber.core.options.RuntimeOptionsBuilder)8 StepDurationTimeService (io.cucumber.core.runner.StepDurationTimeService)7 TestFeatureParser (io.cucumber.core.feature.TestFeatureParser)6 Runtime (io.cucumber.core.runtime.Runtime)6 DataTable (io.cucumber.datatable.DataTable)6 PrintStream (java.io.PrintStream)6 Arrays.asList (java.util.Arrays.asList)6 Collections.emptyList (java.util.Collections.emptyList)6 Collections.singletonList (java.util.Collections.singletonList)6 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)6 DocString (io.cucumber.docstring.DocString)5