Home » Android » gradle – How do I structure project test directory in Android Studio?

gradle – How do I structure project test directory in Android Studio?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&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

Answer:

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.

Answer:

I was experiencing this issue as well. AndroidStudio was only building and installing the main application APK to the device, and failing the tests with following message:

Test running startedTest running failed: Unable to find instrumentation info for:ComponentInfo{com.example.app.test/android.test.InstrumentationTestRunner}

Empty test suite.

Yet, running the connectedCheck Gradle target was generating and installing two APKs and successfully executing the instrumentation tests. Turns out you can ask Gradle to assemble and install the instrumentation APK alone by running the installDebugAndroidTest target. After running this target, AndroidStudio was able to run instrumentation tests fine, and was able to even attach the debugger! I’m guessing that you’ll need to refresh the instrumentation APK every time you change the tests themselves, but not otherwise.

Instrumentation APKs installed on the device can be listed by running:
adb shell pm list instrumentation

Answer:

i have another way to solve this problem , if your reason just is the problem of run configuration.

check your run configuration , if the configuration of your project is Android Tests but not Android Application and then you will face

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

so delete the Android Tests configuration and add a Android Application run configuration for your peoject ,you can run it .