From 26907ec1b657cc924c596e6891398ed1c6314ad6 Mon Sep 17 00:00:00 2001 From: Peter Thomas Date: Sun, 7 Aug 2022 11:49:09 +0530 Subject: [PATCH] new setup hook working with build passing #1905 remaining - include the extra scenario in reports documentation, and the support for multiple named setup scenarios in a feature --- .../intuit/karate/core/ScenarioBridge.java | 1 + .../intuit/karate/core/ScenarioIterator.java | 4 ++ .../intuit/karate/core/ScenarioRuntime.java | 17 ++++---- .../main/java/com/intuit/karate/core/Tag.java | 1 + .../java/com/intuit/karate/core/Tags.java | 3 ++ .../karate/core/jsread/js-read-2.feature | 18 ++------ .../karate/core/jsread/js-read-3.feature | 40 ++---------------- .../core/jsread/js-read-called-2.feature | 5 +-- .../karate/core/outline-background.feature | 24 ++++------- .../karate/core/outline-config-js.feature | 9 ++-- .../karate/core/outline-dynamic.feature | 6 ++- .../karate/core/outline-generator.feature | 5 ++- .../karate/core/parallel/parallel-csv.feature | 19 ++++----- .../core/parallel/parallel-outline-1.feature | 13 +++--- .../core/parallel/parallel-outline-2.feature | 7 ++-- .../parallel-outline-simple.feature | 9 ++-- .../karate/core/parser/FeatureParserTest.java | 4 +- .../core/parser/test-outline-dynamic.feature | 5 ++- .../core/parser/test-outline-name-js.feature | 7 ++-- .../core/parser/test-outline-name.feature | 5 ++- .../karate/core/runner/hooks/HooksTest.java | 42 +++++++++---------- .../runner/hooks/hook-dynamic-outline.feature | 7 ++-- .../hook-multiple-dynamic-outline.feature | 13 +++--- .../demo/outline/dynamic-generator.feature | 5 ++- .../test/java/demo/outline/dynamic.feature | 5 ++- .../demos/outline-dynamic-callonce.feature | 7 ++-- .../intuit/karate/junit4/xml/xml-call.feature | 5 ++- 27 files changed, 131 insertions(+), 155 deletions(-) diff --git a/karate-core/src/main/java/com/intuit/karate/core/ScenarioBridge.java b/karate-core/src/main/java/com/intuit/karate/core/ScenarioBridge.java index 577cc3423..1a13f1d12 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/ScenarioBridge.java +++ b/karate-core/src/main/java/com/intuit/karate/core/ScenarioBridge.java @@ -762,6 +762,7 @@ public Object setup() { Feature feature = engine.runtime.featureRuntime.feature; Scenario scenario = feature.getSetup(); ScenarioRuntime sr = new ScenarioRuntime(engine.runtime.featureRuntime, scenario); + sr.setSkipBackground(true); sr.run(); return JsValue.fromJava(sr.engine.getAllVariablesAsMap()); } diff --git a/karate-core/src/main/java/com/intuit/karate/core/ScenarioIterator.java b/karate-core/src/main/java/com/intuit/karate/core/ScenarioIterator.java index 18c12dfe9..f32983c6d 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/ScenarioIterator.java +++ b/karate-core/src/main/java/com/intuit/karate/core/ScenarioIterator.java @@ -31,6 +31,7 @@ import java.util.function.Consumer; import java.util.stream.Stream; import java.util.stream.StreamSupport; +import org.slf4j.Logger; /** * @@ -88,6 +89,7 @@ public boolean tryAdvance(Consumer action) { } } if (currentScenario.isDynamic()) { + Logger logger = FeatureRuntime.logger; if (expressionValue == null) { String expression = currentScenario.getDynamicExpression(); dynamicRuntime = new ScenarioRuntime(featureRuntime, currentScenario); @@ -103,6 +105,7 @@ public boolean tryAdvance(Consumer action) { } } catch (Exception e) { String message = "dynamic expression evaluation failed: " + expression; + logger.error("{} - {}", currentScenario, message); dynamicRuntime.result.addFakeStepResult(message, e); currentScenario = null; action.accept(dynamicRuntime); @@ -120,6 +123,7 @@ public boolean tryAdvance(Consumer action) { rowValue = dynamicRuntime.engine.executeFunction(expressionValue, rowIndex); } catch (Exception e) { String message = "dynamic function expression evaluation failed at index " + rowIndex + ": " + e.getMessage(); + logger.error("{} - {}", currentScenario, message); dynamicRuntime.result.addFakeStepResult(message, e); currentScenario = null; action.accept(dynamicRuntime); diff --git a/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java b/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java index 61a0feeea..773c93808 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java +++ b/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java @@ -60,7 +60,8 @@ public class ScenarioRuntime implements Runnable { public final boolean dryRun; public final LogAppender logAppender; - public boolean ignoringFailureSteps; + private boolean skipBackground; + private boolean ignoringFailureSteps; public ScenarioRuntime(FeatureRuntime featureRuntime, Scenario scenario) { logger = new Logger(); @@ -127,10 +128,6 @@ public boolean isFailed() { return error != null || result.isFailed(); } - public boolean isIgnoringFailureSteps() { - return ignoringFailureSteps; - } - public Step getCurrentStep() { return currentStep; } @@ -139,6 +136,10 @@ public boolean isStopped() { return stopped; } + public void setSkipBackground(boolean skipBackground) { + this.skipBackground = skipBackground; + } + public String getEmbedFileName(ResourceType resourceType) { String extension = resourceType == null ? null : resourceType.getExtension(); return scenario.getUniqueId() + "_" + System.currentTimeMillis() + (extension == null ? "" : "." + extension); @@ -338,8 +339,8 @@ private static boolean isSelectedForExecution(FeatureRuntime fr, Scenario scenar //========================================================================== // - public void beforeRun() { - steps = scenario.getStepsIncludingBackground(); + public void beforeRun() { + steps = skipBackground ? scenario.getSteps() : scenario.getStepsIncludingBackground(); ScenarioEngine.set(engine); engine.init(); result.setExecutorName(Thread.currentThread().getName()); @@ -449,7 +450,7 @@ public StepResult execute(Step step) { if (currentStepResult.isErrorIgnored()) { engine.setFailedReason(null); } - if (!engine.isIgnoringStepErrors() && isIgnoringFailureSteps()) { + if (!engine.isIgnoringStepErrors() && ignoringFailureSteps) { if (engine.getConfig().isContinueAfterContinueOnStepFailure()) { // continue execution and reset failed reason for engine to null engine.setFailedReason(null); diff --git a/karate-core/src/main/java/com/intuit/karate/core/Tag.java b/karate-core/src/main/java/com/intuit/karate/core/Tag.java index c5938f9fb..e2e391680 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/Tag.java +++ b/karate-core/src/main/java/com/intuit/karate/core/Tag.java @@ -36,6 +36,7 @@ public class Tag { public static final String IGNORE = "ignore"; public static final String ENV = "env"; public static final String ENVNOT = "envnot"; + public static final String SETUP = "setup"; private final int line; private final String text; diff --git a/karate-core/src/main/java/com/intuit/karate/core/Tags.java b/karate-core/src/main/java/com/intuit/karate/core/Tags.java index 0ae7968e1..bb67b6c96 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/Tags.java +++ b/karate-core/src/main/java/com/intuit/karate/core/Tags.java @@ -135,6 +135,9 @@ public boolean evaluate(String tagSelector, String karateEnv) { if (StringUtils.containsIgnoreCase(tags, Tag.IGNORE)) { return false; } + if (tagValues.containsKey(Tag.SETUP)) { + return false; + } Values envValues = valuesFor(Tag.ENV); if (envValues.isPresent) { if (karateEnv == null) { diff --git a/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-2.feature b/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-2.feature index 23f682827..1a1ef5144 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-2.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-2.feature @@ -1,29 +1,19 @@ Feature: -Background: - * def anotherVariable = 'hello' +@setup +Scenario: * def data = [{ name: 'one' }, { name: 'two' }] Scenario Outline: * match name == "#present" - * match anotherVariable == "hello" Examples: - | data | + | karate.setup().data | Scenario Outline: * match name == "#present" - * match anotherVariable == "hello" - -Examples: - | name | - | test | - -Scenario Outline: - * match name == "#present" - * match anotherVariable == "hello" * def params = { 'foo': 'bar' } * call read('js-read-called-2.feature') params Examples: - | data | + | karate.setup().data | diff --git a/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-3.feature b/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-3.feature index ebb6be999..852242297 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-3.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-3.feature @@ -1,44 +1,12 @@ Feature: -Background: - * def anotherVariable = 'hello' +@setup +Scenario: * def x = read('js-read-3.json') * def data = x.thirderror - * def backgroundVar = - """ - {"foo": '#(data)' } - """ Scenario Outline: - * match backgroundVar == { "foo": "#(data)" } - * match id == "#present" - * match anotherVariable == "hello" + * match id == '#number' Examples: - | data | - -Scenario Outline: - * def param = - """ - { bar: '#(backgroundVar)'} - """ - * match param == { bar: '#(backgroundVar)'} - * match id == "#present" - * match anotherVariable == "hello" - -Examples: - | data | - - -Scenario Outline: - * def params = - """ - { backgroundVar: '#(backgroundVar)'} - """ - * print params - * match id == "#present" - * match anotherVariable == "hello" - * call read('js-read-called-2.feature') params - -Examples: - | data | + | karate.setup().data | diff --git a/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-called-2.feature b/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-called-2.feature index 5f7e38d83..c745a3ecd 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-called-2.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/jsread/js-read-called-2.feature @@ -1,10 +1,7 @@ Feature: Background: - #* call read('js-read-3.json') * call read('../utils-reuse-common.feature') Scenario: - * print 'arg: ' + __arg - * match __arg == "#present" - * match __arg == "#notnull" \ No newline at end of file + * match __arg == { foo: 'bar' } \ No newline at end of file diff --git a/karate-core/src/test/java/com/intuit/karate/core/outline-background.feature b/karate-core/src/test/java/com/intuit/karate/core/outline-background.feature index 7c9465fb8..c3accdea6 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/outline-background.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/outline-background.feature @@ -1,21 +1,15 @@ Feature: -Background: - * def foo = 'hello' - * def fun = function(){ return 'bar' } - * configure driver = { type: "noopdriver" } - * driver 'http://google.com' - * table data - | name | extra | - | 'one' | | - | 'two' | configSource | +@setup +Scenario: +* table data + | name | extra | + | 'one' | | + | 'two' | configSource | Scenario Outline: - * assert name == 'one' || name == 'two' - * assert name == 'two' ? extra == 'normal' : true - * match foo == "hello" - * match fun() == 'bar' - * click("#testing.usage.driver.method") +* assert name == 'one' || name == 'two' +* assert name == 'two' ? extra == 'normal' : true Examples: - | data | +| karate.setup().data | diff --git a/karate-core/src/test/java/com/intuit/karate/core/outline-config-js.feature b/karate-core/src/test/java/com/intuit/karate/core/outline-config-js.feature index c68997851..c18fda2e3 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/outline-config-js.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/outline-config-js.feature @@ -1,10 +1,11 @@ -Feature: function from global config +Feature: -Background: - * def data = [ { name: 'value1' }, { name: 'value2' }, { name: 'value3' }, { name: 'value4' } ] +@setup +Scenario: + * def data = [{ name: 'value1' }, { name: 'value2' }, { name: 'value3' }, { name: 'value4' }] Scenario Outline: * print 'name:', name Examples: -| data | +| karate.setup().data | diff --git a/karate-core/src/test/java/com/intuit/karate/core/outline-dynamic.feature b/karate-core/src/test/java/com/intuit/karate/core/outline-dynamic.feature index ad4173cbe..e34a34c56 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/outline-dynamic.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/outline-dynamic.feature @@ -1,7 +1,11 @@ Feature: -@ignore @setup +Background: +* print 'in background', __num + +@setup Scenario: +* print 'in setup' * def data = [{a:1}, {a:2}] Scenario Outline: diff --git a/karate-core/src/test/java/com/intuit/karate/core/outline-generator.feature b/karate-core/src/test/java/com/intuit/karate/core/outline-generator.feature index 75be3c80b..179c97ac2 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/outline-generator.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/outline-generator.feature @@ -1,6 +1,7 @@ Feature: -Background: +@setup +Scenario: * def generator = function(i){ if (i == 5) return null; return { name: 'cat' + i, age: i } } Scenario Outline: @@ -8,4 +9,4 @@ Scenario Outline: * match __row.name == 'cat' + age Examples: -| generator | +| karate.setup().generator | diff --git a/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-csv.feature b/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-csv.feature index 03a043e0b..9fc97a37b 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-csv.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-csv.feature @@ -1,26 +1,25 @@ Feature: -Background: -* def isNot = function(x) { return function(row) { return row.id != x } } -* callonce read('call-once-from-feature.feature') +@setup +Scenario: +* def data = read('data.csv') +* def exclude = x => data.filter(y => y.id != x) +* def include = x => data.filter(y => y.id == x) Scenario Outline: -* call read('called.feature') * assert id != '0' Examples: -| karate.filter(read('data.csv'), isNot('0')) | +| karate.setup().data.filter(x => x.id != '0') | Scenario Outline: -* call read('called.feature') * assert id != '1' Examples: -| karate.filter(read('data.csv'), isNot('1')) | +| karate.setup().exclude('1') | Scenario Outline: -* call read('called.feature') -* assert id != '2' +* assert id == '2' Examples: -| karate.filter(read('data.csv'), isNot('2')) | +| karate.setup().include('2') | diff --git a/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-outline-1.feature b/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-outline-1.feature index 58f4df094..225ec6165 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-outline-1.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-outline-1.feature @@ -1,10 +1,8 @@ -@trigger-by-tag Feature: Background: # background http builder should work even for a dynamic scenario outline - * url serverUrl - * def data = [ { name: 'value1' }, { name: 'value2' }, { name: 'value3' }, { name: 'value4' } ] + * url serverUrl # java object that comes from a callSingle in the config * def HelloBg = HelloConfigSingle * callonce read('call-once-from-feature.feature') @@ -23,6 +21,10 @@ Background: } """ +@setup +Scenario: +* def data = [ { name: 'value1' }, { name: 'value2' }, { name: 'value3' }, { name: 'value4' } ] + Scenario Outline: * call read('called.feature') * match functionFromKarateBase() == 'fromKarateBase' @@ -31,13 +33,12 @@ Scenario Outline: * status 200 * match response == { message: 'from feature' } - * match HelloBg.sayHello('world') == 'hello world' * match HelloOnce.sayHello('world') == 'hello world' * match sayHello('world') == 'hello world' Examples: - | data | + | karate.setup().data | Scenario Outline: validating background http context set in background will be shared in shared scope, with dynamic scenario outline * call read('called.feature') @@ -51,4 +52,4 @@ Scenario Outline: validating background http context set in background will be s * match sayHello('world') == 'hello world' Examples: - | data | \ No newline at end of file + | karate.setup().data | \ No newline at end of file diff --git a/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-outline-2.feature b/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-outline-2.feature index 87dfbd0cd..2e5558c6a 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-outline-2.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/parallel/parallel-outline-2.feature @@ -1,10 +1,11 @@ Feature: -Background: - * def data = [ { name: 'value1' }, { name: 'value2' }, { name: 'value3' }, { name: 'value4' } ] +@setup +Scenario: + * def data = [{ name: 'value1' }, { name: 'value2' }, { name: 'value3' }, { name: 'value4' }] Scenario Outline: * match functionFromKarateBase() == 'fromKarateBase' Examples: - | data | + | karate.setup().data | diff --git a/karate-core/src/test/java/com/intuit/karate/core/parasimple/parallel-outline-simple.feature b/karate-core/src/test/java/com/intuit/karate/core/parasimple/parallel-outline-simple.feature index 58f2fc049..7d1df2d99 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/parasimple/parallel-outline-simple.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/parasimple/parallel-outline-simple.feature @@ -1,8 +1,11 @@ Feature: Background: - * def data = [{ name: 'value1' }, { name: 'value2' }, { name: 'value3' }, { name: 'value4' }] - * call read('headers.feature') +* call read('headers.feature') + +@setup +Scenario: + * def data = [{ name: 'value1' }, { name: 'value2' }, { name: 'value3' }, { name: 'value4' }] Scenario Outline: * url 'http://localhost:' + karate.properties['server.port'] @@ -12,5 +15,5 @@ Scenario Outline: * match response == { message: 'from feature' } Examples: - | data | + | karate.setup().data | diff --git a/karate-core/src/test/java/com/intuit/karate/core/parser/FeatureParserTest.java b/karate-core/src/test/java/com/intuit/karate/core/parser/FeatureParserTest.java index 41c671546..d028ab840 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/parser/FeatureParserTest.java +++ b/karate-core/src/test/java/com/intuit/karate/core/parser/FeatureParserTest.java @@ -232,13 +232,13 @@ void testScenariOutlineReadWithoutTags() { Runner.Builder builder = Runner.builder(); builder.tags("@a-tag"); FeatureRuntime fr = FeatureRuntime.of(new Suite(builder), feature); - ScenarioOutline outline = feature.getSection(0).getScenarioOutline(); + ScenarioOutline outline = feature.getSection(1).getScenarioOutline(); assertEquals(1, outline.getScenarios(fr).size()); feature = Feature.read("classpath:com/intuit/karate/core/parser/test-outline-name.feature"); fr = FeatureRuntime.of(new Suite(builder), feature); - outline = feature.getSection(0).getScenarioOutline(); + outline = feature.getSection(1).getScenarioOutline(); assertEquals(2, outline.getScenarios(fr).size()); // using a tag that does not exist in the Examples Tables diff --git a/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-dynamic.feature b/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-dynamic.feature index c88879f2b..9579c11cd 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-dynamic.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-dynamic.feature @@ -1,6 +1,7 @@ Feature: -Background: +@setup +Scenario: * def cats = [{name: 'Bob', age: 5}, {name: 'Nyan', age: 7}] Scenario Outline: name is and age is @@ -9,4 +10,4 @@ Scenario Outline: name is and age is * def title = karate.info.scenarioName Examples: -| cats | +| karate.setup().cats | diff --git a/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-name-js.feature b/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-name-js.feature index e4b249231..6ea80d2ce 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-name-js.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-name-js.feature @@ -1,6 +1,7 @@ Feature: -Background: +@setup +Scenario: * def sum = function(x,y){ return x + y; } * def js_data = """ @@ -57,7 +58,7 @@ Scenario Outline: `name is ${name} and age is ${age}` * match title == karate.scenario.name Examples: - | js_data | + | karate.setup().js_data | Scenario Outline: `name is ${name.first} and age is ${age}` @@ -65,7 +66,7 @@ Scenario Outline: `name is ${name.first} and age is ${age}` * match title == karate.scenario.name Examples: - | nested_js_data | + | karate.setup().nested_js_data | Scenario Outline: `name is ${name.first} ${name.last} and age is ${age}` diff --git a/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-name.feature b/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-name.feature index d043c8057..5c3a04286 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-name.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/parser/test-outline-name.feature @@ -1,6 +1,7 @@ Feature: -Background: +@setup +Scenario: * def js_data = """ [ @@ -34,4 +35,4 @@ Scenario Outline: name is and age is * def title = karate.info.scenarioName Examples: - | js_data | + | karate.setup().js_data | diff --git a/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/HooksTest.java b/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/HooksTest.java index 6e0680261..141f47510 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/HooksTest.java +++ b/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/HooksTest.java @@ -24,11 +24,11 @@ void testDynamicOutlineHook() { assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("beforeFeature").values().stream().mapToInt(Integer::intValue).sum()); assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("afterFeature").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("beforeBackground").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("afterBackground").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("beforeBackground").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("afterBackground").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(2, testRuntimeHook.getRuntimeHookTracker().get("beforeScenario").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(2, testRuntimeHook.getRuntimeHookTracker().get("afterScenario").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(3, testRuntimeHook.getRuntimeHookTracker().get("beforeScenario").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(3, testRuntimeHook.getRuntimeHookTracker().get("afterScenario").values().stream().mapToInt(Integer::intValue).sum()); assertTrue(testRuntimeHook.getRuntimeHookTracker().get("beforeStep").values().stream().mapToInt(Integer::intValue).sum() > 0); assertTrue(testRuntimeHook.getRuntimeHookTracker().get("afterStep").values().stream().mapToInt(Integer::intValue).sum() > 0); @@ -49,11 +49,11 @@ void testMultipleDynamicOutlineMultipleTablesHook() { assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("beforeFeature").values().stream().mapToInt(Integer::intValue).sum()); assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("afterFeature").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(3, testRuntimeHook.getRuntimeHookTracker().get("beforeBackground").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(3, testRuntimeHook.getRuntimeHookTracker().get("afterBackground").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("beforeBackground").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("afterBackground").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(9, testRuntimeHook.getRuntimeHookTracker().get("beforeScenario").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(9, testRuntimeHook.getRuntimeHookTracker().get("afterScenario").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(12, testRuntimeHook.getRuntimeHookTracker().get("beforeScenario").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(12, testRuntimeHook.getRuntimeHookTracker().get("afterScenario").values().stream().mapToInt(Integer::intValue).sum()); assertTrue(testRuntimeHook.getRuntimeHookTracker().get("beforeStep").values().stream().mapToInt(Integer::intValue).sum() > 0); assertTrue(testRuntimeHook.getRuntimeHookTracker().get("afterStep").values().stream().mapToInt(Integer::intValue).sum() > 0); @@ -75,11 +75,11 @@ void testMultipleDynamicOutlineMultipleTablesTagSelectHook() { assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("beforeFeature").values().stream().mapToInt(Integer::intValue).sum()); assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("afterFeature").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(3, testRuntimeHook.getRuntimeHookTracker().get("beforeBackground").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(3, testRuntimeHook.getRuntimeHookTracker().get("afterBackground").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("beforeBackground").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("afterBackground").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(9, testRuntimeHook.getRuntimeHookTracker().get("beforeScenario").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(9, testRuntimeHook.getRuntimeHookTracker().get("afterScenario").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(12, testRuntimeHook.getRuntimeHookTracker().get("beforeScenario").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(12, testRuntimeHook.getRuntimeHookTracker().get("afterScenario").values().stream().mapToInt(Integer::intValue).sum()); // note how before scenario does not evaluate yet the name of the scenario, allowing you to inject stuff into it potentially? assertEquals(7, testRuntimeHook.getRuntimeHookTracker().get("beforeScenario").get("dogs: ${name}")); @@ -115,11 +115,11 @@ void testDynamicOutlineHookNoStepExecution() { // this unit test is also valuable to check that in the error case we are not executing these beforeBackground() / afterBackground() twice // potentially needed for parallel cases - assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("beforeBackground").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("afterBackground").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("beforeBackground").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("afterBackground").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("beforeScenario").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("afterScenario").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("beforeScenario").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("afterScenario").values().stream().mapToInt(Integer::intValue).sum()); assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("beforeStep").values().stream().mapToInt(Integer::intValue).sum()); assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("afterStep").values().stream().mapToInt(Integer::intValue).sum()); @@ -139,15 +139,15 @@ void testDynamicOutlineHookNoScenarioExecution() { assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("beforeFeature").values().stream().mapToInt(Integer::intValue).sum()); assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("afterFeature").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("beforeBackground").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("afterBackground").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("beforeBackground").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("afterBackground").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(2, testRuntimeHook.getRuntimeHookTracker().get("beforeScenario").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("beforeScenario").values().stream().mapToInt(Integer::intValue).sum()); assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("afterScenario").values().stream().mapToInt(Integer::intValue).sum()); // needed to provide the value on the Examples table - assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("beforeStep").values().stream().mapToInt(Integer::intValue).sum()); - assertEquals(1, testRuntimeHook.getRuntimeHookTracker().get("afterStep").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("beforeStep").values().stream().mapToInt(Integer::intValue).sum()); + assertEquals(0, testRuntimeHook.getRuntimeHookTracker().get("afterStep").values().stream().mapToInt(Integer::intValue).sum()); } @Test diff --git a/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/hook-dynamic-outline.feature b/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/hook-dynamic-outline.feature index 8333d0fd8..43c21d0a3 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/hook-dynamic-outline.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/hook-dynamic-outline.feature @@ -1,9 +1,10 @@ Feature: Test Hook Feature -Background: +@setup +Scenario: * def cats = [{name: 'cat1'}, {name: 'cat2'}]; Scenario Outline: cats: ${name} - * match name == "" + * match name == '' Examples: - | cats | + | karate.setup().cats | diff --git a/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/hook-multiple-dynamic-outline.feature b/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/hook-multiple-dynamic-outline.feature index c299747a2..575d62f62 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/hook-multiple-dynamic-outline.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/runner/hooks/hook-multiple-dynamic-outline.feature @@ -1,20 +1,21 @@ @tagged Feature: Test Hook Feature -Background: +@setup +Scenario: * def cats = [{name: 'cat1'}, {name: 'cat2'}]; * def dogs = [{name: 'dog1'}, {name: 'dog2'}, {name: 'dog3'}]; * def taggedDogs = [{name: 'dog1'}, {name: 'dog2'}, {name: 'dog3'}, {name: 'dog4'}]; Scenario Outline: cats: ${name} - * match name == "" + * match name == '' Examples: - | cats | + | karate.setup().cats | Scenario Outline: dogs: ${name} - * match name == "" + * match name == '' Examples: - | dogs | + | karate.setup().dogs | @anothertag Examples: - | taggedDogs | \ No newline at end of file + | karate.setup().taggedDogs | \ No newline at end of file diff --git a/karate-demo/src/test/java/demo/outline/dynamic-generator.feature b/karate-demo/src/test/java/demo/outline/dynamic-generator.feature index 86618f54e..ba098e99c 100644 --- a/karate-demo/src/test/java/demo/outline/dynamic-generator.feature +++ b/karate-demo/src/test/java/demo/outline/dynamic-generator.feature @@ -1,7 +1,8 @@ @mock-servlet-todo Feature: scenario outline using a dynamic generator function -Background: +@setup +Scenario: * def generator = function(i){ if (i == 10) return null; return { name: 'DynaCat' + i, age: i } } Scenario Outline: cat name: @@ -13,5 +14,5 @@ Scenario Outline: cat name: And match response == { id: '#number', name: '#(name)' } Examples: - | generator | + | karate.setup().generator | \ No newline at end of file diff --git a/karate-demo/src/test/java/demo/outline/dynamic.feature b/karate-demo/src/test/java/demo/outline/dynamic.feature index 18d5ae1af..bd23dfebc 100644 --- a/karate-demo/src/test/java/demo/outline/dynamic.feature +++ b/karate-demo/src/test/java/demo/outline/dynamic.feature @@ -1,7 +1,8 @@ @mock-servlet-todo Feature: scenario outline using a dynamic table -Background: +@setup +Scenario: * def kittens = read('../callarray/kittens.json') Scenario Outline: cat name: @@ -15,5 +16,5 @@ Scenario Outline: cat name: # the single cell can be any valid karate expression # and even reference a variable defined in the Background Examples: - | kittens | + | karate.setup().kittens | \ No newline at end of file diff --git a/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/outline-dynamic-callonce.feature b/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/outline-dynamic-callonce.feature index 1ce2e8aa4..171645a2c 100644 --- a/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/outline-dynamic-callonce.feature +++ b/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/outline-dynamic-callonce.feature @@ -1,12 +1,11 @@ Feature: -Background: -* def fun = function(){ karate.log('CALLED') } -* callonce fun +@setup +Scenario: * def data = [{a: 1}, {a: 2}, {a: 3}] Scenario Outline: row * print 'a: ', a Examples: -| data | +| karate.setup().data | diff --git a/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml-call.feature b/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml-call.feature index bdb37bc14..886d51724 100644 --- a/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml-call.feature +++ b/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml-call.feature @@ -1,6 +1,7 @@ Feature: Example -Background: +@setup +Scenario: * def prepare_data = call read('xml-called.feature') * def data = prepare_data.data @@ -9,7 +10,7 @@ Scenario Outline: make sure any json clone operations don't crash during call * print stats Examples: - | data | + | karate.setup().data | Scenario: make sure call arg json conversion for reporting fails gracefully * def moreXml =