-
Notifications
You must be signed in to change notification settings - Fork 6.4k
[Gradle] Add integration tests to KMP for External Android Target test execution and reporting #7637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yermukhamed Shakhman (ermawqa-jetbrains)
wants to merge
6
commits into
master
Choose a base branch
from
yermukhamed/kmp-external-android-target-integration-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[Gradle] Add integration tests to KMP for External Android Target test execution and reporting #7637
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
44c0296
extract externalAndroidLibraryProject helper fto unify project setup …
ermawqa-jetbrains 4b92070
add test case: externalAndroidLibraryProject with host tests
ermawqa-jetbrains 378675b
add test case: externalAndroidLibraryProject with device tests
ermawqa-jetbrains eeb85f0
dropped injection tautology & cleanup
ermawqa-jetbrains e22a54f
drop unused import
ermawqa-jetbrains ffc125c
add comment clarifying reason of usage AGP 9+
ermawqa-jetbrains File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
.../kotlin/gradle/android/externalAndroidTarget/AndroidTestReportsExternalAndroidTargetIT.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* | ||
| * Copyright 2010-2026 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
| */ | ||
|
|
||
| package org.jetbrains.kotlin.gradle.android.externalAndroidTarget | ||
|
|
||
| import com.android.build.api.dsl.KotlinMultiplatformAndroidDeviceTest | ||
| import com.android.build.api.dsl.KotlinMultiplatformAndroidLibraryTarget | ||
| import org.gradle.kotlin.dsl.kotlin | ||
| import org.gradle.util.GradleVersion | ||
| import org.jetbrains.kotlin.gradle.testbase.* | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertTrue | ||
|
|
||
| @AndroidTestVersions(minVersion = TestVersions.AGP.AGP_90) | ||
| @AndroidGradlePluginTests | ||
| class AndroidTestReportsExternalAndroidTargetIT : KGPBaseTest() { | ||
|
|
||
| // Uses `com.android.kotlin.multiplatform.library`, requires AGP new DSL. | ||
| override val defaultBuildOptions: BuildOptions | ||
| get() = super.defaultBuildOptions.copy(enableLegacyAgpDsl = false) | ||
|
|
||
| @GradleAndroidTest | ||
| fun `test - host tests execute and generate reports`( | ||
| gradleVersion: GradleVersion, | ||
| androidVersion: String, | ||
| jdkVersion: JdkVersions.ProvidedJdk, | ||
| ) { | ||
| externalAndroidLibraryProject( | ||
| gradleVersion = gradleVersion, | ||
| androidVersion = androidVersion, | ||
| jdkVersion = jdkVersion, | ||
| namespace = "org.jetbrains.sample.hosttestreports", | ||
| androidLibraryConfiguration = { | ||
| withHostTest {} | ||
| }, | ||
| ) { | ||
| buildScriptInjection { | ||
| kotlinMultiplatform.apply { | ||
| sourceSets.getByName("commonTest").dependencies { | ||
| implementation(kotlin("test")) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| kotlinSourcesDir("commonTest").resolve("CommonTestBase.kt").apply { | ||
| parent.toFile().mkdirs() | ||
| toFile().writeText( | ||
| """ | ||
| package org.jetbrains.sample | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertTrue | ||
|
|
||
| open class CommonTestBase { | ||
| @Test | ||
| fun testInCommon() { | ||
| assertTrue(true) | ||
| } | ||
| } | ||
| """.trimIndent() | ||
| ) | ||
| } | ||
|
|
||
| kotlinSourcesDir("androidHostTest").resolve("HostTest.kt").apply { | ||
| parent.toFile().mkdirs() | ||
| toFile().writeText( | ||
| """ | ||
| package org.jetbrains.sample | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class HostTest : CommonTestBase() { | ||
| @Test | ||
| fun testInHost() { | ||
| assertEquals("host", "host") | ||
| } | ||
| } | ||
| """.trimIndent() | ||
| ) | ||
| } | ||
|
|
||
| build(":testAndroidHostTest") { | ||
| assertTasksExecuted(":compileAndroidHostTest") | ||
| assertTasksExecuted(":testAndroidHostTest") | ||
| assertFileInProjectExists("build/reports/tests/testAndroidHostTest/index.html") | ||
| assertExecutedTestCases( | ||
| ":testAndroidHostTest", | ||
| "org.jetbrains.sample.CommonTestBase#testInCommon", | ||
| "org.jetbrains.sample.HostTest#testInCommon", | ||
| "org.jetbrains.sample.HostTest#testInHost", | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @GradleAndroidTest | ||
| fun `test - device test configuration time wiring`( | ||
| gradleVersion: GradleVersion, | ||
| androidVersion: String, | ||
| jdkVersion: JdkVersions.ProvidedJdk, | ||
| ) { | ||
| externalAndroidLibraryProject( | ||
| gradleVersion = gradleVersion, | ||
| androidVersion = androidVersion, | ||
| jdkVersion = jdkVersion, | ||
| namespace = "org.jetbrains.sample.devicetestwiring", | ||
| androidLibraryConfiguration = { | ||
| withHostTest {} | ||
| withDeviceTest { | ||
| instrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
| } | ||
| }, | ||
| ) { | ||
| buildScriptInjection { | ||
| kotlinMultiplatform.apply { | ||
| val commonTest = sourceSets.getByName("commonTest") | ||
| sourceSets.getByName("androidDeviceTest").dependsOn(commonTest) | ||
| } | ||
| } | ||
|
|
||
| val result = buildScriptReturn { | ||
| val commonTest = kotlinMultiplatform.sourceSets.getByName("commonTest") | ||
| val deviceTest = kotlinMultiplatform.sourceSets.getByName("androidDeviceTest") | ||
| val dependsOnCommon = deviceTest.dependsOn.contains(commonTest) | ||
| val hasTask = project.tasks.names.contains("connectedAndroidDeviceTest") | ||
|
|
||
| val target = kotlinMultiplatform.targets.withType(KotlinMultiplatformAndroidLibraryTarget::class.java).single() | ||
| val compilation = target.compilations.getByName("deviceTest") | ||
| val runner = compilation::class.java.methods.find { it.name == "getInstrumentationRunner" }?.invoke(compilation) as? String | ||
|
|
||
| Triple(dependsOnCommon, hasTask, runner) | ||
| }.buildAndReturn(":help") | ||
|
|
||
| assertTrue(result.first, "androidDeviceTest source set must depend on commonTest") | ||
| assertTrue(result.second, "Task connectedAndroidDeviceTest must be registered in task graph") | ||
| assertEquals("androidx.test.runner.AndroidJUnitRunner", result.third) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,44 @@ | |
|
|
||
| package org.jetbrains.kotlin.gradle.testbase | ||
|
|
||
| import com.android.build.api.dsl.KotlinMultiplatformAndroidLibraryTarget | ||
| import org.gradle.api.publish.PublishingExtension | ||
| import org.gradle.jvm.toolchain.JavaLanguageVersion | ||
| import org.gradle.kotlin.dsl.kotlin | ||
| import org.gradle.util.GradleVersion | ||
| import java.io.File | ||
|
|
||
| fun KGPBaseTest.externalAndroidLibraryProject( | ||
| gradleVersion: GradleVersion, | ||
| androidVersion: String, | ||
| jdkVersion: JdkVersions.ProvidedJdk, | ||
| namespace: String = "org.jetbrains.sample.androidlibrary", | ||
| withJava: Boolean = false, | ||
| androidLibraryConfiguration: KotlinMultiplatformAndroidLibraryTarget.() -> Unit = {}, | ||
| configureProject: TestProject.() -> Unit = {}, | ||
| ): TestProject = project( | ||
| "empty", | ||
| gradleVersion = gradleVersion, | ||
| buildOptions = defaultBuildOptions.copy(androidVersion = androidVersion), | ||
| buildJdk = jdkVersion.location, | ||
| ) { | ||
| plugins { | ||
| kotlin("multiplatform") | ||
| id("com.android.kotlin.multiplatform.library") | ||
| } | ||
| buildScriptInjection { | ||
| kotlinMultiplatform.apply { | ||
| targets.withType(KotlinMultiplatformAndroidLibraryTarget::class.java).configureEach { target -> | ||
| target.compileSdk = 34 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: let's eventually set this to the latest SDK (37)? |
||
| target.namespace = namespace | ||
| if (withJava) target.withJava() | ||
| target.androidLibraryConfiguration() | ||
| } | ||
| } | ||
| } | ||
| configureProject() | ||
| } | ||
|
|
||
| fun KGPBaseTest.kotlinAndroidLibraryProject( | ||
| gradleVersion: GradleVersion, | ||
| agpVersion: String, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: do we need to configure
withHostTest {}in this test case?