【问题标题】:Unable to run espresso tests for android无法为 android 运行 espresso 测试
【发布时间】:2023-03-28 21:22:01
【问题描述】:

我在为 android 设置/运行 espresso 测试时遇到了一些问题。 我的 TestClass 如下所示:-

import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click;
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withText;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.sample.rasmus.MainActivity;

public class BasicTest extends ActivityInstrumentationTestCase2<MainActivity> {

public BasicTest(String name) {
    super(MainActivity.class);
    Log.v("amtesting","2");
}
 @Override
  public void setUp() throws Exception {
      Log.v("amtesting","5");
    super.setUp();
    Log.v("amtesting","4");
    // Espresso will not launch our activity for us, we must launch it via getActivity().
    getActivity();
  }

public void testSimpleClickAndCheckText(){
    Log.v("amtesting","1");
    onView(withId(com.sample.rasmus.R.id.thebutton)).perform(click());
    onView(withId(com.sample.rasmus.R.id.helloworld)).check(matches(withText("awesome")));
}

protected void tearDown() throws Exception {
    Log.v("amtesting","3");
    super.tearDown();

}

  }

AndroidManifest.xml 看起来像这样:-

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

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

<instrumentation
    android:name="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    android:targetPackage="com.sample.rasmus" />

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

 </manifest>

并且运行配置已更新为使用 Google InGoogleInstrumentationTestRunner 作为 InstrumentationRunner。

但是,当我运行测试时,它会在控制台上为我提供以下信息:-

  • 在设备 emulator-5554 上启动仪器 android.test.InstrumentationTestRunner
  • 向 Eclipse 发送测试信息
  • 测试完成

没有提到运行测试并且测试不运行。我会在这里遗漏什么?

【问题讨论】:

    标签: java android junit junit3 android-espresso


    【解决方案1】:

    好的,这就是我最终解决它的方法。我将测试类的构造函数更改为以下:-

    public BasicTest() {
      super(MainActivity.class);
     }
    

    它开始工作了。有点奇怪,这就是让我忙了一整天的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-02
      • 2016-11-11
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多