简体   繁体   English

Android / Gradle espresso测试没有启动活动

[英]Android/Gradle espresso test not starting activity

I'm having difficulty convincing the new Android build system to run tests. 我难以说服新的Android构建系统运行测试。 When running the test it gives the Unable to resolve activity for: Intent error which has been discussed in other questions but there is nothing in there which have fixed my problem. 在运行测试时,它会让Unable to resolve activity for: Intent错误已在其他问题中讨论过,但没有任何内容可以修复我的问题。

I've stripped it down so that my test package does not rely on my main package ( com.wealdtech.app ) at all but still have the problem starting activity. 我已将其剥离,以便我的测试包完全不依赖于我的主程序包( com.wealdtech.app ),但仍然存在启动活动的问题。

My test activity: 我的测试活动:

package com.wealdtech.test;

import android.app.Activity;
import android.os.Bundle;

public class TileLayoutTestActivity extends Activity
{
  @Override
  public void onCreate(final Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
  }
}

And my test class: 我的测试班:

package com.wealdtech.test;

import android.test.ActivityInstrumentationTestCase2;

public class TileLayoutTest extends ActivityInstrumentationTestCase2<TileLayoutTestActivity>
{
  public TileLayoutTest()
  {
    super(TileLayoutTestActivity.class);
  }

  @Override
  protected void setUp() throws Exception
  {
    super.setUp();
    setActivityInitialTouchMode(false);
  }

  public void testNull()
  {
    final TileLayoutTestActivity activity = getActivity();
    activity.finish();
  }

Relevant parts of build.gradle: build.gradle的相关部分:

apply plugin: 'android-library'

android {
  compileSdkVersion 19
  buildToolsVersion "19.0.3"

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
  }

  defaultConfig {
    minSdkVersion 11
    targetSdkVersion 19

    testPackageName "com.wealdtech.test"
    testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
  }
}

The full stack trace I obtain is: 我获得的完整堆栈跟踪是:

java.lang.RuntimeException: Could not launch activity
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation.startActivitySync(GoogleInstrumentation.java:286)
at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119)
at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:97)
at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:104)
at com.wealdtech.test.TileLayoutTest.testNull(TileLayoutTest.java:21)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.wealdtech.test/.TileLayoutTestActivity }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:379)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation.access$101(GoogleInstrumentation.java:52)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation$2.call(GoogleInstrumentation.java:268)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation$2.call(GoogleInstrumentation.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

I haven't included my AndroidManifest.xml because everything I read suggests that I do not need to add an intent for TileLayoutTestActivity , however I have tried to do this anyway and ended up with the same result. 我没有包含我的AndroidManifest.xml因为我读到的所有内容都表明我不需要为TileLayoutTestActivity添加一个意图,但是无论如何我都试图这样做,结果却得到了相同的结果。

I have also tried changing the Gradle plugin from android-library to android in case that was causing the problem, but again the same result. 我也尝试将Gradle插件从android-library更改为android ,以防引起问题,但同样的结果。

I can't see any documentation regarding prerequisites for Espresso testing, or testing with the Gradle build system, which I haven't already covered. 我看不到有关Espresso测试的先决条件或使用Gradle构建系统进行测试的任何文档,我还没有介绍过。 Any ideas as to which I cannot start the activity as part of the test? 关于我不能在测试中开始活动的任何想法?

For a project that uses the android-library plugin, it's semi-accurate to say that the AndroidManifest.xml isn't really used. 对于使用android-library插件的项目,可以说半实时没有使用AndroidManifest.xml In fact all a library project's manifest needs to compile is this: 事实上,所有库项目的清单需要编译是这样的:

<manifest package="com.package.yours"/>

Any permissions or intents you try to put in it will be ignored when the your AAR file is created. 创建AAR文件时,将忽略您尝试放入的任何权限或意图。 It being a library, and as near as I can tell, nothing in the library project's manifest makes it into the AAR (or JAR if you're making one of those too). 它是一个图书馆,就像我所知道的那样,图书馆项目的清单中的任何内容都没有进入AAR(如果您也正在制作其中一个,则为JAR)。

But! 但! That is the manifest that's going to be used when you build a test project that gets pushed to a device. 在构建推送到设备的测试项目时将使用的清单。 You can literally dump gibberish in src/androidTest/AndroidManifest.xml and gradle won't care, but you have to add your test activity to src/main/AndroidManifest.xml or else ./gradlew connectedCheck is going to throw runtime exceptions. 你可以直接在src/androidTest/AndroidManifest.xml转储乱码,gradle也不在乎,但你必须将测试活动添加到src/main/AndroidManifest.xml ,否则./gradlew connectedCheck将抛出运行时异常。

