Questions tagged [android-testing]

The Android testing framework

The Android testing framework, an integral part of the development environment, provides an architecture and powerful tools that help you test every aspect of your application at every level from unit to framework.

The testing framework has these key features:

  • Android test suites are based on JUnit. You can use plain JUnit to test a class that doesn't call the Android API, or Android's JUnit extensions to test Android components. If you're new to Android testing, you can start with general-purpose test case classes such as AndroidTestCase and then go on to use more sophisticated classes.
  • The Android JUnit extensions provide component-specific test case classes. These classes provide helper methods for creating mock objects and methods that help you control the lifecycle of a component.
  • Test suites are contained in test packages that are similar to main application packages, so you don't need to learn a new set of tools or techniques for designing and building tests.
  • The SDK tools for building and tests are available in Eclipse with ADT, and also in command-line form for use with other IDEs. These tools get information from the project of the application under test and use this information to automatically create the build files, manifest file, and directory structure for the test package.
  • The SDK also provides monkeyrunner, an API for testing devices with Python programs, and UI/Application Exerciser Monkey, a command-line tool for stress-testing UIs by sending pseudo-random events to a device.
2161 questions
51
votes
3 answers

Testing Snackbar show with Espresso

Is there a way to test using Espresso that the snackbar shows up with the right text? I have a simple call to create a snackbar Snackbar.make(mView, "My text", Snackbar.LENGTH_LONG).show(); I have tried this without luck onView(withText("My…
50
votes
7 answers

launchFragmentInContainer unable to resolve Activity in Android

While writing a simple test which uses launchFragmentInContainer, I get the following error message: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]…
Bugdr0id
  • 2,962
  • 6
  • 35
  • 59
50
votes
8 answers

Robolectric: Resources$NotFoundException: String resource ID with Android Gradle Plugin 3

Android Studio 3.0 Beta2 classpath 'com.android.tools.build:gradle:3.0.0-beta3' testCompile 'org.robolectric:robolectric:3.4.2' Test class that I am using that fails to run: @Config(constants = BuildConfig.class, sdk =…
ant2009
  • 27,094
  • 154
  • 411
  • 609
49
votes
8 answers

Espresso: return boolean if view exists

I am trying to check to see if a view is displayed with Espresso. Here is some pseudo code to show what I am trying: if (!Espresso.onView(withId(R.id.someID)).check(doesNotExist()){ // then do something } else { // do nothing, or what have…
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
44
votes
4 answers

What's the difference between getTargetContext() and getContext (on InstrumentationRegistry)?

I'm using the new Android Testing Support Library (com.android.support.test:runner:0.2) to run Instrumentation Tests (a.k.a Device or Emulator Tests). I annotate my test class with @RunWith(AndroidJUnit4.class) and use Android Studio to run…
Zsolt Safrany
  • 13,290
  • 6
  • 50
  • 62
43
votes
4 answers

Unit testing a Kotlin coroutine with delay

I'm trying to unit test a Kotlin coroutine that uses delay(). For the unit test I don't care about the delay(), it's just slowing the test down. I'd like to run the test in some way that doesn't actually delay when delay() is called. I tried running…
Erik Browne
  • 1,393
  • 1
  • 14
  • 19
37
votes
3 answers

How do I allow android app alpha/beta testers to download the app for free?

I have an application that I want to release for $x amount to the public, however, I want to allow the Google Developer Console Alpha/Beta APK to be downloaded for free. I want the testers to be able to download it for free? How do I do that? Thanks…
LuckyMe
  • 3,820
  • 2
  • 27
  • 35
36
votes
3 answers

How to turn on console output in Android Unit tests?

Is there any way to turn on the test logging in the console output? I know that we can look at the test results generated in a HTML file and check standard output there, but I find it a little bit inconvinient. I know that there is a way to do this…
scana
  • 2,785
  • 2
  • 25
  • 49
36
votes
4 answers

How can I run a single instrumentation test with Gradle Android

I'm trying to run the tests with this line... but this launches all tests: ./gradlew -DconnectedAndroidTest.single=LandingActivityTests connectedAndroidTest How can I launch a single test?
Javier Manzano
  • 4,761
  • 16
  • 56
  • 86
36
votes
3 answers

Espresso - Click by text in List view

I am trying to click on a text in a list view using Espresso. I know they have this guide, but I can't see how to make this work by looking for text. This is what I have…
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
35
votes
4 answers

Espresso testing disable animation

@Test public void test3_PaySuccessful(){ init(); ViewInteraction amountEditText = onView( allOf(withId(R.id.et_amount), isDisplayed())); amountEditText.perform(replaceText("SGD 0.010"), closeSoftKeyboard()); …
kggoh
  • 742
  • 1
  • 10
  • 24
35
votes
4 answers

Espresso intent test failing

I'm learning android instrumentation testing with espresso. I have an app which has a drawer menu and there is a menu called About. I was testing click on that menu item and contents of activity. testfunction: @Test public void…
35
votes
7 answers

Mocking library/framework that works best in Android?

I'm developing Android application using third party libraries (Twitter4j). I want to be able mock those objects (also objects created by me) in JUnit and functional tests. Do you have any good experiences using some mocking libraries and you can…
pixel
  • 24,905
  • 36
  • 149
  • 251
34
votes
3 answers

MutableLiveData is null in JUnitTest

I want to write a unit test. Therefore I need MutableLiveData. I started with a very basic test for setup but I cannot instantiate a MutableLiveData object. I is always null when I run the test. Do I have to mock anything? Any suggestions?…
Kewitschka
  • 1,445
  • 1
  • 21
  • 35
33
votes
3 answers

ApplicationTestCase deprecated in API level 24

I created a default empty project on Android Studio 2.1.2 with API 24. In the sample project, Google offers a depreciated class ApplicationTestCase: This class was deprecated in API level 24. Use ActivityTestRule instead. New tests should be…
lopez.mikhael
  • 9,943
  • 19
  • 67
  • 110