How to run android testing in emulator using IntelliJ?

I am trying to run unit test tests for the sample code that comes with the android SDK. Some of the tests run as standard unit tests, no problem. Other tests, such as com.example.android.apis.view.Focus2AndroidTest, use Android classes and therefore must run in the emulator, which is the problem.

I can run applications in my emulator. However, when I run the tests, my emulator starts up and then just sits there. The test never starts. All that I see in the output window in IntelliJ,

Waiting for device. /Users/rfzabick/android-sdk-mac_x86/tools/emulator -avd MyAvd0 -netspeed full -netdelay none Device connected: emulator-5554 

What am I doing wrong?

EDIT: After consulting @CrazyCoder, I switched to android 4.0.3 (API 15). Here's what I got: Testing started at 16:34 ...

 Waiting for device. /Users/rfzabick/android-sdk-mac_x86/tools/emulator -avd android4.0.3--api15 -netspeed full -netdelay none -wipe-data -no-boot-anim Device connected: emulator-5554 Device is online: emulator-5554 Target device: emulator-5554 (android4.0.3--api15) Uploading file local path: /Users/rfzabick/IdeaProjects/ApiDemos/out/production/Tests/Tests.apk remote path: /data/local/tmp/com.example.android.apis.tests Installing com.example.android.apis.tests DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests" Device is not ready. Waiting for 20 sec. DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests" Device is not ready. Waiting for 20 sec. DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests" Device disconnected: emulator-5554 pkg: /data/local/tmp/com.example.android.apis.tests Running tests Test running startedTest running failed: com.android.ddmlib.AdbCommandRejectedException: device not found Empty test suite. 

The only thing I see in logcat is

 01-21 16:36:22.047: WARN/ActivityManager(91): No content provider found for permission revoke: file:///data/local/tmp/com.example.android.apis.tests 
0
source share
3 answers

Can I run regular applications on an emulator or on a USB device? Try creating a new emulator device and see if it helps.

I tried it with IDEA 11.0.1, 4.0.3 Android platform on Windows and it works fine:

 Waiting for device. Target device: emulator-5554 (ICS) Uploading file local path: D:\dev\android-sdk-windows\samples\android-15\ApiDemos\out\production\Tests\Tests.apk remote path: /data/local/tmp/com.example.android.apis.tests Installing com.example.android.apis.tests DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests" pkg: /data/local/tmp/com.example.android.apis.tests Success Running tests Test running startedFinish 

You can also try restarting adb using adb kill-server and starting the emulator manually from the AVD manager.

Make sure that the pre-installed ApiDemos application is not installed in the emulator, or the signatures do not match. Remove the existing ApiDemos, then try deploying and running ApiDemos from IntelliJ IDEA, and then try running the test configuration again.


UPDATE: We did some research and found the source of the problem. By default, IntelliJ IDEA sets the dependency area for the application module inside the Compile test module, so that all production and test classes are compiled into one Test.apk.

Instead, the scope should be set to Granted , and we will fix it in the next update. Right now you need to fix it manually, as shown in the screenshot:

Provided scope

Restore the project and run the tests, again, this time 2 separate apk files will be deployed, one for the main application and one for the tests, then the tests will be executed:

 Waiting for device. Target device: emulator-5554 (ICS) Uploading file local path: D:\dev\android-sdk-windows\samples\android-15\ApiDemos\out\production\Tests\Tests.apk remote path: /data/local/tmp/com.example.android.apis.tests Installing com.example.android.apis.tests DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis.tests" pkg: /data/local/tmp/com.example.android.apis.tests Success Uploading file local path: D:\dev\android-sdk-windows\samples\android-15\ApiDemos\out\production\ApiDemos\ApiDemos.apk remote path: /data/local/tmp/com.example.android.apis Installing com.example.android.apis DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.android.apis" pkg: /data/local/tmp/com.example.android.apis Success Running tests Test running startedFinish 
+2
source

You can run the toolkit from ant, from the command line from a script. In case you prefer the latter, this post may help.

0
source

Running unit tests in the emulator is not practical (it takes too much time basically). And since android banks are only useful for compilation, it seems impossible to run them with unit tests. A good alternative is to use an advanced fake structure (I personally prefer jMockit, but there are others)

See an example:

https://github.com/ko5tik/andject/blob/master/src/test/java/de/pribluda/android/andject/ViewInjectionTest.java

Here I am testing my class against the derived activity of Android, and it should call the methods of the superclass (this is done in maven, eclipse or IDEA in place)

-1
source

Source: https://habr.com/ru/post/949190/


All Articles