chanjungskim
03/06/2023, 7:29 AMscientific
and professional
text are visible(I get this data from API). How can I wait and assert them here?ianbrandt
03/10/2023, 10:17 PMinternal
code from additional test source sets (e.g. when integration testing): https://youtrack.jetbrains.com/issue/KTIJ-23114.chanjungskim
03/22/2023, 3:43 PMchanjungskim
03/23/2023, 12:59 AMDiego DeSouza
03/29/2023, 8:03 PMchanjungskim
03/31/2023, 3:57 AM<https://github.com/android-alatan>
this project? someone in other community, he said he uses this library and he doesn't need to test the view but he can write test code starting from some specific event(for example, click event) and test all the way to the end he wants to test. What does it mean? and How can I write code something like that and I guess it's related to some architecture. I don't really get it. If there's someone who knows how can it be achieved, could you please share that idea? I don't want to test instrumented test because it's slow and hard to test(for example, I need to go throw all the way to the screen to test that screen). It'd be nice If can do unit test and test the behaviours.chanjungskim
04/18/2023, 8:04 AMOlivier Patry
06/28/2023, 9:54 AMViewModel
ran on JVM.
Exception while trying to handle coroutine exception
IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used
CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[<http://Dispatchers.IO|Dispatchers.IO>, Continuation at com.myscript.nebo.dms.MyViewModel$1$1.invokeSuspend(CoroutinesIssueTest.kt)@52fe5814]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
(see complete stack trace in thread).
I extracted a small repro unrelated to business logic but representative of my use case.Peter Mandeljc
08/07/2023, 8:34 AMEdoardo Luppi
08/11/2023, 1:24 PMebonet
08/14/2023, 10:46 AMTgo1014
08/31/2023, 11:50 AMTestCoroutineScope()
usually using TestScope()
makes many tests to break but TestScope(UnconfinedTestDispatcher())
makes the tests run fine. Is the later the same as the TestCoroutineScope()
?Manasseh
08/31/2023, 8:01 PMclass AuthTest {
@BeforeTest
fun setUp() {
embeddedServer(Netty, port = Config.TEST_SERVER_PORT.toInt()) {
configureSerialization()
configureDatabase(testing = true)
configureRouting()
}.start()
}
@Test
fun testLogin() = testApplication {
val res = <http://client.post|client.post>("/v1/login") {
header(HttpHeaders.ContentType, ContentType.Application.Json)
setBody(Json.encodeToString(UserSerializer.serializer(), UserSerializer("<mailto:test@email.com|test@email.com>", "password")))
}
assertEquals(HttpStatusCode.Accepted, res.status)
}
}
ErrorEdoardo Luppi
09/02/2023, 10:12 AMkotlin.test
one can filter the test classes to run using
gradle cleanJvmTest jvmTest --tests "*FakeTestSuite"
Is there a more fine grained mechanism to pass in includes
and excludes
patterns?Alejandro Moya
09/07/2023, 9:31 PMchristophsturm
09/27/2023, 8:44 AMChrys
09/29/2023, 9:27 AMkevindmoore
10/05/2023, 8:33 PMMerlin (they)
10/19/2023, 11:23 PMMethod getSystem in android.content.res.Resources not mocked
.
So I've tried using mockk
and mocking Resources both with mockk<Resources>()
or with mockkStatic
but then I get errors like Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
I'm very new to Android/Kotlin dev in general, so I'm probably doing this very wrong or doing something unnecessary, but I'll take any advice.
Should I even test this extension that seems pretty basic?
Should I be doing an instrumentation test and not a unit test?
If I should do this unit test, how do I mock it successfully?
Side note: what's the difference between this channel and #testing?
Extension Code
package com.package.messages.extensions
import android.content.res.Resources
import kotlin.math.roundToInt
val Int.dp: Int
get() = (this * Resources.getSystem().displayMetrics.density).roundToInt()
Attempted Test Code
import android.content.res.Resources
import android.util.DisplayMetrics
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import kotlin.math.roundToInt
// TODO Fix test with mock
class IntTest {
@Test
fun testDpExtension() {
val expectedDp = 10
val mockResources = mockk<Resources>("mockResources")
val mockStaticResources = mockkStatic(Resources::class)
val mockDisplayMetrics = mockk<DisplayMetrics>("mockDisplayMetrics")
val mockStaticDisplayMetrics = mockkStatic(DisplayMetrics::class)
every { mockResources.getDisplayMetrics() } returns mockDisplayMetrics
every { mockDisplayMetrics.density } returns 1F
// Method getSystem in android.content.res.Resources not mocked
val expectedPixels = (expectedDp * Resources.getSystem().displayMetrics.density).roundToInt()
val actualDp = expectedDp.dp
assertEquals(expectedPixels, actualDp)
}
}
Merlin (they)
10/23/2023, 12:35 AMinstrumentationTests.all
option
2. Also, how can I include instrumentation tests in kover coverage reports?
android {
testOptions {
unitTests.all {
useJUnitPlatform()
testLogging {
events 'passed', 'skipped', 'failed'
}
}
}
}
Piotr Krzemiński
12/11/2023, 6:32 PMPablo
02/06/2024, 6:49 PMFahim
02/19/2024, 10:38 AMimport dev.icerock.moko.mvvm.viewmodel.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
class SignupOtpViewModel: ViewModel() {
private val _isNextButtonEnabledFlow = MutableStateFlow(false)
val isNextButtonEnabledFlow: StateFlow<Boolean>
get() = _isNextButtonEnabledFlow
val otpField = MutableStateFlow("")
init {
setupBinding()
}
// create setup binding for otpField. Set _isNextButtonEnabledFlow to true if otpField count is 6
fun setupBinding() {
otpField
.map { it.length == 6 }
.onEach { _isNextButtonEnabledFlow.value = it }
}
}
and this is my test class:
import kotlin.test.Test
import kotlin.test.assertEquals
class SignupOtpTests {
@Test
fun init_shouldDisableNextButton() {
val sut = makeSut()
assertEquals(sut.isNextButtonEnabledFlow.value, false)
}
@Test
fun fillCompleteOtp_shouldEnableNextButton() {
val sut = makeSut()
sut.otpField.value = "123456"
assertEquals(sut.isNextButtonEnabledFlow.value, true)
}
fun makeSut() : SignupOtpViewModel {
val sut = SignupOtpViewModel()
return sut
}
}
fillCompleteOtp_shouldEnableNextButton is failing. How can I fix it?Marco Garofalo
03/04/2024, 4:48 PMpackage com.example.gameplay
class RotatingSecrets(private val secrets: List<String>) : Secrets {
private var position = 0
override fun next(): String {
return secrets[position++ % secrets.size]
}
}
This is what I have tried so far:
package com.example.gameplay
import io.kotest.matchers.equals.shouldBeEqual
import kotlinx.coroutines.CoroutineStart.LAZY
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
class RotatingSecretsTests {
@Test
fun `is thread safe`() = runTest {
val concurrentAccesses = 100
val secrets = (1..concurrentAccesses).map { "secret-$it" }
val rotatingSecrets = RotatingSecrets(secrets)
val jobs = secrets.map {
async (start = LAZY) {
rotatingSecrets.next()
}
}
val seenSecrets = jobs.awaitAll()
seenSecrets.sorted() shouldBeEqual secrets.sorted()
}
}
I have also tried increasing the concurrency up to 10_000, but no luck.
Any clever idea?ursus
03/08/2024, 10:55 PMrunBlocking
to runTest
in unit tests, and now I get this
kotlinx.coroutines.test.UncaughtExceptionsBeforeTest: There were uncaught exceptions before the test started. Please avoid this, as such exceptions are also reported in a platform-dependent manner so that they are not lost.
at app//kotlinx.coroutines.test.TestScopeImpl.enter(TestScope.kt:242)
at app//kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt.runTest-8Mi8wO0(TestBuilders.kt:307)
at app//kotlinx.coroutines.test.TestBuildersKt.runTest-8Mi8wO0(Unknown Source)
at app//kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt.runTest-8Mi8wO0(TestBuilders.kt:166)
at app//kotlinx.coroutines.test.TestBuildersKt.runTest-8Mi8wO0(Unknown Source)
at app//kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt.runTest-8Mi8wO0$default(TestBuilders.kt:158)
at app//kotlinx.coroutines.test.TestBuildersKt.runTest-8Mi8wO0$default(Unknown Source)
is there an way to debug this, to figure out what error?
@Test
fun `foo bar quax`() = runTest {
...
}
the test is shaped as always like this .. so not sure how can any issue happen BEFORE runTest
Mahmoo
03/17/2024, 11:07 AMabhinaygupta
03/25/2024, 5:50 PM