7

I am trying to write an instrumentation test for my MainActivity. I followed the answers given here. Still Android Studio cannot find any tests. I have the class ApplicationTest.java in the androidTest folder. Here's the contents of the class:

package com.example.monicamarcus.mymusicplayer

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import com.example.monicamarcus.mymusicplayer.MainActivity;

public class ApplicationTest extends ActivityInstrumentationTestCase2<MainActivity> { 
    public ApplicationTest() {
        super(MainActivity.class);
    }

    public void testNextTrackButton() throws Exception {
        Activity activity = getActivity();

        Button nextButton = (Button) activity.findViewById(R.id.nextTrackBt);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                nextButton.performClick();
                assertTrue(currentPosition < songList.size());
            }});
        activity.finish();
    }
}

What is wrong with this test class? Or with the tests? I don't get any error, it just does not find any test to run. After I run the ApplicationTest the output ends with the following lines:

Running tests
Test running startedFinish
Empty test suite.

Here's the build.gradle file for the app:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.monicamarcus.mymusicplayer"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testApplicationId "app.src.androidTest.java.com.example.monicamarcus.mymusicplayer"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
    buildTypes {
        debug {
            minifyEnabled false
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

android {
    useLibrary 'org.apache.http.legacy'
}

android {

    sourceSets {

        androidTest {
            java.srcDirs = ['androidTest/java']
        }

    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    androidTestCompile 'junit:junit:4.12'
}

Here's the output of the test run:

Testing started at 1:56 PM ...
Target device: gt_i8190n-4790068ee9a750c6
Installing APK: /Users/monicamarcus/AndroidStudioProjects/MyMusicPlayer/app/build/outputs/apk/app-debug.apk
Uploading file to: /data/local/tmp/com.example.monicamarcus.mymusicplayer
Installing com.example.monicamarcus.mymusicplayer
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.monicamarcus.mymusicplayer"
pkg: /data/local/tmp/com.example.monicamarcus.mymusicplayer
Success


Installing APK: /Users/monicamarcus/AndroidStudioProjects/MyMusicPlayer/app/build/outputs/apk/app-debug-androidTest-unaligned.apk
Uploading file to: /data/local/tmp/app.src.androidTest.java.com.example.monicamarcus.mymusicplayer
Installing app.src.androidTest.java.com.example.monicamarcus.mymusicplayer
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/app.src.androidTest.java.com.example.monicamarcus.mymusicplayer"
pkg: /data/local/tmp/app.src.androidTest.java.com.example.monicamarcus.mymusicplayer
Success


Running tests
Test running startedFinish
Empty test suite.

After I made some changes (I can publish them if anybody is interested) the result of running the instrumentation test class read as follows: "Running tests. Test running started. Test running failed: No test results. Empty test suite." Nobody has experience with these kind of tests?

Community
  • 1
  • 1
Monica
  • 389
  • 1
  • 7
  • 19
  • Did you try using `@Test` intead of `@UiThreadTest` ? – Officer Bacon Feb 16 '16 at 08:14
  • @OfficerBacon Yes, I tried but the same thing happens. – Monica Feb 16 '16 at 08:19
  • I changed the code. see the edit in the question. Any help would be appreciated very much. – Monica Feb 16 '16 at 21:48
  • How do you run your tests? If in Android Studio, can you show the run configuration? – Code-Apprentice Feb 16 '16 at 21:52
  • Also, please post `build.gradle` and tell us what directory this test class is located in. – Code-Apprentice Feb 16 '16 at 21:54
  • Yes, I run the tests in Android Studio. The t.est class is in the folder androidTest under src. I shall add the run configuration and the file build.gradle – Monica Feb 16 '16 at 22:02
  • @Code-Apprentice Hello! Did you take a look at my updated question? Nobody wants to or can answer... :( – Monica Feb 18 '16 at 23:10
  • @Monica do you also get the error if you attempt to run the tests from the command line using the gradle wrapper? – sfera Feb 26 '16 at 10:54
  • @Monica - did u manage to fix this? I get the same issue too. – Rat-a-tat-a-tat Ratatouille Mar 28 '16 at 04:33
  • @Rat-a-tat-a-tatRatatouille No, not yet. I just postponed doing it, but I think I need to read more about how to write instrumentation tests. I tried to run it from the command line as suggested, initially I didn't get anything when doing this, but then again I believe it is not relevant because I probably didn't do it the right way. I have to learn more about doing instrumentation tests. – Monica Mar 29 '16 at 16:51
  • i have gotten them to run, but it was a pain figuring out how to do that. iirc, i had some luck running using gradle cc https://stackoverflow.com/questions/34247377/where-is-the-output-from-an-android-instrumented-test – Ray Tayek Jan 17 '17 at 07:23

3 Answers3

2

In build.gradle, testApplicationId looks very strange. I would remove this and testInstrumentationRunner. The default values should be fine. Specifically, if you do not specify testApplicationId, it will be created by appending ".test" to your applicationId.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

not sure if the problem has been found. Running into similar but only if added assertTrue() it got the "Empty test suite.".

but replace with assertNotNull() the test is fine, still cannot use assertTrue though (not know why).

(I'm using kotlin and modify the Android studio generated default test in the AndroidTest folder, and the test is coded in kotlin's companion object, if this make any difference). Update: using assertTrue() outside the companion object is fine.

lannyf
  • 9,865
  • 12
  • 70
  • 152
-1

i just created a new project in android studio 2.2.2, made an android test fail did a gradle cC, and got the output below.

you can see the test failing on each device at the bottom of the output.

this question may be related.

the reports are in So35426990/app/build/reports/androidTests/connected/index.html

d:\AndroidStudioProjects\So35426990>gradle cC

Incremental java compilation is an incubating feature.
The TaskInputs.source(Object) method has been deprecated and is scheduled to be
removed in Gradle 4.0. Please use TaskInputs.file(Object).skipWhenEmpty() instea
d.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2421Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72421Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2421Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCompat2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUi2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUtils2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportFragment2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportMediaCompat2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2421Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:incrementalDebugJavaCompilationSafeguard UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:transformClassesWithDexForDebug UP-TO-DATE
:app:mergeDebugJniLibFolders UP-TO-DATE
:app:transformNative_libsWithMergeJniLibsForDebug UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
:app:validateSigningDebug
:app:packageDebug UP-TO-DATE
:app:assembleDebug UP-TO-DATE
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareComAndroidSupportTestEspressoEspressoCore222Library UP-TO-DATE
:app:prepareComAndroidSupportTestEspressoEspressoIdlingResource222Library UP-TO-
DATE
:app:prepareComAndroidSupportTestExposedInstrumentationApiPublish05Library UP-TO
-DATE
:app:prepareComAndroidSupportTestRules05Library UP-TO-DATE
:app:prepareComAndroidSupportTestRunner05Library UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl UP-TO-DATE
:app:processDebugAndroidTestManifest UP-TO-DATE
:app:compileDebugAndroidTestRenderscript UP-TO-DATE
:app:generateDebugAndroidTestBuildConfig UP-TO-DATE
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources UP-TO-DATE
:app:mergeDebugAndroidTestResources
:app:processDebugAndroidTestResources UP-TO-DATE
:app:generateDebugAndroidTestSources UP-TO-DATE
:app:incrementalDebugAndroidTestJavaCompilationSafeguard UP-TO-DATE
:app:compileDebugAndroidTestJavaWithJavac
:app:compileDebugAndroidTestNdk UP-TO-DATE
:app:compileDebugAndroidTestSources
:app:mergeDebugAndroidTestShaders UP-TO-DATE
:app:compileDebugAndroidTestShaders UP-TO-DATE
:app:generateDebugAndroidTestAssets UP-TO-DATE
:app:mergeDebugAndroidTestAssets UP-TO-DATE
:app:transformClassesWithDexForDebugAndroidTest
:app:mergeDebugAndroidTestJniLibFolders UP-TO-DATE
:app:transformNative_libsWithMergeJniLibsForDebugAndroidTest UP-TO-DATE
:app:processDebugAndroidTestJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebugAndroidTest UP-TO-DATE
:app:validateSigningDebugAndroidTest
:app:packageDebugAndroidTest
:app:assembleDebugAndroidTest
:app:connectedDebugAndroidTest

acme.so35426990.ExampleInstrumentedTest > useAppContext[KFFOWI - 5.1] FAILED
        java.lang.AssertionError: failing
        at org.junit.Assert.fail(Assert.java:88)

acme.so35426990.ExampleInstrumentedTest > useAppContext[KFFOWI - 5.1.1] FAILED
        java.lang.AssertionError: failing
        at org.junit.Assert.fail(Assert.java:88)

acme.so35426990.ExampleInstrumentedTest > useAppContext[Nexus 7 - 5.1.1] FAILED

        java.lang.AssertionError: failing
        at org.junit.Assert.fail(Assert.java:88)
                                                   :app:connectedDebugAndroidTes
t FAILEDng 96% > :app:connectedDebugAndroidTest

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:connectedDebugAndroidTest'.
> There were failing tests. See the report at: file:///D:/AndroidStudioProjects/
So35426990/app/build/reports/androidTests/connected/index.html

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.

BUILD FAILED

Total time: 35.227 secs

d:\AndroidStudioProjects\So35426990>
Community
  • 1
  • 1
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90