磨难尝试使用JUnit测试库 [英] Ordeal trying to use JUnit to test a Library
问题描述
我觉得有围绕如何使用JUnit测试一个Android库项目很大的混乱。结果
我将描述我遇到迄今(对其他程序员试图做同样的缘故)的所有错误,并在年底,我将指定我最后和目前存在的问题。
-
由于图书馆不是一个应用程序,也不会产生在
斌的
目录,只是一个.apk文件
文件的.jar
文件,,你需要一个.apk文件
使用Android的JUnit测试 -
所以,你一定要创建测试一个新的项目,按照这指南结果
注:虽然指南将告诉您使用测试用例
和它的派生类AndroidTestCase
,几个职位似乎指向了测试用例
仅用于JUnit3测试,测试JUnit4没有扩展任何类。结果
我不知道我应该当你想运行,如果你想使用它JUnit4下使用了Android API,测试使用,我还没有达到这一点呢。 -
对于不使用Android API,使用JUnit4样式类测试。如果你只跟随导游,当你运行它的的无效布局......在价值的测试将失败的。这是因为运行配置包含Android在它的的classpath 的。转到JUnit测试运行配置的测试(右键单击测试 - >属性 - >运行/调试设置 - >编辑)和自举类路径中删除条目的Android。结果
修改结果
当你首次运行时间的考验,在Eclipse中,你要选择,如果你选择了Android发射器要使用的发射器的Eclipse JUnit的启动器和Android的JUnit测试启动器,无需取出引导入口。 -
从现在开始,我没有成功。对于使用Android API测试,延长
AndroidTestCase
。这些测试将需要更长的时间来启动,他们需要仿真器上运行,即使你是不是测试的应用程序本身,而是一个工具类,没有界面给用户。 -
要运行Android单元格测试发射查找目标包的APK文件,图书馆的目标项目没有。
-
目标包是在您的测试项目清单,当你遵循#2导向自动插入。该测试项目清单应该是这样的:
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.Common.lib.test
安卓版code =1
机器人:=的versionName1.0> <采用-SDK安卓的minSdkVersion =14/> <仪器仪表
机器人:名字=android.test.InstrumentationTestRunner
机器人:targetPackage =com.Common.lib/> <应用
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME>
<使用库机器人:名字=android.test.runner/>
< /用途>< /清单>
块引用>下面的目标包是我想测试库。一个解决方案是给<一个href=\"http://stackoverflow.com/questions/10172636/unable-to-find-instrumentation-target-package-com-xyz/10173034#10173034\">in这个帖子它是使用测试项目作为应用程序,它有一个APK。所以,现在我从
改变目标包的android:targetPackage =com.Common.lib/&GT;
块引用>到
的android:targetPackage =com.Common.lib.test/&GT;
块引用>但仍然发射查找图书馆APK,即使我增加了图书馆作为测试项目的参考,请参阅以下的logcat(注意CommonLibTest.apk被成功加载):
[2014年2月24日16点17分50秒 - CommonLibTest]霉素
麻烦写输出:已经prepared
[2014年2月24日16点17分51秒 - CommonLibTest] ------------------------------
[2014年2月24日16点17分51秒 - CommonLibTest] Android的推出!
[2014年2月24日16点17分51秒 - CommonLibTest]亚洲开发银行运行正常。
[2014年2月24日16点17分51秒 - CommonLibTest]执行android.test.InstrumentationTestRunner JUnit启动
[2014年2月24日16点17分51秒 - CommonLibTest]自动目标模式:使用现有模拟器模拟器-5554'运行兼容AVD电话86 JB
[2014年2月24日16点17分51秒 - CommonLibTest]上传CommonLibTest.apk到设备'模拟器-5554
[2014年2月24日16时17分52秒 - CommonLibTest]安装CommonLibTest.apk ...
[2014年2月24日16时17分52秒 - CommonLibTest]成功!
[2014年2月24日16时17分52秒 - CommonLib]找不到CommonLib.apk!
[2014年2月24日16时17分52秒 - CommonLibTest]在模拟器-5554发射仪器android.test.InstrumentationTestRunner
[2014年2月24日16时17分53秒 - CommonLibTest]测试运行失败:仪表运行失败,原因是抛出java.lang.ClassNotFoundException
块引用>这是据我得到了。结果
如果愿意相信有人已经测试了图书馆的项目成功地使用JUnit并有解决我的问题。解决方案要完成这个故事,我解决了最后的障碍和测试现在运行。结果
让我们开始的底线:
AndroidTestCase
要求JUnit3测试。注释@Test
失败ClassNotFound的
测试为org.junit.Test
类。在我注释掉所有
@Test
注解测试开始运行。
还有一个准则:测试名称应以preFIX测试为JUnit3启动奇怪的JUnit4注解
@BeforeClass
并没有引发故障。结论:结果
JUnit4测试方式只适用于纯Java单元测试良好。 Android的单元测试使用JUnit3风格,因为JUnit4是向下兼容的,将编译罚款。结果
请注意,这个帖子只是指单元测试,我还没有到我需要的功能测试来测试Android应用程序本身的阶段。I feel there is much confusion around how to use JUnit to test an Android Library project.
I will describe all the errors I encountered so far(for the sake of other programmers trying to do the same) and at the end I will specify my last and current problem.
Since a Library is not an Application, it does not produce an
.apk
file in thebin
directory, just a.jar
file, and you need an.apk
to use Android JUnit testingSo you definitely need to create a new project for the tests, follow this guide
Note: Even though the guide tells you to useTestCase
and its derived classAndroidTestCase
, several posts seem to point thatTestCase
is used only for JUnit3 tests, JUnit4 tests do not extend any class.
I don't know what I am supposed to use when you want to run a test that uses the Android API, if you want to use it under JUnit4, I have not reached that point yet.For tests that do not use the Android API, use JUnit4 style class. If you follow only the guides your test will fail when you run it "Invalid layout of ... at value". This is because the run configuration includes Android in its classpath. Go to the JUnit test run configuration for the test (Right click on the test->Properties->Run/Debug Settings->Edit) and remove Android from classPath boostrap entries.
EDIT
When you run the test for the first time, in Eclipse, you have to choose which launcher to use "Eclipse JUnit Launcher" or "Android JUnit Test Launcher" if you choose the Android launcher, no need to remove the bootstrap entry.From now on I had no success. For tests that use the Android API, extend
AndroidTestCase
. These tests will take longer to launch, they need an emulator running, even if you are not testing the application itself but a utility class with no interface to the user.To run Android UNIT case test the launcher looks for the target package APK file, which a Library target project does NOT have.
The target package was automatically inserted in the manifest of your test project when you followed the guide in #2. The manifest for the test project should look like this:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Common.lib.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.Common.lib" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <uses-library android:name="android.test.runner" /> </application> </manifest>
Here the target package is the Library I am trying to test. A solution is given in this post It is to use the test project as application which has an APK. So now I changed the target package from
android:targetPackage="com.Common.lib" />
to
android:targetPackage="com.Common.lib.test" />
But still the launcher looks for the Library APK, even though I added the Library as reference in the test project, see the logcat below (Note the CommonLibTest.apk is loaded succesfully):
[2014-02-24 16:17:50 - CommonLibTest] Dx trouble writing output: already prepared [2014-02-24 16:17:51 - CommonLibTest] ------------------------------ [2014-02-24 16:17:51 - CommonLibTest] Android Launch! [2014-02-24 16:17:51 - CommonLibTest] adb is running normally. [2014-02-24 16:17:51 - CommonLibTest] Performing android.test.InstrumentationTestRunner JUnit launch [2014-02-24 16:17:51 - CommonLibTest] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Phone-x86-jb' [2014-02-24 16:17:51 - CommonLibTest] Uploading CommonLibTest.apk onto device 'emulator-5554' [2014-02-24 16:17:52 - CommonLibTest] Installing CommonLibTest.apk... [2014-02-24 16:17:52 - CommonLibTest] Success! [2014-02-24 16:17:52 - CommonLib] Could not find CommonLib.apk! [2014-02-24 16:17:52 - CommonLibTest] Launching instrumentation android.test.InstrumentationTestRunner on emulator-5554 [2014-02-24 16:17:53 - CommonLibTest] Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
This is as far as I got.
If want to believe someone already has tested a Library project succesfully with JUnit and has the solution to my problem.解决方案To complete the tale, I solved the last obstacle and the test now runs.
Lets start with the bottom line:
AndroidTestCase
requires JUnit3 tests. The annotation@Test
fails the test withClassNotFound
for theorg.junit.Test
class.Once I commented out all
@Test
annotations the test started to run. One more guideline: test names should start with the prefix "test" for JUnit3Strangely the JUnit4 annotation
@BeforeClass
did not trigger a failure.Conclusion:
JUnit4 test styles is only good for pure Java Unit tests. For Android Unit tests use JUnit3 style, since JUnit4 is backwards compatible it will compile fine.
Please note that this post refers only to Unit testing, I haven't got to the stage where I need to test the Android application itself with functional tests.这篇关于磨难尝试使用JUnit测试库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!