最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Running InstrumentedAndroidTest in KMP project - Stack Overflow

programmeradmin3浏览0评论

I need some help please,

I want to do some tests using Instrumented in Android (I need to get the appContext, to get the directory to create my test db). But I was having the error: No instrumentation registered! Must run under a registering instrumentation.

Now, with Kotest and Roboeletric, I can make Android Studio recognize my class to test as Instrumented test, but now I have the error: basic:

Invalid test class 'com.diegogaona.cocktailhype.data.repository.TagRepositoryDbTest':

1. No runnable methods
at .junit.runners.ParentRunner.validate(ParentRunner.java:525)
at .junit.runners.ParentRunner.<init>(ParentRunner.java:92)
at .junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:74)

My code is: My test class in commonTest:

@file:OptIn(ExperimentalCoroutinesApi::class, ExperimentalSerializationApi::class)

package com.diegogaona.cocktailhype.data.repository

import com.diegogaona.cocktailhype.data.local.CouchbaseManager
import com.diegogaona.cocktailhype.data.local.couchbaseLocalConfigTest
import com.diegogaona.cocktailhype.di.LogWrapper
import com.diegogaona.cocktailhype.domain.model.Colors
import com.diegogaona.cocktailhype.domain.model.TagToSave
import com.diegogaona.cocktailhype.domain.model.Translation
import com.diegogaona.cocktailhype.domain.repository.IDatabaseManager
import io.kotest.core.spec.style.FunSpec
import io.kotest.koin.KoinExtension
import kotbase.Database
import kotbase.Collection
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.ExperimentalSerializationApi
import .junit.runner.RunWith
import .koin.dsl.module
import .koin.test.KoinTest
import .koin.test.inject
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.fail
import .robolectric.RobolectricTestRunner

val testModule = module {
    val logger = object : LogWrapper {
        override fun d(message: String, tag: String?) {
            println("Debug: $message | Tag: $tag")
        }

        override fun i(message: String, tag: String?) {
            println("Info: $message | Tag: $tag")
        }

        override fun w(message: String, tag: String?) {
            println("W: $message | Tag: $tag")
        }

        override fun e(message: String, tag: String?, throwable: Throwable?) {
            println("Error: $message, Throwable: $throwable")
        }
    }
    single<IDatabaseManager<Database, Collection>> {
        CouchbaseManager(couchbaseLocalConfigTest)
    }
    single<LogWrapper> { logger }
    single<ITagRepository> { TagRepositoryDb(databaseManager = get(), logger = get()) }
}



@RunWith(RobolectricTestRunner::class)
class TagRepositoryDbTest : FunSpec(), KoinTest {
    override fun extensions() = listOf(KoinExtension(testModule))
    val tagRepository by inject<ITagRepository>()
    val databaseManager by inject<IDatabaseManager<Database, Collection>>()

    init {
        test("save and get operations should work correctly") {
            runBlocking {
                val tagToSave = TagToSave(
                    name = Translation(txt = "IntegrationTestTag"),
                    colors = Colors(high = "#FF5733", bg = "#C70039")
                )
                val saveResult = tagRepository.save(tagToSave)
                assertTrue(saveResult.isSuccess, "Tag should be saved successfully.")

                delay(300)

                val tagFlow = tagRepository.getMany("IntegrationTestTag", useFts = false)
                val tags = tagFlow.first()
                assertTrue(
                    tags.isNotEmpty(),
                    "There should be at least one tag matching the search term."
                )

                val savedTag = tags.first() ?: fail("Saved tag should not be null")
                val getResult = tagRepository.get(savedTag.id)
                getResult?.let { assertTrue(it.isSuccess, "get() should return the tag successfully.") }
                val retrievedTag = getResult?.getOrNull() ?: fail("Retrieved tag is null")
                assertEquals(savedTag.id, retrievedTag.id)
                assertEquals("IntegrationTestTag", retrievedTag.name.txt)
            }
        } // More tests....

In my androidInstrumentedTest directory, I have:

package com.diegogaona.cocktailhype

import android.app.Instrumentation
import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import br.colman.kotest.KotestRunnerAndroid
import .junit.BeforeClass
import .junit.runner.RunWith
import .robolectric.RobolectricTestRunner

lateinit var testAppContext: Context

@RunWith(RobolectricTestRunner::class)
class TestSetup {
    companion object {
        private lateinit var instrumentation: Instrumentation

        @BeforeClass
        @JvmStatic
        fun setupContext() {
            instrumentation = InstrumentationRegistry.getInstrumentation()
            testAppContext = instrumentation.targetContext
        }
    }
}

actual fun getDatabasePathTest(): String {
    TestSetup.setupContext()
    return testAppContext.filesDir.path
}

Why I'm getting this error? Why it doesn't recognize my tests?

Thanks in advance!

I tried to do it without Roboelectric and got the error No instrumentation registered! Must run under a registering instrumentation.. Tried junit 4 and 5 and other configs.

发布评论

评论列表(0)

  1. 暂无评论