안드로이드 TDD 요약

Dev 2025. 2. 21. 16:22

안드로이드 앱을 개발하게 됐습니다.
할 일은 많고 일손은 부족합니다. 무심코 개발을 진행하다 삐끗하면 버그 잡느라 많은 시간을 소진할 것 같습니다. 그래서 웹앱 개발할 때 도움됐던 TDD를 안드로이드 앱 개발에도 적용해보기로 합니다.

관련 내용을 요약합니다. 과제가 마무리되면 소감도 올려보겠습니다.

 

개요

  • 안드로이드는 앱 테스트 자동화를 위해 JUnit(Test Runner)을 사용한다
  • 안드로이드 앱 프로젝트를 생성하면 유닛 테스트(Unit Test)와 기기 테스트(Instrumentation Test) 환경이 자동 생성된다
    • Unit Test: app/src/test 디렉토리의 test 프로젝트
    • Instrumentation Test: app/src/androidTest 디렉토리의 androidTest 프로젝트

 

상세

  • 주로 ViewModel 클래스가 안드로이드 Unit Test의 대상이다
  • JUnit에서 사용하는 주요 어노테이션은 다음과 같다
    • @Before, @After, @Test, @Ignore, ...
  • 테스트 피라미드

 

TDD 관점 상세

  • 테스트 코드를 (구현 코드보다) 먼저 작성해야 TDD다
  • TDD의 효과
    • 테스트 코드가 프로젝트에 대한 최신의(up-to-date한) 문서 역할을 한다
    • 확신을 갖고 개발할 수 있다 (코드 수정으로 인해 깨지는 기능이 없음을 확신할 수 있다)
  • 테스트 피라미드
    • (최상위) End-to-end Test : Appium, Calaba.sh 사용 (둘 다 Selenium 기반 도구)
    • (중간) Integration Test : mockServer with OkHttp 사용
    • (최하위) Unit Test : JUnit, Robolectric, Espresso 사용

테스트 피라미드

  • 다음과 같은 경우 Unit Test가 아니다
    • DB를 호출하는 경우
    • 네트워크를 이용하는 경우
    • 파일 시스템을 건드리는 경우
    • 다른 유닛 테스트와 동시에 실행할 수 없는 경우
    • 테스트 실행을 위해 (환경 변수 설정 같은) 특별한 작업이 필요한 경우

 

테스트 프레임워크 Robolectric

  • app/src/test 디렉토리에 위치하는 Unit Test 작성에 적합하다 (실행 속도가 빠르고 안드로이드 view의 라이프사이클을 테스트할 수 있다)
  • 테스트 코드 샘플 : 버튼을 클릭하면 inputText에 입력한 문자열을 label에 표시
@Test 
public void shouIdDispIayTextFromEditTextOnTextField_whenClickOnChangeHelloWorldButton() throws Exception { 
  EditText inputText = (EditText) mainActivity.findViewById(R.id.label_input);
  inputText.setText("This app is fun!"); 
  changeHelloWorldTextButton.performClick();
  assertThat(helloWordLabel).hasText("This app is fun!");
}

 

테스트 프레임워크 Espresso

  • app/src/androidTest 디렉토리에 위치하는 기기 테스트(instrumentation test: 실제 기기 또는 에뮬레이터에서 실행되는 테스트) 작성에 적합하다
  • Espresso 테스트는 실행 시간이 오래 걸린다 (그래서 빠른 피드백이 필수인 TDD에 적합하지 않다)
  • Unit Test보다 End-to-end Test에 가깝게 쓰는 게 좋다

 

결론 (이렇게 해보기로 했다)

  • app/src/test 디렉토리의 test 프로젝트에서 Roboletric을 이용해서 Unit Test를 작성하면서 TDD를 시도한다
  • 통합 테스트는 필요할 경우(Unit Test 보다 적게), app/src/androidTest 디렉토리의 androidTest 프로젝트에서 Espresso를 이용해서 작성한다
  • e2e 테스트는 정말 필요성할 때만, Appium을 이용해서 작성한다

 

후기

  • 안드로이드 테스트 자동화 관련 최신 자료가 별로 없었다
  • 테스트 도구 자체는 오랫동안 변화가 없었던 듯하다

 

Ref.

Posted by ingeeC
,
웹킷 어디까지 왔나?
(GPU 가속 feature를 중심으로...)


웹킷 뉴스 그룹에서의 주요 안건들 요약
* HTML5 Microdata 지원 공지 (2011/9/22 : New feature announcement - Implement HTML5 Microdata in WebKit)
* HTML5 WebWorker 관련 기능 구현 논의 (2011/9/23 : starting implementation of postMessage tranferables)
* HTML5 Canvas 관련 기능 구현 논의 (2011/9/13 : Mouse Lock API)
* HTML5 time element 관련 기능 구현 논의 (2011/9/16 : Implementing HTML5 time element)
==> 요약: 현재 WebKit 진영에서는 CSS3와 HTML5에 관한 구현 논의가 주요 이슈임


GPU 가속 관련 현황 요약
* GPU는 CPU와 달리 데이터 병렬성이 풍부해 대용량 데이터 처리에 효율적임
* HTML5 Video를 이용 브라우저에서 HD 영상을 표시하기 위해서는 GPU 가속 기능 활용이 필수적임
* WebKit은 화면 처리를 위해 software rendering path와 hardware accelerated path를 제공함
* GPU 가속 feature는 WebKit 차원에서는 종료된 문제임; GPU 가속 기능 제공 여부는 브라우저 port의 문제임
* 애플의 사파리 브라우저는 오래전부터 hardware accelerated path를 심도 깊게 활용함; GPU 가속 기능 제공함
* 애플의 모바일 사파리 브라우저도 아이폰을 처음 출시할 때부터 GPU 가속 기능을 제공함
* 구글의 크롬 브라우저는 2010년 9월 (크롬7) 부터 GPU 가속 기능을 제공하기 시작함
* 구글의 폰용 안드로이드 브라우저는 아직 GPU 가속 기능을 제공하지 않음 (2011/10/19 현재)
* 구글의 패드용 안드로이드는 (안드로이드 3.0 허니콤은) GPU 가속 기능을 제공하나 아직 최적화 되지 않음; 간혹 화면이 깨지는 문제, 성능 문제 등이 보임 (2011/10/19 현재)
* 구글이 곧 발표할 아이스크림 샌드위치는 스마트 폰에서도 GPU 가속 기능을 제공하리라 전망함. 하지만 아직(2011/10/19)까지 구글의 명시적인 발표는 없음; 오늘(2011/10/21) 아이스크림 샌드위치 발표함. GPU 가속 지원 현황에 대해 좀 더 조사가 필요함 (젠장...)
==> 이제 Android 4.0을 (아이스크림 샌드위치를) 사용하는 모든 단말은 GPU 가속 기능을 제공해야 한다. 이로 인해 모든 안드로이드 App (웹 브라우저도 포함하겠지?..)은 GPU 가속 기능의 혜택을 누릴 수 있게 됐다. (2011.10.24. 내용 추가함. GPU 가속 기능이 OS 차원에서 지원된다는 얘기)
http://developer.android.com/sdk/android-4.0-highlights.html#DeveloperApis
(
Hardware-accelerated 2D drawing
All Android-powered devices running Android 4.0 are required to support hardware-accelerated 2D drawing. Developers can take advantage of this to add great UI effects while maintaining optimal performance on high-resolution screens, even on phones. For example, developers can rely on accelerated scaling, rotation, and other 2D operations, as well as accelerated UI components such as TextureView and compositing modes such as filtering, blending, and opacity.)
 
 

Reference
* 브라우저에서 그래픽 가속하기 (from WebKit mailing-group)
Accelerated 2D Tesselation Implementation (from WebKit mailing-group)
Chromium "GPU" LayoutTests (from WebKit mailing-group)
GPU Accelerated Compositing in Chrome (from Chrome 개발자 사이트)
Google Activates Chrome GPU Acceleration
Guess who is WebKit’s new best friend
Issue 6914: Make android use the GPU (if available) for UI and browsing (안드로이드 이슈 페이지)
 
(이상)
 
