Skip to content

Commit

Permalink
new setup lifcycle mostly done #1905
Browse files Browse the repository at this point in the history
multiple setup tags callable by name supported
the setup scenario is also included in the feature report
they even show up in the timeline report in the scenario-iterator thread
only catch is if the setup runs more than once, it will be duplicated in the report
which may be okay to figure how much overhead it adds
  • Loading branch information
ptrthomas committed Aug 7, 2022
1 parent 26907ec commit 58ff67a
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 33 deletions.
17 changes: 11 additions & 6 deletions karate-core/src/main/java/com/intuit/karate/core/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tag> tags = scenario.getTags();
if (tags != null) {
for (Tag tag : tags) {
if ("setup".equals(tag.getName())) {
return scenario;
List<Tag> 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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public boolean tryAdvance(Consumer<? super ScenarioRuntime> 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);
Expand All @@ -122,8 +122,8 @@ public boolean tryAdvance(Consumer<? super ScenarioRuntime> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -91,5 +81,12 @@ void testUiGoogle() {
assertFalse(fr.result.isFailed());
report();
}

@Test
void testOutlineDynamicFail() {
run("outline-dynamic-fail.feature");
assertTrue(fr.result.isFailed());
report();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature:

# @setup
Scenario:
* def data = [{a: 1}, {a: 2}]

Scenario Outline:
* print __row

Examples:
| karate.setup().data |
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> map = result.getVariables();
match(map.get("name"), "Nyan");
match(map.get("title"), "name is Nyan and age is 7");
Expand Down

0 comments on commit 58ff67a

Please sign in to comment.