【问题标题】:Android Espresso not working with Multidex gives "No tests found"Android Espresso 无法与 Multidex 一起使用会给出“未找到测试”
【发布时间】:2016-01-07 00:45:22
【问题描述】:

我的 Espresso 测试一直在运行,直到我不得不支持 multidex。

我的 build.gradle,我有

minSdkVersion 14
targetSdkVersion 23
multiDexEnabled = true

testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"


androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'

dexOptions {
        jumboMode true
        javaMaxHeapSize "4g"
        incremental true
    }

Test1AuthenticationEspressoTest

@RunWith(AndroidJUnit4.class)
@SmallTest
public class Test1AuthenticationEspressoTest {
    @Rule
    public ActivityTestRule<WelcomeActivity> mActivityRule = new  ActivityTestRule(WelcomeActivity.class);

}

这是我得到的错误

junit.framework.AssertionFailedError: 未找到测试 com.livestrong.tracker.test.Test1AuthenticationEspressoTest

任何帮助将不胜感激。任何人都使用 multidex 的 espresso 吗?

【问题讨论】:

  • 所以如果你删除'multiDexEnabled = true'你就可以测试了,对吧?用自己的 Gradle 文件考虑项目中的新模块
  • 当我将测试运行器更改为 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 时它开始工作了
  • 我遇到了完全相同的问题,但您所做的更改似乎都没有解决它。这吹。

标签: android android-espresso android-multidex


【解决方案1】:

我遇到了同样的问题,事实证明您需要构建一个自定义运行程序,以启用 MultiDex 并从 AndroidJUnitRunner 扩展。然后,您需要将该运行器设置为 build.gradle 中的 testInstrumentationRunner,并在运行配置中设置为运行器。无需修改测试类(保留@RunWith(AndroidJunit4.class))。

以下是操作的分步说明:

  1. 为您的自定义跑步者创建一个类:

    package com.bla.bla.bla;  // your package
    
    import android.os.Bundle;
    import android.support.multidex.MultiDex;
    import android.support.test.runner.AndroidJUnitRunner;
    
    public class CustomTestRunner extends AndroidJUnitRunner
    {
        @Override
        public void onCreate(Bundle arguments)
        {
            MultiDex.install(getTargetContext());
            super.onCreate(arguments);
        }
    }
    
  2. 在您的 build.gradle 中,将运行器设置为您的自定义运行器:

    android {
        // ...
        defaultConfig {
            // ...
            testInstrumentationRunner "com.bla.bla.bla.CustomTestRunner"
        }
    }
    
  3. 在您的运行配置中,make sure the instrumentation runner is also set to the same runner.注意: 在 Android Studio 3.x 上可能不需要此步骤,一些以前的版本也可能不需要。此选项已不存在。

使用上述方法,我能够在我们启用了多 dex 的应用上运行 Espresso 测试。

我应该注意到网上关于这个主题的许多其他帖子,建议将你的跑步者设置为com.android.test.runner.MultiDexTestRunner,并在你的 build.gradle 中排除com.android.support:multidex-instrumentation:1.0.1 中的一些依赖项。该解决方案似乎不再是这种情况,并且从 gradle 1.5.0 开始不起作用。如果您设置了任何这些东西,那么它将阻止上述工作。有关更多信息,请参阅this stack overflow post 中的 cmets。

【讨论】:

  • 您的回答很有希望,但我仍然遇到问题:错误:任务':app:transformClassesWithMultidexlistForDebugAndroidTest'的执行失败。 > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command'/Library/Java/JavaVirtualMachines/jdk1.7.0_75 .jdk/Contents/Home/bin/java'' 以非零退出值 1 结束
  • 这个答案应该被接受。
  • 这基本上对我有用,除了一件事:当我以这种方式运行我的任何引用从“libs”目录中提取的 jar 中的类的代码时(像这样加载 compile fileTree(dir: 'libs', include: '*.jar'))将因NoClassDefFoundError 而失败。其他一切正常。
  • 这不起作用。此外,您的代码正是 MultiDexTestRunner 中的代码,因此如果此代码确实有效,则没有必要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-02
  • 2022-10-17
  • 2023-03-28
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
相关资源
最近更新 更多