Search code examples
androidandroid-studiotestingandroid-testing

Android Studio -> Test Passed: 0 passed


I'm doing this tutorial to refresh my memory and update my knowledge with new state of the art "Developing Android Apps with Kotlin".

https://classroom.udacity.com/courses/ud9012

On class 6, lesson 10, there is this source code: https://github.com/udacity/andfun-kotlin-sleep-tracker/tree/Step.03-Solution-Create-RoomDatabase

@Test
@Throws(Exception::class)
fun insertAndGetNight() {
    val night = SleepNight()
    sleepDao.insert(night)
    val tonight = sleepDao.getTonight()
    assertEquals(tonight?.sleepQuality, -1)
}

Supposedly, it should pass a test, but after executing it, it returns this:

Test Passed: 0 passed.

And in the terminal of the right I can read this:

Running tests

$ adb shell am instrument -w -m    -e debug false -e class 'com.example.android.trackmysleepquality.SleepDatabaseTest#insertAndGetNight' com.example.android.trackmysleepquality.test/android.test.InstrumentationTestRunner
"Run Android instrumented tests using Gradle" option was ignored because this module type is not supported yet.

What is wrong with the project and why the test is not passed?

I added a text manually writing it, and the result is the same, 0 test passed. This is the test I added:

@Test
fun addition_isCorrect() {
    assertEquals(4, 2 + 2)
}

Also, I tried unchecking the box next to Run Android instrumented tests using Gradle in the testing settings (Go to File>>Settings>>Testing) with same result.


Solution

  • I downloaded the source code and figured out that **testInstrumentationRunner ** is missing so you need to add it in gradle.build

        defaultConfig {
            applicationId "com.example.android.trackmysleepquality"
            minSdkVersion 19
            targetSdkVersion 30
            versionCode 1
            versionName "1.0"
            multiDexEnabled true
            vectorDrawables.useSupportLibrary = true
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    

    and room operations need to be inside a coroutine scope so without it compiler is giving error

        @Test
        @Throws(Exception::class)
        fun insertAndGetNight() {
            runBlocking {
                val night = SleepNight()
                sleepDao.insert(night)
                val tonight = sleepDao.getTonight()
                assertEquals(tonight?.sleepQuality, -1)
            }
        }