※ 상기 사항

1) ADT 20r 버전 이상을 사용

2) Android Application Testing Guide에 나온 방법 처럼 AOSP로 할 경우 잘 안됨.

3) tools/ant/build.xml을 변경하는 것은 비 추천.



1. android tool을 이용해서 project 생성 및 update


 생성: application과 test 각각 1개씩 project를 생성

test project creation: android create test-project -m <main_path> -n <project_name> -p <test_path>


▣ 갱신: eclipse로 기존에 작성된 app과 test project가 있을 경우

app project: $ android update project -p [현재 위치] -n [project 이름]

test project: $ android update test-project -m [app project의 위치] -p [현재위치]



2. ant와 emma를 이용해서 실행


app project: $ ant emma debug install

test project: $ ant emma debug install test



3. 실행 결과 및 자료.


▣ ant emma debug install



 ▣ $ ant emma debug install test





▣ HelloWordTest Coverage Result



▣ TemperatrueConverterTest Coverage Result




관련 파일


HelloWord.zip


HelloWordTest.zip


TemperatureConverter.zip


TemperatureConverterTest.zip




▣ reference site list 


1) ant command[stackoverflow]: http://stackoverflow.com/questions/2762665/how-to-use-emma-code-coverage-in-android 

2) build.xml 수정 한글 블로그: http://codemuri.tistory.com/826 

3) GeekTaco [완벽한 외국 블로그]: http://geektaco.blogspot.kr/2012/07/code-coverage-with-android-and-emma.html

4) google site [android tool 관련]: http://developer.android.com/tools/projects/projects-cmdline.html

5) google site [ am instrumentation과 android test-project 생성 관련]: http://developer.android.com/tools/testing/testing_otheride.html


6) google site [ command line으로 빌드하는 방법]: http://developer.android.com/tools/building/building-cmdline.html

7) unrooted device에 대한 정보: http://stackoverflow.com/questions/8599097/emma-does-not-generate-coverage-ec



3. adb shell am instrument를 이용해서 emma 실행



가장 먼저 coverage.em 를 생성 해야한다. 이때 명령어를 ant instrument 를 해서 coverage를 생성 한다. 

1) app project에서 ant instrument 명령어를 실행해서 coverage.em 을 생성 한다.

root@ubuntu:~/Android_Application_source/HelloWord# ant instrument


Buildfile: /root/Android_Application_source/HelloWord/build.xml


-set-mode-check:

..
..
BUILD SUCCESSFUL
Total time: 1 second


2) app project에서 ant emma debug install 명령어를 실행해서 emma를 포함해서 디버그 모드로 해당 프로젝트를 Target smartphone에 설치를 한다.


위 작업을, app test project에 대해서도 동일하게 해준다. test 프로젝트도 target 스마트폰에 설치를 한다.


root@ubuntu:~/Android_Application_source/HelloWord# ant emma debug install

Buildfile: /root/Android_Application_source/HelloWord/build.xml

emma:

-set-mode-check:
..
..
install:
     [echo] Installing /root/Android_Application_source/HelloWord/bin/HelloWorld-debug.apk onto default emulator or device...
     [exec] 226 KB/s (13892 bytes in 0.059s)
     [exec]  pkg: /data/local/tmp/HelloWorld-debug.apk
     [exec] Success

BUILD SUCCESSFUL
Total time: 5 seconds

----------------------------------------------------------------------

root@ubuntu:~/Android_Application_source/HelloWordTest# ant emma debug install
Buildfile: /root/Android_Application_source/HelloWordTest/build.xml

emma:
..
..
install:
     [echo] Installing /root/Android_Application_source/HelloWord/bin/HelloWorld-instrumented.apk onto default emulator or device...
     [exec] 710 KB/s (166754 bytes in 0.229s)
     [exec] pkg: /data/local/tmp/HelloWorld-instrumented.apk
     [exec] Success

installi:

BUILD SUCCESSFUL
Total time: 12 seconds



3) 아래의 명령어를 실행 해준다.


adb shell am instrument -e coverage true -w com.eslab.test.test/android.test.InstrumentationTestRunner


여기서 com.eslab.test.test 는 테스팅 코드가 instrumentation 되어있는 테스트 프로제긑의 package 이름이다. 뒤의 android.test.InstrumentationTestRunner는 변경하지 말고 다른 test project를 사용할 때에도 그대로 사용해 준다.


root@ubuntu:~/Android_Application_source/HelloWordTest# adb shell am instrument -e coverage true -w com.eslab.test.test/android.test.InstrumentationTestRunner

