Why does Android Studio say "Test events were not received"?
Asked Answered
C

18

51

I'm trying to unit test in my android application, and this is the simple test tutorial what i'm doing.

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class ServerListManagerTest extends AndroidTestCase{

   @Test
   public void testTrueIsTrue() throws Exception {
    assertEquals(true, true);
    }
}

The directory is like this, src\main\androidTest\java\some packages\ServerListManagerTest.java

I tried changing directory of this, and also build configuration. but android studio still doesn't recognize my unit test though build was successful.

This is my build.gradle in app,

apply plugin: 'com.android.application'

android {
   compileSdkVersion 21
   buildToolsVersion "21.1.2"

   defaultConfig {
       applicationId "com.kaist.se.pmpapp"
       minSdkVersion 16
       targetSdkVersion 21
       versionCode 1
       versionName "1.0"
   }

buildTypes {
       release {
          minifyEnabled false
          proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
         }
      }
sourceSets { main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } } }



dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    androidTestCompile 'org.robolectric:robolectric:2.4'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile group: 'junit', name: 'junit', version: '4.12'
  }

What's wrong in my code????

Constitutionality answered 22/5, 2015 at 10:24 Comment(1)
In my case, I just scrolled down the message window and found actually error.Trenchant
F
22

I assume you're using Android Studio version 1.2, the latest at this time.

I don't think anything is wrong with your code. According to Jason Atwood's post, the problem seems related to gradle caching the previous results and not running it again. If you look at the "Gradle console", you'll see everything say "UP-TO-DATE". However, his suggestion of adding the "--rerun-tasks" option to the script parameters was not sufficient for me.

In addition to "--rerun-tasks", I had to turn off the in-process build and force it to call the external gradlew tool. To do this, go to...

File > Settings > Build, Execution, Deployment > Compiler

Then un-check the "Use in-process build" option. Hopefully a future release of Android Studio will fix this and we can re-enable that option.

Fencesitter answered 26/5, 2015 at 19:56 Comment(10)
I tried your suggestion and now I can see the message 'No tests found for given includes', though my test code is same above.Constitutionality
At least it's a different problem so we're making progress. I believe your test is on the wrong directory. I have mine in src/test/java/... (i.e. parallel to main). src/androidTest/java is for tests run on the device itself.Fencesitter
In the report the unit tests were successful but AS said test events were not received. I just disabled "Use in-process build" according to your advice, and now I see the result in AS. I freaking hate this kind of things in tools. Why can't they just work as expected out of the box, without user's spending hours for unknown errors and then finally searching the web for a fix.Bohi
As I mentioned earlier, it works, but I have a strange issue. In the report, the unit tests seems to have run once. But in AS, there are "Gradle Test Executor 1" and "Gradle Test Executor 2" running the same tests twice each. This is not a serious problem for me, but can I fix this?Bohi
@SinJeong-hun I just tried this on Android Studio 1.2.2, it is indeed only the "in-process build" option that makes the difference. The --rerun-tasks switch didn't change anything. I feel your pain, instead of doing the actual work I'm forced to deal with this stuff.Mejia
@Mejia this is weird, I'm on AS 1.2.2 as well and disabling "in-process build" didn't change anything for me, but adding "--rerun-tasks" did the trick.Quest
After spending 4 hours fighting this with Android Studio 1.2.2 on my Mac, disabling in-process build fixed the issue.Laguna
FIxed the issue, but now my tests seem to be running twice. Better than none at all, I supposePoisoning
Thank you, this helped me. Wasted a days development finding out what the problem was. Obviously productivity belongs in other languages and environments.Suavity
Hey guys where is this option in the current Android Studio?Wilder
I
71

Sometimes you might have simple a compiler issue and end up with this message. This happened to me.. The compiler issue was not pointed out directly had to scroll down on the stack trace to find what is the issue.

enter image description here

Injured answered 4/10, 2021 at 7:24 Comment(0)
Y
24

I faced the same issue Test events were not received , I checked the function I wrote and found that there is a compiler error.

My solution is check the files/functions you are testing there may be compiler errors

this may help some one with same problem.

Youmans answered 10/8, 2021 at 13:46 Comment(3)
Thanks, exactly my case. I would add that links compilation errors were actually presented in the logs above.Ungava
Thanks for this! In my case, it was a compiler error somewhere else in that module's test code, unrelated to the test I was trying to run.Unhitch
Generally the log console of the running test shows what's wrong. In my case there was also a compiler error but I didn't scroll until the end of the log console... that's a learnt lesson for me. Thanks for your comment.Colincolinson
F
22

I assume you're using Android Studio version 1.2, the latest at this time.

I don't think anything is wrong with your code. According to Jason Atwood's post, the problem seems related to gradle caching the previous results and not running it again. If you look at the "Gradle console", you'll see everything say "UP-TO-DATE". However, his suggestion of adding the "--rerun-tasks" option to the script parameters was not sufficient for me.

In addition to "--rerun-tasks", I had to turn off the in-process build and force it to call the external gradlew tool. To do this, go to...

File > Settings > Build, Execution, Deployment > Compiler

Then un-check the "Use in-process build" option. Hopefully a future release of Android Studio will fix this and we can re-enable that option.