Posted by ingeeC
,
찾는데 힘들었다. 커맨드 라인 콘솔에서 java 소스 코드를 컴파일해서 ADB shell을 통해 안드로이드 플랫폼에서 실행시키기 까지의 과정을 보여준다.
(원본 링크) http://davanum.wordpress.com/2007/12/04/command-line-java-on-dalvikvm/


December 4, 2007

Command line Java on DalvikVM

Filed under: Uncategorized — Davanum Srinivas @ 11:21 am

Found this very useful when i was trying out the JNI under android (Short Story – Could not get it to work!).

Step #1: Start with a simple java class

package org.apache;

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

Compile the java class

C:\android\CmdLine>javac -d . -g Helloworld.java

Step #2: Package the generated classes into a temporary jar

C:\android\CmdLine>jar -cvf Temp.jar *
added manifest
adding: Hello.java(in = 0) (out= 0)(stored 0%)
adding: HelloWorld.java(in = 149) (out= 122)(deflated 18%)
adding: org/(in = 0) (out= 0)(stored 0%)
adding: org/apache/(in = 0) (out= 0)(stored 0%)
adding: org/apache/HelloWorld.class(in = 556) (out= 344)(deflated 38%)

Step #3: Use the “dx” tool to generate a classes.dex from our temporary jar.

C:\android\CmdLine>dx --dex --output=c:/android/CmdLine/classes.dex c:/android/CmdLine/Temp.jar

Step #4: Use the “aapt” tool to create a new jar suitable for use with dalvikvm

C:\android\CmdLine>aapt add CmdLine.jar classes.dex
'classes.dex'...

and push it into a known location on the emulator

C:\android\CmdLine>adb push CmdLine.jar /data
30 KB/s (0 bytes in 481.000s)

Step #5: kick the tires of dalvikvm

C:\android\CmdLine>adb shell
# /system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -version
/system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -version
DalvikVM version 0.2.0
Copyright (C) 2007 Google, Inc.
Blah blah blah LICENSE blah blah.
Dalvik VM init failed (check log file)

Step #6: Run our code

# /system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -classpath /data/CmdLine.jar org.apache.HelloWorld
/system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -classpath /data/CmdLine.jar org.apache.HelloWorld
Hello World!

(끝)

Posted by ingeeC
,

http://android-developers.blogspot.com/ 블로그에서 NDK를 키워드로 기사를 검색해서 4개의 포스트를 찾았다. 4개 기사의 내용을 요약한다. 영어다. 갑갑한가? 나는 환장하겠다. 당분간 영어로 포스트를 올리게 될 것 같다. -_-;;


http://android-developers.blogspot.com/2010/05/android-22-and-developers-goodies.html
"Android 2.2 and developers goodies."
Posted by Xavier Ducrohet, Android SDK Tech Lead on 20 May 2010 at 9:00 AM
 - 20/May/2010 Android 2.2 announced. (7th platform release since Android 1.0)
 - (on the native side,) add new APIs to access Skia bitmap
 - tools update
  - Android SDK Tools, Revision 6, Eclipse plug-in 0.9.7
  - Android NDK, Revision 4

http://android-developers.blogspot.com/2010/03/android-ndk-r3.html
"Android NDK r3"
Posted by David Turner on 08 March 2010 at 11:25 AM
 - NDK r3 can used to target devices running Android 1.5 and higher
 - use GCC 4.4.0 (previously used GCC 4.2.1)
  so, generate slightly more compact and efficient machine code
 - support OpenGL ES 2.0

http://android-developers.blogspot.com/2009/09/now-available-android-16-ndk.html
"Now available: Android 1.6 NDK"
Posted by David Turner on 29 September 2009 at 10:00 AM
 - NDK is a companion to SDK
 - NDK is used to generate and embed ARM machine code within your App Package
 - NDK 1.6 features
  - support OpenGL ES 1.1
  - many fixes to the NDK build scripts

http://android-developers.blogspot.com/2009/06/introducing-android-15-ndk-release-1.html
"Introducing Android 1.5 NDK, Release 1"
Posted by David Turner on 25 June 2009 at 10:30 AM
 - finally, android App-developers can use native code for their App
 - provide libc, libm, JNI, libz, and liblog libraries


(끝)

Posted by ingeeC
,