diff --git a/karate-core/src/main/java/com/intuit/karate/core/Feature.java b/karate-core/src/main/java/com/intuit/karate/core/Feature.java index d821499d7..287da48fa 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/Feature.java +++ b/karate-core/src/main/java/com/intuit/karate/core/Feature.java @@ -123,17 +123,22 @@ public Step findStepByLine(int line) { return null; } - public Scenario getSetup() { + public Scenario getSetup(String name) { for (FeatureSection section : sections) { if (section.isOutline()) { continue; } Scenario scenario = section.getScenario(); - List tags = scenario.getTags(); - if (tags != null) { - for (Tag tag : tags) { - if ("setup".equals(tag.getName())) { - return scenario; + List foundTags = scenario.getTags(); + if (foundTags != null) { + for (Tag tag : foundTags) { + if (Tag.SETUP.equals(tag.getName())) { + if (name == null) { + return scenario; + } + if (tag.getValues().contains(name)) { + return scenario; + } } } } 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 1a13f1d12..6e4a58127 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 @@ -538,11 +538,11 @@ public Object getPrevRequest() { public Object getProperties() { return new JsMap(getEngine().runtime.featureRuntime.suite.systemProperties); } - + public Object getResponse() { return getEngine().getResponse(); } - + public Object getRequest() { return getEngine().getRequest(); } @@ -756,15 +756,32 @@ public void set(String name, Value value) { public void set(String name, String path, Object value) { getEngine().set(name, path, new Variable(value)); } - + public Object setup() { + return setup(null); + } + + public Object setup(String name) { ScenarioEngine engine = getEngine(); Feature feature = engine.runtime.featureRuntime.feature; - Scenario scenario = feature.getSetup(); + Scenario scenario = feature.getSetup(name); + if (scenario == null) { + String message = "no scenario found with @setup tag"; + if (name != null) { + message = message + " and name '" + name + "'"; + } + engine.logger.error(message); + throw new RuntimeException(message); + } ScenarioRuntime sr = new ScenarioRuntime(engine.runtime.featureRuntime, scenario); sr.setSkipBackground(true); - sr.run(); - return JsValue.fromJava(sr.engine.getAllVariablesAsMap()); + sr.run(); + ScenarioEngine.set(engine); + FeatureResult result = engine.runtime.featureRuntime.result; + synchronized (result) { + result.addResult(sr.result); + } + return JsValue.fromJava(sr.engine.getAllVariablesAsMap()); } public void setXml(String name, String xml) { @@ -776,7 +793,7 @@ public void setXml(String name, String path, String xml) { getEngine().set(name, path, new Variable(XmlUtils.toXmlDoc(xml))); } - public void signal(Value v) { + public void signal(Value v) { getEngine().signal(JsValue.toJava(v)); } 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 f32983c6d..bb5eaab8b 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 @@ -104,8 +104,8 @@ public boolean tryAdvance(Consumer action) { throw new RuntimeException("result is neither list nor function: " + expressionValue); } } catch (Exception e) { - String message = "dynamic expression evaluation failed: " + expression; - logger.error("{} - {}", currentScenario, message); + String message = currentScenario + " dynamic expression evaluation failed: " + expression; + logger.error(message); dynamicRuntime.result.addFakeStepResult(message, e); currentScenario = null; action.accept(dynamicRuntime); @@ -122,8 +122,8 @@ public boolean tryAdvance(Consumer action) { ScenarioEngine.set(dynamicRuntime.engine); 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); + String message = currentScenario + " dynamic function expression evaluation failed at index " + rowIndex + ": " + e.getMessage(); + logger.error(message); dynamicRuntime.result.addFakeStepResult(message, e); currentScenario = null; action.accept(dynamicRuntime); diff --git a/karate-core/src/main/java/com/intuit/karate/core/ScenarioResult.java b/karate-core/src/main/java/com/intuit/karate/core/ScenarioResult.java index ff5c127ba..cfeedaab7 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/ScenarioResult.java +++ b/karate-core/src/main/java/com/intuit/karate/core/ScenarioResult.java @@ -64,8 +64,7 @@ public int compareTo(ScenarioResult sr) { public String getFailureMessageForDisplay() { if (failedStep == null) { return null; - } - // val message = feature + ":" + step.getLine + " " + result.getStep.getText + } Step step = failedStep.getStep(); String featureName = scenario.getFeature().getResource().getRelativePath(); return featureName + ":" + step.getLine() + " " + step.getText(); @@ -79,7 +78,7 @@ public StepResult addFakeStepResult(String message, Throwable error) { Result result = error == null ? Result.passed(0) : Result.failed(0, error, step); StepResult sr = new StepResult(step, result); if (error != null) { - sr.setStepLog(error.getMessage() + "\n" + StringUtils.throwableToString(error)); + sr.setStepLog(error.getMessage()); } addStepResult(sr); return sr; diff --git a/karate-core/src/test/java/com/intuit/karate/core/FeatureFailRunner.java b/karate-core/src/test/java/com/intuit/karate/core/FeatureFailRunner.java index c1e618ce5..2369b5825 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/FeatureFailRunner.java +++ b/karate-core/src/test/java/com/intuit/karate/core/FeatureFailRunner.java @@ -35,17 +35,7 @@ private File report() { logger.debug("saved report: {}", file.getAbsolutePath()); return file; } - - private void match(Object actual, Object expected) { - Match.Result mr = Match.evaluate(actual).isEqualTo(expected); - assertTrue(mr.pass, mr.message); - } - - private void matchContains(Object actual, Object expected) { - Match.Result mr = Match.evaluate(actual).contains(expected); - assertTrue(mr.pass, mr.message); - } - + @Test void testFailJs() { run("fail-js.feature"); @@ -91,5 +81,12 @@ void testUiGoogle() { assertFalse(fr.result.isFailed()); report(); } + + @Test + void testOutlineDynamicFail() { + run("outline-dynamic-fail.feature"); + assertTrue(fr.result.isFailed()); + report(); + } } diff --git a/karate-core/src/test/java/com/intuit/karate/core/outline-dynamic-fail.feature b/karate-core/src/test/java/com/intuit/karate/core/outline-dynamic-fail.feature new file mode 100644 index 000000000..56d622ec1 --- /dev/null +++ b/karate-core/src/test/java/com/intuit/karate/core/outline-dynamic-fail.feature @@ -0,0 +1,11 @@ +Feature: + +# @setup +Scenario: +* def data = [{a: 1}, {a: 2}] + +Scenario Outline: +* print __row + +Examples: +| karate.setup().data | diff --git a/karate-core/src/test/java/com/intuit/karate/core/parallel/ParallelOutlineTest.java b/karate-core/src/test/java/com/intuit/karate/core/parallel/ParallelOutlineTest.java index 75bcd9568..84b49fe8b 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/parallel/ParallelOutlineTest.java +++ b/karate-core/src/test/java/com/intuit/karate/core/parallel/ParallelOutlineTest.java @@ -36,7 +36,7 @@ void testParallelOutline() { .systemProperty("server.port", server.getPort() + "") .parallel(3); assertEquals(2, results.getFeaturesPassed()); - assertEquals(12, results.getScenariosPassed()); + assertEquals(15, results.getScenariosPassed()); assertEquals(0, results.getFailCount()); } 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 d028ab840..062bd13d9 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 @@ -172,7 +172,7 @@ void testEdgeCases() { @Test void testOutlineDynamic() { FeatureResult result = execute("test-outline-dynamic.feature"); - assertEquals(2, result.getScenarioResults().size()); + assertEquals(3, result.getScenarioResults().size()); Map map = result.getVariables(); match(map.get("name"), "Nyan"); match(map.get("title"), "name is Nyan and age is 7");