Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(347)

Unified Diff: testing/android/junit/java/src/org/chromium/testing/local/JsonLogger.java

Issue 1003463002: Add json output to Junit tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: testing/android/junit/java/src/org/chromium/testing/local/JsonLogger.java
diff --git a/testing/android/junit/java/src/org/chromium/testing/local/JsonLogger.java b/testing/android/junit/java/src/org/chromium/testing/local/JsonLogger.java
new file mode 100644
index 0000000000000000000000000000000000000000..a876e2cf55c22200924102f05b423343ce6ca4c2
--- /dev/null
+++ b/testing/android/junit/java/src/org/chromium/testing/local/JsonLogger.java
@@ -0,0 +1,117 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.testing.local;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import org.junit.runner.Description;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.PrintStream;
+
+/*
+ Format of the json file mirrors the one generated by
+ build/android/pylib/results/json_results.py.
+ Below is an example json output.
+ {
+ "global_tags": [],
+ "all_tests": [
+ "test1",
+ "test2",
+ ],
+ "disabled_tests": [],
+ "per_iteration_data": [
+ {
+ "test1": [
+ {
+ "status": "SUCCESS",
+ "elapsed_time_ms": 1,
+ "output_snippet": "",
+ "output_snippet_base64": "",
+ "losless_snippet": "".
+ }
+ ],
+ "test2": [
+ {
+ "status": "SUCCESS",
+ "elapsed_time_ms": 1,
+ "output_snippet": "",
+ "output_snippet_base64": "",
+ "losless_snippet": "".
+ }
+ ]
+ }
+ ]
+ }
+*/
+
+/**
+ * Creates json file with junit test information.
+ */
+public class JsonLogger {
+
+ private JSONObject mBaseJsonObject;
+ private JSONObject mBaseTestInfoJsonObject;
+ private File mOutputFile;
+
+ public JsonLogger(File outputFile) {
+ mBaseJsonObject = new JSONObject();
+ mBaseTestInfoJsonObject = new JSONObject();
+ mOutputFile = outputFile;
+
+ try {
+ mBaseJsonObject.put("global_tags", new JSONArray());
+ mBaseJsonObject.put("all_tests", new JSONArray());
+ mBaseJsonObject.put("disabled_tests", new JSONArray());
+ mBaseJsonObject.put("per_iteration_data",
+ new JSONArray().put(mBaseTestInfoJsonObject));
+ } catch (JSONException e) {
+ System.err.println("Unable to create json output.");
+ }
+ }
+
+ /**
+ * Add the results of a test run to the json output.
+ */
+ public void addTestResultInfo(Description test, boolean passed, long elapsedTimeMillis) {
+ JSONObject testInfoJsonObject = new JSONObject();
+
+ try {
+ testInfoJsonObject.put("status", (passed ? "SUCCESS" : "FAILURE"));
+ testInfoJsonObject.put("elapsed_time_ms", elapsedTimeMillis);
+ testInfoJsonObject.put("output_snippet", "");
+ testInfoJsonObject.put("output_snippet_base64", "");
+ testInfoJsonObject.put("losless_snippet", "");
+
+ if (mBaseTestInfoJsonObject.optJSONArray(testName(test)) == null) {
+ mBaseTestInfoJsonObject.put(testName(test), new JSONArray());
+ mBaseJsonObject.getJSONArray("all_tests").put(testName(test));
+ }
+ mBaseTestInfoJsonObject.getJSONArray(testName(test)).put(testInfoJsonObject);
+ } catch (JSONException e) {
+ System.err.println("Unable to log test to json output: " + testName(test));
+ }
+ }
+
+ /**
+ * Writes the json output to a file.
+ */
+ public void writeJsonToFile() {
+ try {
+ PrintStream stream = new PrintStream(new FileOutputStream(mOutputFile));
+ stream.print(mBaseJsonObject);
+ } catch (FileNotFoundException e) {
+ System.err.println("File not found: " + mOutputFile.getPath());
+ }
+ }
+
+ private String testName(Description test) {
+ return test.getClassName() + "." + test.getMethodName();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698