절차
- File > New Module > Test Module > 이름 결정 > Finish
- Generate(Alt+insert) > Test Method
- test method 를 작성한다.
- Run > Run/Debug Configurations
Error
만약 support library 를 사용하는 경우라면 아래와 같은 error 가 발생할 수 있다.java: cannot access android.support.v7.app.ActionBarActivity이때는 Module dependency 를 추가하자.
class file for android.support.v7.app.ActionBarActivity not found
File > Project Structure > Project Settings > Modules > Tests > + sign > Module Dependency > android-support-v7-appcompat
혹시 + 버튼을 누르고 마우스로 선택이 안될 수 있다. 이 때는 그냥 화살표키를 이용하면 선택할 수 있다.
library 만 추가해서 동작해 봤지만 아래와 같은 Error 가 발생했다.
Error
cannot access android.support.v7.app.ActionBarActivityclass file for android.support.v7.app.ActionBarActivity not found
Test Result
Test 가 성공적으로 끝나면 아래와 같은 화면을 볼 수 있다.Samples
sdk 에서 test sample 들을 찾아볼 수 있다.
<Android_sdk>\sources\android-17\com\android\bandwidthtest\BandwidthTest.java
그밖에
Test Classes & sources
- http://developer.android.com/tools/testing/activity_testing.html
- <Android_sdk>\sources\android-17\android\test
InstrumentationTestRunner 사용법
- http://developer.android.com/tools/testing/testing_android.html#TestAPI
- http://developer.android.com/tools/testing/index.html
- 번역 : http://blog.naver.com/huewu/110089444708
AsynTask class test
기본적으로 AsyncTask 는 UI Thread 에서 동작하는데, JUnit 은 seperate thread 를 이용한다. 그래서 runTestOnUiThread() 를 이용해서 AsyncTask 를 실행 해 줘야 한다.
MockCursor 사용예제
- <Android_sdk>\samples\android-19\legacy\NotePad\tests\src\com\example\android\notepad\NotePadProviderTest.java
- Content Provider Testing | Android Developers
- MockCursor | Android Developers
public class LettersProviderTest extends ProviderTestCase2{ // Contains a reference to the mocked content resolver for the provider under test. private MockContentResolver mMockResolver; // Contains an SQLite database, used as test data private SQLiteDatabase mDb; public LettersProviderTest() { super(ShLettersProvider.class, ShLetterContract.AUTHORITY); } @Override protected void setUp() throws Exception { super.setUp(); // Gets the resolver for this test. mMockResolver = getMockContentResolver(); /* * Gets a handle to the database underlying the provider. Gets the provider instance * created in super.setUp(), gets the DatabaseOpenHelper for the provider, and gets * a database object from the helper. */ mDb = getProvider().getOpenHelperForTest().getWritableDatabase(); } /* * Tests inserts into the data model. */ public void testInserts() { ShareholderLetter letter = new ShareholderLetter("2012", "2012ltr.pdf", new ShareholderLetterPageParserInfoHathaway().getFeedHost()); // Insert subtest 1. // Inserts a row using the new note instance. // No assertion will be done. The insert() method either works or throws an Exception Uri rowUri = mMockResolver.insert( ShLetterContract.Letter.CONTENT_URI, // the main table URI letter.getContentValues() // the map of values to insert as a new record ); Cursor cursor = mMockResolver.query( ShLetterContract.Letter.CONTENT_URI, // the main table URI null, // no projection, return all the columns null, // no selection criteria, return all the rows in the model null, // no selection arguments null // default sort order ); assertEquals(1, cursor.getCount()); assertTrue(cursor.moveToFirst()); int nameIndex = cursor.getColumnIndex(ShLetterContract.Letter.COLUMN_NAME_NAME); int yearIndex = cursor.getColumnIndex(ShLetterContract.Letter.COLUMN_NAME_YEAR); assertEquals(letter.getName(), cursor.getString(nameIndex)); assertEquals(letter.getYear(), cursor.getString(yearIndex)); } @Override public void tearDown() throws Exception { super.tearDown(); } }
References
- Dino Esposito. (2013) Creating Unit Tests - IntelliJ IDEA - Confluence. Retrieved October 09, 2013, from http://confluence.jetbrains.com/display/IntelliJIDEA/Creating+Unit+Tests
댓글 없음:
댓글 쓰기