Report generation for test cases for some tests " TestExecutionOrder " and the project " com.example "
adb shell am instrument -w -r -e log true -e class 'com.example.TestExecutionOrder' com.example.test/android.support.test.runner.AndroidJUnitRunner > /home/user/Downloads/raw-tests.txt
This creates a raw test file that contains examples of successful and failed tests. Successful test cases do not have a stack field, but failure test cases have.
Parsing this text file using a simple Java parser.
import java.io.*; public class ReportScript { public static void main(String[] args) { File file = new File("/home/user/Downloads/raw-tests.txt"); FileInputStream fis = null; try { fis = new FileInputStream(file); byte[] data = new byte[(int) file.length()]; fis.read(data); fis.close(); String inputStream = new String(data, "UTF-8"); String[] tests = inputStream.split("INSTRUMENTATION_RESULT: "); String[] testCases = tests[0].split("NSTRUMENTATION_STATUS_CODE: -2|INSTRUMENTATION_STATUS_CODE: -1|INSTRUMENTATION_STATUS_CODE: 0|INSTRUMENTATION_STATUS_CODE: 1|INSTRUMENTATION_STATUS_CODE: 2"); //Don't take last one since blank string String cases=""; int failed = 0; for(int i = 1 ; i < testCases.length-1 ; i+=2){ String[] result = testCases[i].split("INSTRUMENTATION_STATUS: "); String test = " ", classname = " ", time = " " , stack = " "; if(result.length == 7){ for(int j =1 ; j<=6 ; j++){ String[] map = result[j].split("="); String key = map[0]; String value = map[1]; if("test".equalsIgnoreCase(key)){ test = value; }else if ("class".equalsIgnoreCase(key)){ classname = value; }else if ("time".equalsIgnoreCase(key)){ time = value; } } cases += makePassXml(test,classname,time); }else{ for(int j =1 ; j<=6 ; j++){ String[] map = result[j].split("="); String key = map[0]; String value = map[1]; if("test".equalsIgnoreCase(key)){ test = value; }else if ("class".equalsIgnoreCase(key)){ classname = value; }else if ("time".equalsIgnoreCase(key)){ time = value; }else if ("stack".equalsIgnoreCase(key)){ stack = value; } } cases += makeFailXml(test,classname,stack,time); failed++; } } String xml = makeTestSuiteXml(cases,"TestExecutorOrder",(testCases.length-1)/2 + "" ,failed + ""); FileWriter writer = new FileWriter("/home/user/Downloads/junit_report.xml"); writer.write(xml); writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static String makePassXml(String test, String classname, String time){ return "<testcase name=\""+ test +"\" classname=\""+classname+"\" time=\"0.0\"/>\n"; } public static String makeFailXml(String test, String classname, String stack,String time){ return "<testcase name=\""+ test +"\" classname=\""+ classname +"\" time=\"0.0\">\n" + "\t\t<failure message=\"java.lang.AssertionError: test failed \" type=\"java.lang.AssertionError\">" + stack.replaceAll("\\<","") + "</failure>\n" + "\t</testcase>\n"; } public static String makeTestSuiteXml(String cases, String testSuite, String total , String failed){ String top = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<testsuite name=\""+testSuite+"\" tests=\""+total+"\" skipped=\"0\" failures=\""+failed+"\" errors=\"0\" timestamp=\"2019-01-08T18:36:58\" hostname=\"jenkins-android-testing\" time=\"0.000\">\n" + "\t<properties/>\n"; String bottom = "<system-out>\n" + "\t\t<![CDATA[]]>\n" + "\t</system-out>\n" + "\t<system-err>\n" + "\t\t<![CDATA[]]>\n" + "\t</system-err>\n" + "</testsuite>"; return top + cases + bottom; } }
Output :
junit_report.xml will be a JUnit report that can be visualized using the Jenkins Junit report plugin.
source share