简体   繁体   中英

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 eg 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.

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".

在此处输入图片说明

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

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.

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