일반 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; |
07 |
public class MainTest extends ActivityUnitTestCase<main> { |
09 |
private Intent mStartIntent; |
10 |
private Button mStartButton; |
16 |
protected void setUp() throws Exception { |
18 |
mStartIntent = new Intent(Intent.ACTION_MAIN); |
22 |
public void testInit(){ |
24 |
Log.d( "MainTest" , "ActivityUnitTestCase test" ); |
기본골격은 위와 같이 만들며 테스트하고자 하는 함수를 만들때는 함수명 앞에 test~ 라고
붙여주면 된다. 그리고 클래스를 만들 때 상속 클래스를 ActivityUnitTestCase<T> 로
설정하면 된다. T 부분은 실행할 Activity 클래스를 가리킨다
만든 클래스를 돌려보기 위해 파일을 선택하고 오른쪽 마우스를 클릭한뒤 그림과 같이
Android Junit Test 를 선택한다. 그러면 testInit() 함수를 실행하고 뒤이어 Main Activity 를
실행한다. 그리고 결과가 아래와 같이 Junit 익스플로어에 나타날것이다