Kotlin Multiplatform (KMP) plugin and library that add support for reading resources in tests.
The plugin and a library work in tandem to provide a unified API across platforms for reading resources from each source set's resources
folder.
List the plugin in your build.gradle.kts
:
plugins {
id("com.goncalossilva.resources") version "<version>"
}
And add the dependency to your commonTest
source set:
kotlin {
sourceSets {
val commonTest by getting {
dependencies {
implementation("com.goncalossilva:resources:<version>")
}
}
}
}
Important
The latest version targets Kotlin 2.0 and is incompatible with Kotlin 1.9. On 2.0 and above, use the latest version available. On 1.9, use the latest version below v0.9.0.
Once setup is done done, a Resource
class becomes available in all test sources, with a simple API:
class Resource(path: String) {
fun exists(): Boolean
fun readText(): String
fun readBytes(): ByteArray
}
To setup resources correctly and avoid FilNotFoundException
& co:
- Put them in the resources folder of a source set. For example,
src/commonTest/resources/
orsrc/jsTest/resources/
. - Specify the path relative to the project's directory. For example,
src/commonTest/resources/a-folder/a-file.txt
.
With these in mind, you're ready to go.
Library tests use the library itself, so they serve as a practical example.
See ResourceTest
for example usage, and resources-test/src/commonTest/resources
for the associated folder structure for resources.
This library is inspired by this gist by @dellisd.
Released under the MIT License.