简体   繁体   中英

Testing Android Components With Espresso

I have a number of custom Android components and wish to test them using Espresso. As an Espresso test runs against an Activity I added a simple Activity class to the androidTest directory which programatically creates a view with my component in it ready for testing.

For example if I'm testing a MyView component then my Espresso test class might look something like this:

public class MyViewTest extends ActivityInstrumentationTestCase2<MyViewTestActivity>
{
  private MyViewTestActivity activity;

  public MyViewTest()
  {
    super(MyViewTestActivity.class);
  }

  @Override
  protected void setUp() throws Exception
  {
    super.setUp();
    setActivityInitialTouchMode(false);
    // Launches the activity
    activity = getActivity();
  }

  // Ensure that expected items are present
  public void testLayout()
  {
    onView(withId(activity.view.getId())).check(matches(isDisplayed()));
  }
}

with a simple MyViewTestActivity as follows:

public class MyViewTestActivity extends Activity
{
  private static final Random RANDOM = new Random();

  public LinearLayout layout;
  public MyView view;

  @Override
  public void onCreate(final Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    layout = new LinearLayout(this);
    layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                                         LinearLayout.LayoutParams.MATCH_PARENT));

    view = new MyView(this);
    view.setId(RANDOM.nextInt());
    view.setItem("Test text");

    layout.addView(view);
    setContentView(layout);
  }
}

My problem is I appear to need to add the test Activity class, in this case MyViewTestActivity , to the main AndroidManifest.xml to make this work, otherwise I receive an Unable to resolve activity for: Intent... error when attempting to run the test. However I now have test activities in the main manifest, which seems like a bad thing to do.

How can I set up test-specific activities which are included in the test manifest but not the main one?

I'm using the gradle-based build system for Android.

Yes, you have to add MyViewTestActivity to the main AndroidManifest.xml.

If you look at the ActivityInstrumentationTestCase2 source code, you will see that getActivity() looks for the tested Activity in the target (ie, the app under test) context.

Here is the relevant part of the source code.

@Override
public T getActivity() {
    // ...
    final String targetPackage = getInstrumentation().getTargetContext().getPackageName();
    // ...
    a = launchActivity(targetPackage, mActivityClass, null);
    // ...
    setActivity(a);
    // ...
}

What I do in my projects is that I create a generic TestingActivity, put it in a .test package in the target app, and use it for all GUI-component testing. It is not ideal, but I never had any problem with this approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM