user2261015
user2261015

Reputation: 458

How can I run Android unit tests while not simultaneously starting the application?

When I run unit tests by Android Studio, the testrunner executes the test methods of the test case (which extends AndroidTestCase) as expected. However, the actual application (which extends Application) is started, too. Naturally this interferes with the unit tests, since both use the same classes and hardware resources. I have no idea why Android Studio (or any other module) starts the application?

I'm running the unit test e.g. by using the context menu of Android Studio (v 1.3.2), selecting the test case file/class and the selecting 'run' (ctrl+shift+f10), or using the run menu from the menu bar. The application and/or the unit tests run on a real device (HTC Nexus 9 Android 5.1.1 API 22) connected via USB. I can't use the emulator, since it does not provide bluetooth.

Upvotes: 1

Views: 1375

Answers (2)

alfoks
alfoks

Reputation: 4624

In the version of Android Studio you are using, there are 2 types of tests,

  • "Unit Tests" and
  • "Android Instrumentation Tests"

which can be selected in the tab called "Build variants".

enter image description here

Android Instrumentation Tests launch the Application, while Unit Tests not. You can search the net of how to configure each and when to use the one over the other.

Some links to get you started:

http://tools.android.com/tech-docs/unit-testing-support http://www.vogella.com/tutorials/AndroidTesting/article.html

Upvotes: 1

Nabil Souk
Nabil Souk

Reputation: 164

There are usually 2 types of tests in Android Studio, the instrumentation test and the unit test. Running an instrumentation test will launch your application for sure since it executes and Android component running in a lifecycle determined by the system. A unit test will not launch your application, because its execute methods not related to any Android API. So in your case it depends on what are you testing.

Upvotes: 0

Related Questions