【问题标题】:Using custom InstrumentationTestRunner in Eclipse causes Error在 Eclipse 中使用自定义 InstrumentationTestRunner 会导致错误
【发布时间】:2012-10-23 02:24:27
【问题描述】:

我在 Eclipse 中有一个单独的测试项目,它已经在命令行和 Eclipse 中成功运行了一段时间。在使用 Jenkins 运行我的测试时,我遇到了标准 InstrumentationTestRunner 不以 Jenkins 支持的 xml 格式输出的问题。我在互联网上阅读过使用自定义 InstrumentationTestRunner。这在使用 ADB 的命令行中有效,但在作为 Android 测试用例运行时在 Eclipse 中失败。

我已经下载了一个自定义仪器测试运行程序 (com.neenbedankt.android.test) 并将其添加到 AndroidManifest 中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testedapplication.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <instrumentation
        android:name="com.neenbedankt.android.test.InstrumentationTestRunner"
        android:targetPackage="com.testedapplication" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>    
</manifest>

这是我在 Eclipse 中遇到的错误:

[测试项目] 未正确配置以运行测试: 无法在其 AndroidManifest.xml 中找到检测 android.test.InstrumentationTestRunner 的 targetPackage 属性!

你可以看到我已经在那里设置了targetPackage,所以我不确定我还能做什么?

【问题讨论】:

    标签: android unit-testing


    【解决方案1】:

    在您的 AndroidManifest.xml 中添加这两个工具。

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.example.myapp" />
    
    <instrumentation
        android:name=".MyRunner"
        android:targetPackage="com.example.myapp" />
    

    然后转到 Package explorer --> $(Your Test Prject$) --> Run As --> Run configurations --> Android JUnit Test --> $(Your Test Project) --> Instrumentation Runner 并选择你的跑步者在那里。

    【讨论】:

    • 我又试了一次,好像不需要android.test.InstrumentationTestRunner。您所要做的就是更改 Instrumentation Runner 设置。
    • 伙计,我喜欢 stackoverflow。
    【解决方案2】:

    要让 eclipse 在以/android junit 运行时默认选择自定义运行器 - 只需在清单文件中切换顺序即可。确保你是第一位的

    <instrumentation
    android:name="*.Custom.TestRunner"
    android:targetPackage="com.*" />
    
    <instrumentation
    android:name="*.InstrumentationTestRunner"
    android:targetPackage="com.*" />
    

    【讨论】:

      【解决方案3】:

      由于我看不到您的整个项目设置,所以我会尝试以下几个检查清单。

      1. 您在 Eclipse 中有单独的测试项目吗?上述检测块是在源项目的清单文件中还是在测试项目的清单文件中?

      2. 您是否包含了 块?

      3. 您是否将测试项目的检测块放在 块之外,将 块放在测试项目的应用程序块中?

      4. 您是否尝试通过“新建项目 -> Android 测试项目”在 Eclipse 中创建一个测试项目,并在添加源代码后更改检测类?如果您不更改检测类,它会起作用吗?

      5. 您是否通过“运行方式 -> Android JUnit 测试”在 Eclipse 中运行测试?

      如果您已经完成了所有这些,我认为粘贴整个 AndroidManifest 文件将有助于澄清一点,以及您是否有 2 个单独的项目。

      【讨论】:

      • 我已经编辑了我原来的 cmets 来回答你的问题。在我切换到自定义测试运行器之前,我的测试是在命令行和 Eclipse 中运行的。在原始问题中查看我的新编辑。谢谢。
      【解决方案4】:

      到目前为止,我已经能够通过清单文件中描述的 android.test.InstrumentationRunner 运行程序和客户测试运行程序来使其工作。我似乎从 Eclipse 运行将使用 android.test.InstrumentationRunner 并且从命令行运行将使用自定义测试运行器(如果它在 ant 脚本中设置)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-07
        相关资源
        最近更新 更多