Collect Android program test code coverage

Source: Internet
Author: User

CodeThe coverage rate is mainly used to check which code has not been covered after the test case is executed. The code that has not been covered usually means the function or scenario that has not been covered. For andriodProgramFor example, we are talking about this field in China.ArticleNot much. Record it here for your reference.

Because the andriod program is actually a Java program, we will not talk about collecting the code coverage rate of the andriod native C program here (as described in the following article, it is actually implemented using gcov ), for Java program code coverage statistics, you can use an open-source software, Emma, which can calculate code coverage without the need for source code. This is because there are two methods to calculate code coverage (see previous articles :):

1. modify the programSource codeTo add code that collects code coverage. For example, gcov adopts this approach.

2. Modify the final program. For example, Emma modifies the Oolong code of the Java class bytecode. In order to trace the statistical code coverage results to the source code, Java is usually compiled into the debug version. The practice is as follows: emma adds the statistical coverage code before each Oolong jump code, and the debugging version of the class contains. source ,. line .. the VaR commands tell the debugger the ing between bytecode and the number reference of Java source code, Java variables, and oolong variables. The advantage of this method is that as long as your program will eventually generate a Java bytecode, for example, scala and other programs, the generated debugging version can be modified with Emma to achieve the purpose of counting code coverage. For Java Virtual Machine support for debugging, see the description in Chapter 7th of programming for the Java Virtual Machine.

Before the andriod program is deployed to a device, a program (dx) will translate the Java bytecode into the bytecode in the andriod Virtual Machine. Therefore, you can use Emma to modify the class file before translation, package again.

In fact, andriod provides an ant build file, which encapsulates this function. Here I have dug up the script and explained the steps. Before explaining this, let's take a look at how to compile a coverage statistics version with ant:

1. generate an ant build file from eclipse's andriod project. andriod-app is the Project name:

Android update project-P Android-app

2. Convert the eclipse andriod test project into an ant project. The-M option specifies the location of the main andriod project corresponding to the test project, and Android-test indicates the name of the test project:

Android update test-project-M ../Android-app-P Android-test

3. Execute the following commands to compile and execute unit tests and collect the coverage rate:

Ant clean Emma debug install test

4. On the device, the/data/<package name>/directory contains a file folder containing coverage. the EC file records the code coverage information, but this information also needs to be mapped with the source code to see which code lines have been overwritten.

5. SetCoverage. ECCopy the file to the bin folder of the andriod main project. This folder contains a file name.Coverage. Em, Which records the ing between modified bytecode and source code.

6. To view the code coverage rate, run the following command (Emma. jar is included in andriod SDK). The SP parameter in the following command specifies the source code location:

Java-CP ~ /Android-SDK/tools/lib/Emma. Jar Emma Report-r HTML-in coverage. Em-in coverage. EC-sp ~ /<Andriod-app>/src/

Here, we break down the ant command (assuming that the andriod Eclipse project has been converted into an ant Project). In fact, we can execute the following commands ~ /Research/multiplatformdemoproject is the main project path of andriod:

Cd ~ /Research/multiplatformdemoproject/

# Compile to debug version

Ant debug

# Modify the class bytecode before packaging it into a DEX File

CD Bin

Java-CP ~ /Android-SDK/tools/lib/Emma. Jar Emma instr-IP classes-D instrumented

# Package the class bytecode modified to coverage statistics into an andriod VM File

~ /Android-SDK/platform-tools/dx -- Dex -- debug -- no-optimize -- output = classes. Dex instrumented

~ /Android-SDK/platform-tools/aapt package-v-F-M/home/shiyimin/research/multiplatformdemoproject/androidmanifest. XML-S/home/shiyimin/research/multiplatformdemoproject/RES-I/home/shiyimin/Android-SDK/platforms/Android-8/android. jar-F multiplatformdemoproject.unsigned.apk/home/shiyimin/research/multiplatformdemoproject/bin/

# Signature

Jarsigner-keystore ~ /. Android/debug. keystore-storepass Android-keypass Android-signedjar multiplatformdemoproject.signed.apk multiplatformdemoproject.unsigned.apk androiddebugkey

~ /Android-SDK/tools/zipalign 4 multiplatformdemoproject.signed.apk multiplatformdemoproject.apk

# Deploy the main program to the device

~ /Android-SDK/platform-tools/ADB install multiplatformdemoproject.apk

# Compile test cases

CD tests

Ant debug

# Deploy and execute test cases

~ /Android-SDK/platform-tools/ADB install multiplatformdemoproject.test-debug.apk

~ /Android-SDK/platform-tools/ADB shell am instrument-w-e coverage true cc. IQA. Studio. Demo. Test/Android. Test. instrumentationtestrunner

# View the code coverage result report

CD http://www.cnblogs.com/bin/

~ /Android-SDK/platform-tools/ADB pull/data/CC. IQA. Studio. Demo/files/coverage. ec.

Java-CP ~ /Android-SDK/tools/lib/Emma. Jar Emma Report-r HTML-in coverage. Em-in coverage. EC-sp ~ /Research/multiplatformdemoproject/src/

Below are several:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.