Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I structure project test directory in Android Studio?

There is no convention for this yet, but how do I structure the test directory for Android Studio, now that what's stated on the Android testing fundamentals page differs?

Particularly, any pointers on how to get jUnit tests up and running on Android Studio would be helpful, thanks.

Also, using the android tool does not really help now, since things are a bit different with Android Studio.

Update:

I tried setting up the test folder and running it, but all I'm getting is the following:

Running tests Test running startedTest running failed: Unable to find instrumentation info for:ComponentInfo{<project-package-name>/android.test.InstrumentationTestRunner} Empty test suite. 

I've also tried adding a standard AndroidManifest.xml file for tests in there.

like image 774
Jonathan Lin Avatar asked May 22 '13 01:05

Jonathan Lin


People also ask

How do I create a test folder on Android?

Right click on app > New > Folder > Java Folder. On Configure Component window check Change Folder Location checkbox. Change the location to src/test. Your test folder will be created under src folder i.e. same place where previous test folder was.

Which directory is used to store unit tests in your Android project?

By default, the source files for local unit tests are placed in module-name/src/test/ . This directory already exists when you create a new project using Android Studio.

When should you use the Android test directory?

A typical project in Android Studio contains two directories in which you place tests. The androidTest directory should contain the tests that run on real or virtual devices. Such tests include integration tests, end-to-end tests, and other tests where the JVM alone cannot validate your app's functionality.

What is project structure in Android Studio?

Project structure settings Project: Sets the version for Gradle and the Android plugin for Gradle, and the repository location name. Modules: Allows you to edit module-specific build configurations, including the target and minimum SDK, the app signature, and library dependencies. See Modules, below.


2 Answers

UPDATE

Starting from Build Tools 19.1.0 and build plugin 0.11.0 build.gradle files needs to have testPackageName renamed to testApplicationId ( also packageName should be renamed to androidId)

As of build plugin 0.9.0 instrumentTest folder is renamed to androidTest. That's all we need for testing.

Here is example of 0.11.+ DSL

android {     compileSdkVersion 19     buildToolsVersion "19.1.0"      defaultConfig {         minSdkVersion 16         targetSdkVersion 19         versionCode 1         versionName "1.0"         androidId "org.homelab.lab"         testApplicationId "org.homelab.lab.test"         testInstrumentationRunner "org.homelab.lab.test.Runner"     }      ... } 

GOTCHAS : if your build file consists definitions of testPackageName and testInstrumentationRunner, remove them

For version 0.5.0 - 0.8.+

Android Studio uses Gradle plugin version 0.5.+ which follows Gradle SourceDir principles.

Android Studio project structure

How to make it work:
1.update SDK
2.install or update Gradle to 1.6 (reported problems with 1.7) or stick with gradle wrapper
3.don't use Android Studio for running instrumentation task, use gradle command

gradle connectedCheck 

4.don't use same package for test and main apk
5.check results using browser

<project>/build/reports/instrumentTests/index.html 

Gotchas:
If test package and main package are the same it may create empty TestSuite. Result is misleading as Gradle reports no problems but reports show that no Class has been tested.

EDIT:

Below is the part of build.gradle which configures instrument tests required before 0.9.0:

android {     compileSdkVersion 14     buildToolsVersion "17.0.0"      defaultConfig {         minSdkVersion 14         targetSdkVersion 17         testPackageName "org.homelab.lab.test"         testInstrumentationRunner "org.homelab.lab.test.Runner"     }      ... } 

example project https://github.com/swavkulinski/android-studio-instrumentation-test

like image 106
robotoaster Avatar answered Sep 28 '22 14:09

robotoaster


Now in Android Studio you can set up instrumentTests by simply following the convention of placing the tests in the instrumentDirectory. Gradle only needs to know of any depencies that you have, which in my case is Robotium:

Android Studio Screenshot

dependencies {     androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.0.+' } 

When the test is finished you will have the results displayed in the GUI, so you do not have to use any command line at all! You can also right click the module above the JUnit test to run all JUnit tests.

like image 41
jbejar Avatar answered Sep 28 '22 14:09

jbejar