Kotlin / kmp-basic-sample

Example of Kotlin multiplatform project
Apache License 2.0
1.01k stars 185 forks source link

Run Android Junit #59

Open StanKocken opened 3 years ago

StanKocken commented 3 years ago

Using this project as a base to understand how to run JUnit Android test within the shared Library: https://github.com/Kotlin/kmm-sample/blob/master/shared/src/androidTest/kotlin/com/jetbrains/kmm/shared/androidTest.kt

Cross-posting from Android Studio issue: https://issuetracker.google.com/issues/191287536 I'm not sure if it's a code issue (so a modification on this code base), or a IDE issue.

Goal: run with JUnit the tests with the Android configuration on the shared module.

Issue: the junit test folder is not recognized as such with Android, it seems to be override by an Android Connected test.

Symptoms:

1- "No Task Available"

NoTaskAvailable

by right-click on the folder, clicking on "Run test", it creates a configuration that does not work.

SetupTest

The issue seems to be that Android Studio doesn't recognised "kotlin" as a valid language for testing. By renaming the folder "kotlin" within the test to "java", it is now possible to run it.

2- Test run on device, not on JUnit

CreateConfiguration

By running the CLI "./gradlew :shared:tasks", I am missing some configurations (it should contains "androidTest" - Executes unit tests for target Android.):

Verification tasks
------------------
allTests - Runs the tests for all targets and create aggregated report
check - Runs all checks.
connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
connectedCheck - Runs all device checks on currently connected devices.
connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
iosX64Test - Executes Kotlin/Native unit tests for target iosX64.
lint - Runs lint on all variants.
lintDebug - Runs lint on the Debug build.
lintRelease - Runs lint on the Release build.
test - Run unit tests for all variants.
testDebugUnitTest - Run unit tests for the debug build.
testReleaseUnitTest - Run unit tests for the release build.

Build: AI-203.7717.56.2031.7435690, 202106072248, 

AI-203.7717.56.2031.7435690, JRE 11.0.10+0-b96-7281165x64 JetBrains s.r.o., OS Mac OS X(x86_64) v11.2.3, screens 3584.0x2240.0; Retina

AS: Arctic Fox | 2020.3.1 Beta 4; Kotlin plugin: 203-1.5.10-release-891-AS7717.8; Android Gradle Plugin: 4.0.1; Gradle: 6.5; Gradle JDK: version 11.0.10; NDK: from local.properties: (not specified), latest from SDK: (not found); LLDB: pinned revision 3.1 not found, latest from SDK: (package not found); CMake: from local.properties: (not specified), latest from SDK: (not found), from PATH: (not found)
StanKocken commented 3 years ago

As answered within the Android bug tracker, it seems to be a name conflict: AndroidTest is reserved for Instrumented Test by the Android plugin and cannot be used for Unit Test as all the other Platforms.

https://issuetracker.google.com/issues/191287536?pli=1