I'm working on an Android app written in Kotlin. I would like to use Log.* to see the output of some very simple logic in a unit test, rendered as string. I'm using mockk as suggestested in this other question.
I don't get any errors, the test works fine; however, not even a single line is printed in the Logcat of Android Studio. I checked filters and current device with no avail.
Here is my simplified code:
// UnitTest.kt
class MyUnitTest {
@Before
fun setup() {
mockkStatic(Log::class)
every { Log.d(any(), any()) } returns 0
}
@Test
fun do_test() {
// ... some logic
Log.d("TEST_TAG", "Test string")
}
}
I also tried to:
@JvmStatic
into a companion objectbuild.gradle
as suggested here (though unrecommended)testOptions {
unitTests.returnDefaultValues = true
}
// app/src/test/android/util/MyLog.kt
package android.util
class MyLog {
companion object {
@JvmStatic
fun d(tag: String, msg: String): Int {
println("DEBUG: $tag: $msg")
return 0
}
}
Unfortunately none of these worked (with any of the Log levels).
I don't have great experience in writing tests, am I missing something?
Edit (about solution):
I was targeting Logcat, which is wrong, because unit tests run on a local machine.
You need to use println()
for logging in unit tests.
Logcat shows messages coming from a device. As unit tests are running on your local machine, it is physically impossible to see messages. println()
prints messages to the test "console".