글
Android-Gradle-Robolectric 테스트용 iml 파일 자동 설정하기
Robolectric 테스트 코드를 위해서 매번 iml 을 설정해줘야 하는 번거로움이 있어서
오늘은 작정하고 테스트용 iml 을 자동 설정하도록 했습니다.
1. iml 수정용 Task 생성
task initGradleTest << { def imlFile = project.name + '.iml' def parse = new XmlParser().parse(imlFile) def modulePath = parse.@'external.linked.project.path' // It's Robolectric Default ouputPath def outputTestPath = "file://$modulePath/build/test-classes" def moduleComponent = parse.component.find { it.@name == 'NewModuleRootManager' } def outputTest = moduleComponent.find {it.name() == 'output-test'} if (outputTest != null) { outputTest.@url = outputTestPath } else { moduleComponent.appendNode('output-test', [url : outputTestPath]) } // jdk orderEntry must be last def orderEntry = moduleComponent.orderEntry def jdkOrderEntry = orderEntry.find { it.@type == 'jdk' } moduleComponent.remove(jdkOrderEntry) moduleComponent.append(jdkOrderEntry) // rewrite $project.iml file FileWriter fileWriter = new FileWriter(imlFile) new XmlNodePrinter(new PrintWriter(fileWriter)).print(parse) }
2. Gradle 빌드시 Task 동작하도록 하기
tasks.preBuild.dependsOn initGradleTest
prebuild 는 모든 Task 동작시 최초의 Android Gradle Task 입니다.
3. JUnit 테스트때마다 test-code compile 하도록 하기
|
테스트 코드가 동작할 때 Run 설정에서 Defaults -> JUnit -> Before Launch 설정에 Make 보다 compileTestDebugJava 가 동작하도록 해주십시요.
(Robolectric 에서 생성한 Task 로 1번 설정에서 해주었던 output-test 경로에 맞춰서 test-class 가 compile 됩니다)
이리 하면 이제부터 JUnit 으로 하는 Robolectric 테스트는 별다른 오류 없이 동작하는 것을 확인하실 수 있습니다.
게을러터져서는 조금만 섬세하면 되는 것을 전부 자동화 하려는 욕심에 이런것까지 하게 되었네요.
원래는 idea.module 을 손대서 아예 자동 생성하도록 하려고 했는데
아무래도 그건 무리였나보더군요. 그거 하려다가 Android-Gradle 원소스를 통째로 보고 있는 제 스스로가 느껴지니 이걸 왜 하나 싶어서 이정도만으로 만족하려고 합니다.
위의 코드는
Github : https://github.com/ZeroBrain/Android-Sqlite-Object-Convert
를 통해 공유됩니다.
'개발 이야기' 카테고리의 다른 글
Maven Central 에 라이브러리 올리기 (3) | 2014.08.08 |
---|---|
Android PlayStore Publisher Sample 동작 (0) | 2014.08.02 |
Android-Gradle-Robolectric 테스트용 iml 파일 자동 설정하기 (0) | 2014.07.23 |
Robolectric 에서 SupoortFragment 사용하기 (0) | 2014.07.10 |
Robolectric 에서 Activity 테스트하기 (0) | 2014.07.10 |
Android-Gradle-Robolectric 에서 Sqlite DB 테스트 하기 (0) | 2014.06.30 |