I was wondering, is it possible to create commonIn...
# multiplatform
a
I was wondering, is it possible to create commonIntegrationTest sourceset which behaves the same way commonTest does? which would mean you could run integration tests on all native targets(ios, android, etc). I know you can create independant source sets, but they are usually bound to a single native target. All/most of the examples are just around the jvm target
https://kotlinlang.org/docs/multiplatform-configure-compilations.html#create-a-custom-compilation Here it infers that i use the jvm target, but i dont want to do that, as i dont currently use jvm target, it would then mean i would have to support my expects for a target i dont want to have to support.
k
Is there any reason you wouldn’t just create a separate gradle module for this and use normal old
commonTest
in that?
a
The items I want to test via integration, they are marked as internal 😱
If unit tests live in this module then why wouldn’t integration test do
k
Normally you’d have them both just live in
commonTest
. I’m not quite sure why you want them to live in separate sourcesets
a
If I didn’t want to run my integration tests every time I ran my unit tests, then it would make more sense
Also it shows a clear degree of separation between the two, to ensure the lines are not blurred between unit tests and integration tests with a big project
k
I think if I were in your shoes I would just have two separate packages in common test,
unit
and
integration
. Then I would invoke them separately based on package name from the CLI
a
Feels like there should be a way
k
eg.
Copy code
./gradlew :shared:cleanIosSimulatorArm64Test :shared:iosSimulatorArm64Test --tests "my.package.integration.*"
a
Yeah but that’s not the solution, that’s just a plaster over the problem. There must be a cleaner way
j
There is a way to do this with Kotlin 1.9.0. See the new target hierarchy APIs: SourceSetTree.integrationTest, KotlinTargetHierarchyBuilder.sourceSetTrees(), and targetHierarchy.default and targetHierarchy.custom. I haven't done this specifically myself, but something like this should work:
Copy code
@OptIn(ExperimentalKotlinGradlePluginApi::class)
kotlin {
    // or targetHierarchy.custom if you want to manually define intermediate source sets
    targetHierarchy.default {
        // create commonIntegrationTest source set tree as well as the default commonMain and commonTest
        sourceSetTrees(SourceSetTree.main, SourceSetTree.test, SourceSetTree.integrationTest)
    }

    androidTarget {
        publishAllLibraryVariants()
        // androidInstrumentedTest will be part of the commonIntegrationTest source set tree
        instrumentedTestVariant.sourceSetTree.set(SourceSetTree.integrationTest)
        // androidUnitTest will be part of the commonUnitTest source set tree
        unitTestVariant.sourceSetTree.set(SourceSetTree.test)
    }
    jvm()
    iosArm64()
    iosX64()
    // ... other supported targets
}
a
Will have to check later
j
Not sure if you target Android, but the ability to assign the androidUnitTest and androidInstrumentedTest source sets to separate source set trees is one thing this feature enables that you can't do with simply keeping unit and integration tests in separate packages in the same source set.
a
cheers @Jeff Lockhart - for now i have gone with using the android instrument tests as the integration tests for now(i can use 1.8.20).
j
Yes, that part of the API was introduced in 1.8.20.
k
Struggling to set this up, anyone got a chance to use new
integrationTest
? I’m specifically interested in having
jvmIntegrationTest
(depends on
jvmMain
but not
jvmTest
).
b
Hi folks, Did anyone succeed in creating custom compilation for
androidTarget
? The documentation only mentions example for
jvm
target, but when I try to create in similar way new compilation for
androidTarget
it fails with:
Copy code
> Could not create an instance of type org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation.
      > Unable to determine constructor argument #1: value 'integrationTest' is not assignable to type KotlinCompilationImpl, or no service of type KotlinCompilationImpl.
for the following (analogous to what we have in docs for
jvm
target) code:
Copy code
androidTarget {
    compilations {
        val integrationTest by compilations.creating {
        }
    }
}
I'm using
v2.1.10
of Kotlin Multiplatform Plugin. Maybe @Jeff Lockhart or @Andrew Reed, do you have some ideas?
a
you are including compilations twice.
Copy code
androidTarget {
    compilations {
        val integrationTest by creating {
        }
    }
}
does that not work?
b
yeah, result is the same with your code.
e
afaik it's just impossible for Android as AGP has its own special handling for test types
integrationTest should be doable for all non-android targets, but for android you will have to use test tags or other ways of selecting tests if you want to run only a subset of them
b
Yes, unfortunately it looks like this, pity :(.