My project looks like this (it really does, I only changed the names): 我的项目看起来像这样(它确实如此,我只更改了名称):

src/
  androidTest/
    java/
      com.package.mine/
        TestActivity.java
        VariousTests.java
  main/
    java/
      com.package.mine/
        FancyLibrary.java
    AndroidManifest.xml

And here is my AndroidManifest.xml : 这是我的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.uie.uieanalytics">

    <uses-permission android:name="android.permission.PERM_I_NEED"/>

    <application>
        <activity android:name=".TestActivity" />
    </application>
</manifest>

Otherwise, I'm using the same test runner as you, and my build.gradle is similar enough. 否则,我和你一样使用相同的测试运行器,我的build.gradle就足够了。

Reference for others who can come to this post as I came, so they don't lose the time. 参考其他可以在我来的时候来这个帖子,所以他们不会浪费时间。

  1. change from legacy, abandoned, ugly ActivityInstrumentationTestCase2 to annotations which is supported by AndroidStudio, Gradle and Espresso 2. This will be developed by Google further. 从传统的,遗弃的,丑陋的ActivityInstrumentationTestCase2变为AndroidStudio,Gradle和Espresso 2支持的注释 。这将由Google进一步开发。

  2. forget about that ActivityInstrumentationTestCase2 forever! 永远忘记ActivityInstrumentationTestCase2吧!

  3. start using @RunWith , @LargeTest , @Test , @Rule ... 开始使用@RunWith,@LargeTest,@Test,@rule ...

For instrumentation tests Android builds two APK's - one with the app and one with the tests. 对于仪器测试,Android构建了两个APK - 一个用于app,另一个用于测试。 If you put activity to androidTest flavour, then it belongs to the test APK. 如果你把活动放到androidTest风格,那么它属于测试APK。 If you start an activity using instrumentation (either directly or by using ActivityTestRule ) later on, then Android searches for it in your app APK and fails, because there is no such activity in the app APK. 如果您稍后使用工具(直接或使用ActivityTestRule )启动活动,则Android会在您的应用APK中搜索它并失败,因为应用APK中没有此类活动。

To solve the problem you can define a test activity (class and manifest) in the debug flavour of your app. 要解决此问题,您可以在应用程序的debug风格中定义测试活动(类和清单)。 Then it will be packed with your app APK and tests will work just fine. 然后它将与你的应用程序APK打包,测试将工作得很好。

Update: or - as Austyn Mahoney suggested - you should use InstrumentationRegistry.getInstrumentation().getTargetContex‌​t() to access the app context instead of instrumentation one. 更新:或 - 正如Austyn Mahoney建议的那样 - 你应该使用InstrumentationRegistry.getInstrumentation().getTargetContex‌​t()来访问应用程序上下文而不是检测上下文。

Kindly change activity name inside Rule that u can able to run 请更改您能够运行的规则内的活动名称

ActivityTestRule mActivityRule = new ActivityTestRule<>( change activity name) ActivityTestRule mActivityRule = new ActivityTestRule <>(更改活动名称)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Espresso 测试错误活动 - Android Espresso test on wrong activity Android Espresso:在第二活动上运行测试用例 - Android Espresso: Run test cases on Second activity Android-Espresso:为每个测试重新创建活动 - Android - Espresso : Activity getting recreated for each test Android:如何使用Espresso测试活动过渡动画? - Android: How to test Activity transition animation with Espresso? Espresso未启动活动在参数化测试中进行第二次迭代 - Espresso not starting Activity the same for second iteration in parameterised test 为什么没有在 Android 仪器测试(活动测试)中使用浓缩咖啡启动活动? - Why is activity not launching in Android Instrumentation test (Activity Test) with espresso? 使用浓缩咖啡测试拨号活动 - Test dial activity with espresso 无法在Android Espresso测试中获得测试中的活动 - Can't get the activity under test in an Android Espresso test 开始第一个Android Studio活动时出现Gradle错误 - Gradle Errors on starting first Android Studio Activity 升级到Android Gradle 2.2.0插件后,使用Dagger 2进行Espresso测试中的ClassCastException - ClassCastException in Espresso test with Dagger 2 after upgrading to Android Gradle 2.2.0 plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM