考验试图使用JUnit测试库(Ordeal trying to use JUnit to test

2019-10-19 09:40发布

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.

  1. Since a Library is not an Application, it does not produce an .apk file in the bin directory, just a .jar file, and you need an .apk to use Android JUnit testing

  2. So you definitely need to create a new project for the tests, follow this guide
    Note: Even though the guide tells you to use TestCase and its derived class AndroidTestCase, several posts seem to point that TestCase 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.

  3. 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.

  4. 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.

  5. To run Android UNIT case test the launcher looks for the target package APK file, which a Library target project does NOT have.

  6. 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'

这是据我得到了。
如果愿意相信有人已经测试了图书馆项目成功地使用JUnit和有解决我的问题。

Answer 1:

为了完成这个故事,我解决了最后的障碍和测试现在运行。
让我们先从底线:

AndroidTestCase需要JUnit3测试。 注释@Test失败与测试ClassNotFoundorg.junit.Test类。

一旦我注释掉所有@Test注解测试开始运行。 还有一个准则:测试名称应以前缀“测试” JUnit3启动

奇怪的JUnit4注解@BeforeClass没有引发故障。

结论:
JUnit4测试方式只适用于纯Java单元测试良好。 对于Android的单元测试使用JUnit3风格,因为JUnit4是向后兼容的,将编译好的。
请注意,这个帖子只是指单元测试,我还没有到我需要的功能测试来测试Android应用程序本身的阶段。



Answer 2:

一些调查结果:

apk的为“需要”,因为该向导添加项目被测作为项目的依赖(见测试项目属性)。 在测试期间,框架需要上传工程设备不知何故,因此需要一个APK。 由于没有APK会有一个警告消息。 删除此依赖性沉默的警告。 请注意 ,由于没有APK上传没有库上传到设备或者,在测试以后给NoClassDefFound例外。

在我的情况下,与原来的岗位,除去从测试项目的项目被测依赖和手动添加所有需要的jar文件,包括内置和需要,从项目被测解决它。 我没有需要额外的“测试应用程序”取决于项目下测试一些职位elsewere建议。 也许这是一个方便易解决方案,如果一个,否则有很多罐子的手动添加。

双方保持项目的依赖,并手动将导致解决类yeilding在NoClassDefFound异常问题所需的罐子。 我现在为什么不。



文章来源: Ordeal trying to use JUnit to test a Library