Android Studio "No tests were found"

37,927

Solution 1

Ok, I found the cause of my problem.

In the misc.xml file which is located in the .idea folder of my project had an invalid attribute value for the ProjectRootManager component, which looked like the following:

<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="1.6 (3)" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/build/classes" />
</component>

So changing the project-jdk-name attribute value to just "1.6", fixed the problem for me. Below is what it looked like after I had updated the value.

<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="1.6" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/build/classes" />
</component>

Hope this helps anyone out.

Solution 2

Today I had the same problem with some Espresso tests and it was getting me crazy because everything seemed normal. Finally I discovered the problem was because the method annotated with @BeforeClass was throwing an exception. If something goes wrong in that method, the stacktrace of the exception is not shown in the Log window of the Run tab but in the Log window of the Android Monitor tab

If you want to reproduce the problem just add this to your testing class:

@BeforeClass
public static void setupClass() {
    throw new RuntimeException("Sorry dude, you won't find any test!");
}

Solution 3

This can happen when the type of your run configuration is incorrect.

With me, this goes wrong when running an Espresso test which used to be a unit test. For some reason it still uses the Android JUnit test configuration when running this test. Manually creating an Android Instrumented Test solves the problem.

Solution 4

Just for posterity sakes, here's the solution that worked for me -

The error you're seeing happens if AS for some reason thinks that the test that you're about to run is a unit and not an instrumentation test. Often this error goes away buy just clicking on the package instead of the actual class.

Found at https://github.com/googlecodelabs/android-testing/issues/27#issuecomment-219074863

I just ran the tests by right clicking on the package name and choosing "Run 'Tests in {packageName}'" and it worked.

Solution 5

To run instrumentation test cases on android mobile device with kotlin, add below things into your app build.gradle:

 defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
         

Add these permissions:

    dependencies {
        testImplementation 'junit:junit:4.12'
        testImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestUtil 'androidx.test:orchestrator:1.2.0'
        androidTestImplementation 'androidx.test:runner:1.2.0'
        androidTestImplementation 'androidx.test:rules:1.2.0'
    }
        

I used below gradle version:

distributionUrl=https://services.gradle.org/distributions/gradle-5.4.1-all.zip

 buildscript {
        
            ext.kotlin_version = '1.3.71' 
          
            repositories {
                flatDir { dirs './lib' }
                google()
                mavenCentral()
                jcenter()
                maven { url 'https://plugins.gradle.org/m2/' }
            }
        
            dependencies {
                classpath 'com.android.tools.build:gradle:3.4.2'     
                classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            }
        }
        

Then in the src->androidTest->java->com.example.demo-> create class DemoTest.kt. If you need context, then get it via InstrumentationRegistry.getInstrumentation().targetContext

If your test cases requires any permissions, then add below rule:

@Rule
@JvmField
var permissionRule: GrantPermissionRule = GrantPermissionRule.grant(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN)
            

You can do all set up inside:

 @Before
 fun setup() {
    // Do initial setup here
 }

and then write test cases like this:

@Test
fun testFunctionality() {
    // write your code here         
}
Share:
37,927

Related videos on Youtube

Shane Doyle
Author by

Shane Doyle

Husband • Lead UX Designer You can find me on Twitter, GitHub, and Dribbble. Take a look at my GitHub Resume Take a look at some of my writing

Updated on March 24, 2021

Comments

  • Shane Doyle
    Shane Doyle over 3 years

    Has anyone been able to get tests to run in Android Studio (from the GUI and not terminal), I have been unable to run tests from the GUI.

    Everytime I try to run tests through the GUI I just get the following message:

    enter image description here

    I am able to run the tests from terminal using the following command:

    ./gradlew connectedAndroidTest
    

    I am running Android Studio 0.5.2 with Gradle 1.11 with Plugin 0.9.0 on Mac OSX

    My project structure is as follows;

    MyProject/
       src/
          androidTest/
             java/
                 com.myproject.app.test/
                    … (tests source code) …
          main/
             java/
                 com.myproject.app/
                    … (source code) …
             res/
                    … (resources code) …
       build.gradle
    

    My build.gradle file looks similar to the following:

    …
    android {
        compileSdkVersion 19
        buildToolsVersion "19.0.1"
    
        defaultConfig {
            versionCode 12
            versionName "2.0"
            minSdkVersion 9
            targetSdkVersion 19       
        testPackageName "com.test.foo"
            testInstrumentationRunner "android.test.InstrumentationTestRunner"
        }
    }
    …
    

    If anyone has any suggestions, I will be more than happy to here them.

    • gnuf
      gnuf over 10 years
      Can you post the contents of one of your test files?
    • Shane Doyle
      Shane Doyle over 10 years
      The content of the test files was not the issue, it was a problem with the misc.xml file in the .idea folder. But the test files were just simple TestCase files so I wouldn't see any reason why they be an issue.
    • IgorGanapolsky
      IgorGanapolsky about 10 years
      @iamshanedoyle You put /androidTest directory right under /src. But it should be at the same level as /src, not under it. Look at your build.gradle, and it should have: androidTest.setRoot('androidTest').
  • dazza5000
    dazza5000 over 7 years
    For me it was an issue with the dagger graph not being able to be created. The exception was showing up in the Log window of the Android Monitor tab and not in the Run tab!
  • Vicky Chijwani
    Vicky Chijwani over 6 years
    I'm having a similar issue with AS 3.0, but it is reproducible only intermittently. I created an Android Instrumented Test manually, it ran for a while, then at one point I had a test failure and since then it won't detect instrumented tests at all. Says "no tests were found".
  • aleksandrbel
    aleksandrbel over 6 years
    Hi, I have the same problem. I am using both java and Kotlin in my module. I have created Junit test manually, but maybe I have done it incorrectly. Can you write how you create test manually?
  • Supreethks
    Supreethks over 5 years
    I had my project-jdk-name attribute set to string JDK changed it to 1.8. Worked like charm!
  • Val Okafor
    Val Okafor almost 5 years
    How did you open your class?
  • m0skit0
    m0skit0 over 4 years
    It's a must with Mockito, be specific.
  • David Ferrand
    David Ferrand about 4 years
    Thanks -- your answer put me on the right track. As explained in that GitHub issue for android-test, the old (pre-AndroidX) android-test runner will NOT work on an emulator that has Android 10 API 29. Workaround is to use an emulator on Android 9 API 28.
  • Stachu
    Stachu almost 4 years
    worked for me as well, It might have been due to me moving the test to androidTest package from test
  • Demigod
    Demigod over 3 years
    Changed my 1.8 (2) to 1.8 but it didn't helped.
  • Ibrahim.H
    Ibrahim.H almost 3 years
    The only thing I had to change is defaultConfig, because I changed project setting to use androidx after creating the project.