Libraryプロジェクトのテスト
Libraryテストでハマったので覚書
通常のLibraryプロジェクトからテストプロジェクトを作成して実行するとエラーが発生する
Could not find XXX.apk!
テストプロジェクトではapkファイルが作成されないので見つかんないってエラーが起きる
調べるとdeveloperサイトにはライブラリのテスト方法が書かれていた
http://developer.android.com/tools/projects/index.html#testing
Testing a Library Project There are two recommended ways of setting up testing on code and resources in a library project: You can set up a test project that instruments an application project that depends on the library project. You can then add tests to the project for library-specific features. You can set up a standard application project that depends on the library and put the instrumentation in that project. This lets you create a self-contained project that contains both the tests/instrumentations and the code to test.
要約すると方法は2つあるんでどっちか選んで
# ライブラリ使ってるアプリケーションのテストプロジェクトを作成するとOK
# 普通のプロジェクト作成してtestケースとinstrumentations入れといて
前者はアプリのテストで一緒にライブラリもテストしてくれってことらしいが、そんなのライブラリプロジェクトの意味がいない
r‐z´ / `ヽL_ |/|/ / /| | ト、 \ \ヽト \ V/|/| /‐|<|_,l |_>‐ヽ| ト、|| ト、| イ|イ |/y'´(.j`´|\|´ヒ)`'v|ノN`| | あんた バカァ? || | |∧ ・ /k) | | || | |ヽヘ、 「  ̄`| イ-'| | | 別アプリでライブラリテストなんてなんて信じられない || | |:|:|:|:l\. |_ ノ.///:::| | | |{< ̄ ̄\´\ ̄ 7/ ̄〉´ ̄\l.| どう考えても底辺プログラマの発想だわ ノ/ー(⌒ヽ:.:.:\ \/ .イ:.:.:.:.://、| // ヽ::::ハ:.:.:.:.:| >Xく |:.:.:.:.|/ \ /〈 / ̄}::::::\:.:.ト、/∧ .>|:.:.:.:.}∠ \ { :}/´l .::|/::::: ̄ ̄`)ハ〉|:.:.:./:::::< > |Kl |..:::::ヘ / ̄| ̄/ ./:.:./\::::::::..../\
なので後者の手法でやってみるが、これが色々と問題がありすぎて困る
devサイトももう少しドキュメントを詳しくしたほうがいいだろ
ライブラリプロジェクトのテストプロジェクトの作成方法
手順
- 普通にライブラリプロジェクトを作成する
- テストプロジェクトを普通のアプリケーションプロジェクトとして作成する
- パッケージはライブラリと同一にする
- テストプロジェクトにライブラリを追加
- Manifestにinstrumentationとuses-library追加
- TestCase作成
普通にライブラリプロジェクトを作成する
MyLibというライブラリを作成して、Utilクラスを作成
package com.example.lib; public class Util { public static String echo(String message){ return message; } }
Manifestにinstrumentationとuses-library追加
android:targetPackageの値はライブラリプロジェクトのパッケージ=テストプロジェクトのパッケージとなる
TestCase作成
普通にテストケースを作成する
package com.example.lib; import junit.framework.TestCase; public class UtilTest extends TestCase { public void testEcho() { String send = "Hello"; String res = Util.echo(send); assertEquals(res, send); } }