Skip to content

Commit

Permalink
implemented results json output #561
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Oct 27, 2018
1 parent 7648b89 commit ae3e59b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 36 deletions.
87 changes: 60 additions & 27 deletions karate-core/src/main/java/com/intuit/karate/Results.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.intuit.karate.core.ScenarioResult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -35,8 +36,9 @@
*/
public class Results {

private final int threadCount;
private int featureCount;
private int testCount;
private int scenarioCount;
private int failCount;
private int skipCount;
private double timeTakenMillis;
Expand All @@ -47,8 +49,44 @@ public class Results {
private String reportDir;
private final List<ScenarioResult> scenarioResults = new ArrayList();

private Results(long startTime) {
public void printStats(int threadCount) {
System.out.println("Karate version: " + FileUtils.getKarateVersion());
System.out.println("======================================================");
System.out.println(String.format("elapsed: %6.2f | threads: %4d | thread time: %.2f ",
getElapsedTime() / 1000, threadCount, timeTakenMillis / 1000));
System.out.println(String.format("features: %5d | ignored: %4d | efficiency: %.2f", featureCount, skipCount, getEfficiency()));
System.out.println(String.format("scenarios: %4d | passed: %5d | failed: %d",
scenarioCount, getPassCount(), failCount));
System.out.println("======================================================");
System.out.println(getErrorMessages());
if (failureReason != null) {
if (failCount == 0) {
failCount = 1;
}
System.out.println("*** runner exception stack trace ***");
failureReason.printStackTrace();
}
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap();
map.put("version", FileUtils.getKarateVersion());
map.put("threads", threadCount);
map.put("features", featureCount);
map.put("ignored", skipCount);
map.put("scenarios", scenarioCount);
map.put("failed", failCount);
map.put("passed", getPassCount());
map.put("elapsedTime", getElapsedTime());
map.put("totalTime", getTimeTakenMillis());
map.put("efficiency", getEfficiency());
map.put("failures", failedMap);
return map;
}

private Results(long startTime, int threadCount) {
this.startTime = startTime;
this.threadCount = threadCount;
}

public void addToFailedList(String name, String errorMessage) {
Expand All @@ -58,8 +96,8 @@ public void addToFailedList(String name, String errorMessage) {
failedMap.put(name, errorMessage);
}

public static Results startTimer() {
return new Results(System.currentTimeMillis());
public static Results startTimer(int threadCount) {
return new Results(System.currentTimeMillis(), threadCount);
}

public String getReportDir() {
Expand All @@ -78,8 +116,8 @@ public Throwable getFailureReason() {
return failureReason;
}

public void addToTestCount(int count) {
testCount += count;
public void addToScenarioCount(int count) {
scenarioCount += count;
}

public void addToFailCount(int count) {
Expand Down Expand Up @@ -117,25 +155,20 @@ public String getErrorMessages() {
return sb.toString();
}

public void printStats(int threadCount) {
double elapsedTime = endTime - startTime;
System.out.println("Karate version: " + FileUtils.getKarateVersion());
System.out.println("======================================================");
double efficiency = timeTakenMillis / (elapsedTime * threadCount);
System.out.println(String.format("elapsed: %6.2f | threads: %4d | thread time: %.2f ",
elapsedTime / 1000, threadCount, timeTakenMillis / 1000));
System.out.println(String.format("features: %5d | ignored: %4d | efficiency: %.2f", featureCount, skipCount, efficiency));
System.out.println(String.format("scenarios: %4d | passed: %5d | failed: %d",
testCount, testCount - failCount, failCount));
System.out.println("======================================================");
System.out.println(getErrorMessages());
if (failureReason != null) {
if (failCount == 0) {
failCount = 1;
}
System.out.println("*** runner exception stack trace ***");
failureReason.printStackTrace();
}
public double getElapsedTime() {
return endTime - startTime;
}

public double getEfficiency() {
return timeTakenMillis / (getElapsedTime() * threadCount);
}

public int getPassCount() {
return scenarioCount - failCount;
}

public int getThreadCount() {
return threadCount;
}

public void setFeatureCount(int featureCount) {
Expand All @@ -146,8 +179,8 @@ public int getFeatureCount() {
return featureCount;
}

public int getTestCount() {
return testCount;
public int getScenarioCount() {
return scenarioCount;
}

public int getFailCount() {
Expand Down
5 changes: 3 additions & 2 deletions karate-core/src/main/java/com/intuit/karate/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static Results parallel(String tagSelector, List<Resource> resources, Exe
}
final String finalReportDir = reportDir;
logger.info("Karate version: {}", FileUtils.getKarateVersion());
Results results = Results.startTimer();
Results results = Results.startTimer(threadCount);
ExecutorService featureExecutor = Executors.newFixedThreadPool(threadCount);
ExecutorService scenarioExecutor = Executors.newWorkStealingPool(threadCount);
ExecutorService singleExecutor = Executors.newSingleThreadExecutor();
Expand Down Expand Up @@ -122,7 +122,7 @@ public static Results parallel(String tagSelector, List<Resource> resources, Exe
results.stopTimer();
for (FeatureResult result : featureResults) {
int scenarioCount = result.getScenarioCount();
results.addToTestCount(scenarioCount);
results.addToScenarioCount(scenarioCount);
if (scenarioCount != 0) {
executedFeatureCount++;
}
Expand All @@ -143,6 +143,7 @@ public static Results parallel(String tagSelector, List<Resource> resources, Exe
}
results.setFeatureCount(executedFeatureCount);
results.printStats(threadCount);
Engine.saveStatsJson(reportDir, results, null);
Engine.saveTimelineHtml(reportDir, results, null);
results.setReportDir(reportDir);
return results;
Expand Down
21 changes: 16 additions & 5 deletions karate-core/src/main/java/com/intuit/karate/core/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ public static File saveResultJson(String targetDir, FeatureResult result, String
List<Map> single = Collections.singletonList(result.toMap());
String json = JsonUtils.toJson(single);
if (fileName == null) {
fileName = File.separator + result.getPackageQualifiedName() + ".json";
fileName = result.getPackageQualifiedName() + ".json";
}
File file = new File(targetDir + fileName);
File file = new File(targetDir + File.separator + fileName);
FileUtils.writeToFile(file, json);
return file;
}
Expand Down Expand Up @@ -386,9 +386,9 @@ public static File saveResultHtml(String targetDir, FeatureResult result, String
}
}
if (fileName == null) {
fileName = File.separator + baseName + ".html";
fileName = baseName + ".html";
}
File file = new File(targetDir + fileName);
File file = new File(targetDir + File.separator + fileName);
String xml = "<!DOCTYPE html>\n" + XmlUtils.toString(doc, false);
try {
FileUtils.writeToFile(file, xml);
Expand All @@ -405,8 +405,18 @@ public static File saveResultHtml(String targetDir, FeatureResult result, String
private static long getElapsedTime(long startTime) {
return System.nanoTime() - startTime;
}

public static File saveStatsJson(String targetDir, Results results, String fileName) {
String json = JsonUtils.toJson(results.toMap());
if (fileName == null) {
fileName = "results.json";
}
File file = new File(targetDir + File.separator + fileName);
FileUtils.writeToFile(file, json);
return file;
}

public static void saveTimelineHtml(String targetDir, Results results, String fileName) {
public static File saveTimelineHtml(String targetDir, Results results, String fileName) {
Map<String, Integer> groupsMap = new LinkedHashMap();
List<ScenarioResult> scenarioResults = results.getScenarioResults();
List<Map> items = new ArrayList(scenarioResults.size());
Expand Down Expand Up @@ -452,6 +462,7 @@ public static void saveTimelineHtml(String targetDir, Results results, String fi
String html = getClasspathResource("timeline-template.html");
html = html.replaceFirst("//timeline//", sb.toString());
FileUtils.writeToFile(htmlFile, html);
return htmlFile;
}

private static List<MethodMatch> findMethodsMatching(String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class KarateStats {

protected KarateStats(Results from) {
featureCount = from.getFeatureCount();
testCount = from.getTestCount();
testCount = from.getScenarioCount();
failCount = from.getFailCount();
timeTakenMillis = from.getTimeTakenMillis();
startTime = from.getStartTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testHookForExamplesWithTags() {
String path = "classpath:com/intuit/karate/core/test-hook-multiexample.feature";
Results results = Runner.parallel(null, Collections.singletonList(path), new MandatoryTagHook(), 1, null);
assertEquals(1, results.getFeatureCount());
assertEquals(7, results.getTestCount());
assertEquals(7, results.getScenarioCount());
assertEquals(0, results.getFailCount());
}

Expand Down

0 comments on commit ae3e59b

Please sign in to comment.