안드로이드에서 단위테스트를 위한 Junit 사용하기 (2)

 

일반 Junit test case 가 아닌 안드로이드에서 제공하는 ActivityUnitTestCase 클래스로

Activity 단위별 테스트를 실행해보자.

먼저 테스트를 위해서 서브 패키지를 하나 만든다이것은 실제 개발소스와 테스트소스를

구분하기 위함이다프로젝트를 선택하고 test/src 라는 폴더를 하나 만든다그리고

Build Path 로 들어가서 source 탭을 클릭한다화면에서 Add Folder 를 클릭해 방금

추가한 경로를 입력한다.

이 패키지에 테스트할 ActivityUnitTestCase 클래스를 만든다
01 import android.content.Intent;
02 import android.test.ActivityUnitTestCase;
03 import android.test.suitebuilder.annotation.MediumTest;
04 import android.util.Log;
05 import android.widget.Button;
06  
07 public class MainTest extends ActivityUnitTestCase<main> {
08      
09     private Intent mStartIntent;
10     private Button mStartButton;
11      
12     public MainTest() {
13         super(Main.class);
14          
15     }
16     protected void setUp() throws Exception {
17         super.setUp();
18         mStartIntent = new Intent(Intent.ACTION_MAIN);
19     }
20      
21     @MediumTest
22     public void testInit(){
23         //startActivity(mStartIntent, null, null);
24         Log.d("MainTest""ActivityUnitTestCase test");
25     }
26 }
27 </main>

 

기본골격은 위와 같이 만들며 테스트하고자 하는 함수를 만들때는 함수명 앞에 test~ 라고

붙여주면 된다그리고 클래스를 만들 때 상속 클래스를 ActivityUnitTestCase<T> 

설정하면 된다. T 부분은 실행할 Activity 클래스를 가리킨다

만든 클래스를 돌려보기 위해 파일을 선택하고 오른쪽 마우스를 클릭한뒤 그림과 같이

Android Junit Test 를 선택한다그러면 testInit() 함수를 실행하고 뒤이어 Main Activity 

실행한다그리고 결과가 아래와 같이 Junit 익스플로어에 나타날것이다

+ Recent posts