How to create JUnit test reports for android for jenkins

I am trying to use the "Publish JUnit Test Results Report" in Jenkins, but cannot make it work for my Android testing project. Setting up my Jenkins Android test project is based on this guide: https://wiki.jenkins-ci.org/display/JENKINS/Building+an+Android+app+and+test+project

I hope someone can post an easy step-by-step guide on how to get JUnit test result reports from a test run so that you can use the โ€œPublish JUnit Test Reportโ€. I would like to use this function because the standard output of the Jenkins Junit test console is not very convenient.

I found several guides (not many) on the Internet, but not all of them worked for me. This is what I have tried so far (without success):

  • Just add the "Post JUnit Test Report" post-build-action with no arguments
  • Follow this guide, which assumes reports can be downloaded from the device / emulator ( http://blackriver.to/2012/08/android-continuous-integration-with-ant-and-jenkins-part-2-2/ )
  • Then this is a guide (http://blog.cloudbees.com/2012/11/unit-test-results-code-coverage-and.html), which is similar to the previous guide, but adds a special tool library
  • And then I found this API for ant in junit reports (http://ant.apache.org/manual/Tasks/junitreport.html), but I absolutely don't know how to add this to my android test projected using android sdk

(Some links do not display correctly because they cannot send more than two links with my reputation.)

Help is much appreciated :)

+6
source share
3 answers

I just had to solve this problem for myself, this is how I worked it:

1.) Install the performance plugin 2.) In the project settings, add a new Post build action "Publish JUnit-testresults" 3.) Add / app / build / androidTest -results / connected / *. Xml as the path (the path may vary slightly your computer)

4.) Now, after starting the project for the first time, you have a new link "Last test result", and on the right - a graphical representation.

Hello

+3
source

Include the Spoon library in your jenkins project add a build step to build apk and one more step to run the command

java -jar spoon-runner-1.1.8-jar-with-dependencies.jar \ --apk example-app.apk \ --test-apk example-tests.apk 

to run control tests and view magic.

0
source

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.

0
source

Source: https://habr.com/ru/post/969599/


All Articles