Fencesitter answered 26/5, 2015 at 19:56 Comment(10)
I tried your suggestion and now I can see the message 'No tests found for given includes', though my test code is same above.Constitutionality
At least it's a different problem so we're making progress. I believe your test is on the wrong directory. I have mine in src/test/java/... (i.e. parallel to main). src/androidTest/java is for tests run on the device itself.Fencesitter
In the report the unit tests were successful but AS said test events were not received. I just disabled "Use in-process build" according to your advice, and now I see the result in AS. I freaking hate this kind of things in tools. Why can't they just work as expected out of the box, without user's spending hours for unknown errors and then finally searching the web for a fix.Bohi
As I mentioned earlier, it works, but I have a strange issue. In the report, the unit tests seems to have run once. But in AS, there are "Gradle Test Executor 1" and "Gradle Test Executor 2" running the same tests twice each. This is not a serious problem for me, but can I fix this?Bohi
@SinJeong-hun I just tried this on Android Studio 1.2.2, it is indeed only the "in-process build" option that makes the difference. The --rerun-tasks switch didn't change anything. I feel your pain, instead of doing the actual work I'm forced to deal with this stuff.Mejia
@Mejia this is weird, I'm on AS 1.2.2 as well and disabling "in-process build" didn't change anything for me, but adding "--rerun-tasks" did the trick.Quest
After spending 4 hours fighting this with Android Studio 1.2.2 on my Mac, disabling in-process build fixed the issue.Laguna
FIxed the issue, but now my tests seem to be running twice. Better than none at all, I supposePoisoning
Thank you, this helped me. Wasted a days development finding out what the problem was. Obviously productivity belongs in other languages and environments.Suavity
Hey guys where is this option in the current Android Studio?Wilder
D
7

On my side I fixed the issue by using:

import org.junit.jupiter.api.Test

instead of

import org.junit.Test

Dareece answered 4/6, 2023 at 10:23 Comment(0)
B
4

If you have multiple Flavours, you may have selected another flavour, not the one with your tests. E.g. if you have prod and debug flavour and your tests are in debug, you may have switched to prod one and then you'll get the same error. Selecting the debug flavour will resolve the issue.

Bisson answered 16/12, 2021 at 6:7 Comment(0)
F
3

In my case (AS Chipmunk), I started receiving this error after trying to modify the run configurations for the tests (to stop code coverage from showing the Hilt generated classes).

What I did was delete the .idea folder and after restarting Android Studio tests were working once again.

Forcible answered 4/8, 2022 at 13:16 Comment(0)
A
1

Try to build the project and run tests again. In my case I had a bug in the code and therefore the tests did not run either.

Auburn answered 5/4, 2023 at 13:49 Comment(0)
L
0

In my case, apart from fixing compiling issues, none of the answers worked. However, invalidating Android Studio's cache did the trick.

  1. Go to File
  2. Press Invalidate Caches...
  3. Click on Invalidate and Restart
Lumper answered 29/3, 2022 at 13:42 Comment(0)
A
0

In my case, the problem was that one of the test methods I had written was missing public from the method declaration. Like

@Test
void testSomething() {
   //
}

as opposed to

@Test
public void testSomething() {
}

Interestingly even though I was trying to debug some other properly defined method in the same class, the framework would not find any of the tests in the class.

So it's not just the syntax errors, but the semantic issues wrt the unit test framework also throw everything off.

Acierate answered 19/4, 2022 at 23:30 Comment(0)
M
0

In My case the solution was to go to androidStudio -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle and then, change Gradle JDK (From 15 to 13)

Regards

Mintamintage answered 31/5, 2022 at 14:55 Comment(0)
P
0

Do double check if you are in gradle offline mode. A stupid but pretty common mistake.

Poliomyelitis answered 5/9, 2022 at 16:3 Comment(0)
C
0

This is an old issue, but for future google searchers and perhaps interested parties that could work around this, it is worth mentioning that if you are using one of the more popular walk-throughs to set up JaCoCo + Robolectric + Espresso - https://medium.com/@rafael_toledo/setting-up-an-unified-coverage-report-in-android-with-jacoco-robolectric-and-espresso-ffe239aaf3fa . Please add below this:

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}
Courtly answered 3/11, 2022 at 11:4 Comment(0)
H
0

I had a similar message with instrumented tests. Turned out, you have to run connectedAndroidTest for them to be run.

Humour answered 24/2, 2023 at 9:13 Comment(0)
W
0

This happens when some of the unit test cases fail. Try to run

./gradlew clean testDebugUnitTest 

to get the failing unit tests.

Whimsey answered 31/7, 2023 at 7:51 Comment(0)
M
0

This problem seems to crop up in different ways over the years. Here is what fixed it for me in 2023 with Studio 2022.2.1 Patch 2.

Tests in module projects were not running and "Tests not recieved" error. Added this to the module's gradle to fix:

android {
    namespace = "com.foo.bar"
    testOptions {
         unitTests {
            all {
                 it.enabled = true
            }
        }
    }
 }
Mcgowen answered 25/8, 2023 at 16:2 Comment(0)
A
0

I had the same issue. Fixed it by appending the following plugin declaration to my module's build.gradle file's existing plugins:

plugins {
    ...
    ...
    id 'kotlin-android'
}
Armidaarmiger answered 4/10, 2023 at 16:7 Comment(0)
M
0

For me it was the mockito library that I imported into my project, after deleting it it worked again, see if there is no new libraries that you imported, delete them just to test that the problem comes from a newly added lib.

Morril answered 11/12, 2023 at 14:17 Comment(0)
S
0

In my case, I synced old dependency from a tutorial, So I copied the correct dependency from the official site.

Satyr answered 6/2 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.