com.eslab.test.test.HelloAndroidTest:..
Test results for InstrumentationTestRunner=..
Time: 0.786

OK (2 tests)


Generated code coverage data to /data/data/com.eslab.test/files/coverage.ec


// coverage.ec 에 대한 report 생성 위치는 app project의 설치 위치에 저장되어 진다. 

// test project가 아니다.




4) html 파일로 coverage 보고서를 생성해 준다. 이때 app project 위치에서 해야한다.

당연히, coverage.em 파일은 app project에 있는 것을 사용 해야한다. app test project가 아니라.


소스 코드를 참조해서 보고서를 생성하는 명령어: java -cp ~/Android_Application_tools/android-sdk-linux/tools/lib/emma.jar emma emma report -r html -in coverage.ec -sp ~/Android_Application_source/HelloWord/src/ -in coverage.em


소스코드를 참조하지 않고 보고서를 생성하는 명령어: java -cp ~/Android_Application_tools/android-sdk-linux/tools/lib/emma.jar emma report -r html -in coverage.em,coverage.ec


root@ubuntu:~/Android_Application_source/HelloWord/bin# adb pull /data/data/com.eslab.test/files/coverage.ec .

adb server is out of date.  killing...

* daemon started successfully *

2 KB/s (98 bytes in 0.038s)


--------------------------------------------------------------------------------


root@ubuntu:~/Android_Application_source/HelloWord/bin# ls

AndroidManifest.xml               HelloWorld-instrumented-unaligned.apk    build.prop     coverage.ec

AndroidManifest.xml.d             HelloWorld-instrumented-unaligned.apk.d  classes        coverage.em

HelloWorld-debug-unaligned.apk    HelloWorld-instrumented.apk              classes.dex    jarlist.cache

HelloWorld-debug-unaligned.apk.d  HelloWorld.ap_                           classes.dex.d  proguard.txt

HelloWorld-debug.apk              HelloWorld.ap_.d                         coverage       res





root@ubuntu:~/Android_Application_source/HelloWord/bin#  java -cp ~/Android_Application_tools/android-sdk-linux/tools/lib/emma.jar emma report -r html -in coverage.em,coverage.ec

EMMA: processing input files ...

EMMA: 2 file(s) read and merged in 6 ms

EMMA: writing [html] report to [/root/Android_Application_source/HelloWord/bin/coverage/index.html] ...


root@ubuntu:~/Android_Application_source/HelloWord/bin# ls

AndroidManifest.xml               HelloWorld-instrumented-unaligned.apk    build.prop     coverage.ec

AndroidManifest.xml.d             HelloWorld-instrumented-unaligned.apk.d  classes        coverage.em

HelloWorld-debug-unaligned.apk    HelloWorld-instrumented.apk              classes.dex    jarlist.cache

HelloWorld-debug-unaligned.apk.d  HelloWorld.ap_                           classes.dex.d  proguard.txt

HelloWorld-debug.apk              HelloWorld.ap_.d                         coverage       res





결과: ant emma debug install test 보다 좀 더 많은 testing 요소들을 검사한다.








EMMA


1) ant 환경만 지원, build.xml 이 필요.

2) am instrument의 option으로 EMMA를 실행 가능.

3) running android application의 code coverage를 얻기 위해서는 android instrumentation framework를 반드시 이용 해야한다. 얻고자 하는 기등을 안에다가 넣던가, 아니면 code coverage를 수집하는 코드를 저 안에다가 구현 해야 한다.



java -cp ~/Android_Platform/aosp-2.3.3_nexus_one/external/emma/lib/emma.jar emma report -r html -in coverage.ec -sp ~/Android_Application_source/HelloWord/src/ -in coverage.em



Command line으로 Project를 생성 하는 방법.


1) Creating an Android Project

android project를 생성 하기위해서 android tool을 사용하게 된다. 

이것을 사용하면 project directory를 생성하며 몇몇 default application file들과 stub, configuration file, build file을 생성 하게 된다.


android create project

--target or -t <target_ID>

--name or -n <your_project_name>

--path or -p <path/to/your/project>

--activity -a <your_activity_name>

--package -p <your_package_namesapce>





1. Android Application Testing Guide 방법


우선 테스트 대상이 되는 프로그램인 2가지를 구성한다.


TemperatureConverter

TemperatureConverterTest


1) AOSP 컴파일


AOSP를 준비해야한다.


명렁어순서

. build/envsetup.sh

