Why is the Android test runner reporting “Empty test suite”?

前端 未结 30 2031
有刺的猬
有刺的猬 2020-11-28 07:59

I am banging my head against the wall here trying to figure out why IntelliJ/Android is reporting \"Empty test suite\". I have a small project with two IntelliJ Modules (\"

相关标签:
30条回答
  • 2020-11-28 08:09

    My issue was caused by an exception being thrown in the @BeforeClass method of my test case. It some how wasn't causing the test to fail - I only found it by inspecting the logcat output.

    I fixed the exception and suddenly my tests were running!

    0 讨论(0)
  • 2020-11-28 08:09

    The accepted answer didn't solve my problem. So I decided to copy ExampleInstrumentedTest which is created by default in Android Studio and runs without any problems, renamed it during the copy process (no Refactor->Rename after copying!) and pasted the contents of my unit test into it. After that the error disappeared.

    0 讨论(0)
  • 2020-11-28 08:12

    I don't know if it helps for Android Studio, but I had some kind of Intellij-Gradle conflict. Solved it by "right-clicking" on the test-file and hit "compile file ...Test.java". After that I could run single tests again.

    0 讨论(0)
  • 2020-11-28 08:17

    I had tests that were running fine until gradle and android studio got upgraded.

    Apart from adding a default constructor to your tests, you might need to do some of these things to get your test suite to work

    Under src/ create androidTest/java/<your-package-name>/test . Note the androidTest. Anything else including instrumentTest will not work.

    Add this to build.gradle

    sourceSets {
        testLocal {
            java.srcDir file('src/androidTest/java')
            resources.srcDir file('src/androidTest/resources')
        }
    }
    
    
    
    android{
        sourceSets {
           instrumentTest.setRoot('src/androidTest/')
        }
    }
    
    dependencies{
         testLocalCompile 'junit:junit:4.11'
    }
    
    task localTest(type: Test, dependsOn: assemble) {
        testClassesDir = sourceSets.testLocal.output.classesDir
    
        android.sourceSets.main.java.srcDirs.each { dir ->
            def buildDir = dir.getAbsolutePath().split('/')
            buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
    
            sourceSets.testLocal.compileClasspath += files(buildDir)
            sourceSets.testLocal.runtimeClasspath += files(buildDir)
        }
    
        classpath = sourceSets.testLocal.runtimeClasspath
    }
    
    check.dependsOn localTest
    

    Add this to the AndroidManifest.xml

     <instrumentation
            android:name="android.test.InstrumentationTestRunner"
            android:label="Tests for my packaged app"
            android:targetPackage="<my-package-name>.test" />
    
    0 讨论(0)
  • 2020-11-28 08:18

    I just renamed the file and the problem fixed.

    0 讨论(0)
  • 2020-11-28 08:18

    I was doing some insertions in a db in the @BeforeClass method. I realised I had an object/database mapping problem. This data mapping problem was the cause of this issue for me.

    0 讨论(0)
提交回复
热议问题