lunch -> generic-eng 선택


export EMMA_INSTRUMENT=true


make -j[작업 수]



2) AOSP에 Built-In App으로 우리의 테스팅 App을집어 넣는다.


테스팅 App의 심볼릭 링크로 만들어서 Android 아래의 디렉터리에 프로젝트 2개를 추가한다.


development/samples/ 

ln -s ~/Android_Application_source/TemperatureConverter .

ln -s ~/Android_Application_source/TemperatureConverterTest .


각 프로젝트 디렉터리에 Android Build System에 들어가 지도록 Android.mk 파일을 추가한다.


일단 samples에존재하는 아무 App이다. 꺼내온 다음. 


LOCAL_PACKAGE_NAME := TemperatureConverter 


로 변경 한다.


컴파일

development/samples/TemperatureConverter $ EMMA_INSTRUMENT=true mm


TemperatureConverterTest의 프로젝트도 동일한 작업 수행.


심볼릭 링크 or File copy

Android.mk 생성

컴파일



3) 코드 커버리지 분석 보고서 생성


컴파일의 결과가는, out/target/common/obj/APPS 디렉터리에 나와있다.

Coverage 측정을 위한 Emulator는 out/host/linux-86/bin/emulator 


Emulator 실행 명령어

$ mksdcard sdcard.img // sdcard 이미지 생성

$ out/host/linux-86/bin/emulator -sdcard sdcard.img -partition-size 256 // emulator 실행

$ adb remount // sdcard에 쓰기권한 설정

development/samples/TemperatureConverterTest$ adb Sync // 변경 사항 동기화


$adb shell am instrument -e coverage 'true' =w com.example.aatg.tc.test/android.test.InstrumentationTestRunner



저장을 위해서 mksdcard 명령어를 이용해서 sdcard.img를 생성해 준다.



에뮬레이터 실행에서 막힘 중단. 


-----------------------------



우선 Ant를 설치한다. 아래의 Web 주소를 참고 한다.

Apache Ant home page: http://ant.apache.org/


▣ Ant Command Reference

ant clean

Cleans the project. If you include the all target before clean (ant all clean), other projects are also cleaned. For instance if you clean a test project, the tested project is also cleaned.

테스팅 대상 프로젝트와 테스트 프로젝트 모두 클린 된다.


ant debug

Builds a debug package. Works on application, library, and test projects and compiles dependencies as needed.

debug package로 모든, 필요한 것들을 컴파일 한다.


ant emma debug

Builds a test project while building the tested project with instrumentation turned on. This is used to run tests with code coverage enabled.

테스팅 되어지는 프로젝트를 instrumentation을 켠상태로 빌드한다. 이것은 테스트 과정에서 code coverage를 측정 할 수 있다.


ant release

Builds a release package.


ant instrument

Builds an instrumented debug package. This is generally called automatically when building a test project with code coverage enabled (with the emma target)

이것은 emma라는 키워드를 사용할때 자동적으로 불리워지는 내용이다. 즉, ant emma를 할시 동일하게 적용되는 내용이다. debug pacakge에 instrumented code를 삽입하는 과정을 취한다.


ant <build_target> install

Builds and installs a package. Using install by itself fails.


ant installd

Installs an already compiled debug package. This fails if the .apk is not already built.


ant installr

Installs an already compiled release package. This fails if the .apk is not already built.


ant installt

Installs an already compiled test package. Also installs the .apk of the tested application. This fails if the .apk is not already built.


ant installi

Installs an already compiled instrumented package. This is generally not used manually as it's called when installing a test package. This fails if the .apk is not already built.


ant test

Runs the tests (for test projects). The tested and test .apk files must be previously installed.


ant debug installt test

Builds a test project and the tested project, installs both .apk files, and runs the tests.


ant emma debug install test

Builds a test project and the tested project, installs both .apk files, and runs the tests with code coverage enabled.


▣ am instrument option reference


am instrument의 일반적인 사용법


am instrument [flags] <COMPONENT>

-r: 결과를 모두 출력한다. 선능 측정을 위한 자료 수집에 유용하다.

-e <NAME> <VALUE>: Name을 Value로 설정 한다. 일반적으로 인자 <name,value> 쌍이다.

-p <FILE> 측정 데이터를 외부 파일에 저장한다.

-w 종료하기 전에 측정 기능이 끝나기를 기다린다. 보통 이 옵션을 쓰지 않으면 테스트 결과를 볼 수 없으므로 필수는 아니지만 꽤 유용하다.




+ Recent posts