kotest-android
kotest-android copied to clipboard
Runner, assertions and extensions to run Android Instrumented Tests using Kotest
Kotest Android
Unofficial integration between the Kotest Framework and
the Android Platform. This repository aims to contain the tools necessary to run
Kotest in the complicated Android environment, with kotest-runner-android - a customized
version
of kotest-runner-junit4 - kotest-assertions-android
containing matchers and assertions specific to Android
Kotest Runner Android
A custom version
of kotest-runner-junit4 made
specifically to run Android Integration Tests.
Adding the Runner to an Android Project
The SubProject kotest-runner-android-tests should provide a good sample if you need one.
Android Block Configuration
In the Android Block of your build.gradle.kts you need to ensure that the testInstrumentationRunner is set
to "androidx.test.runner.AndroidJUnitRunner". Yes, the default runner is what we will leverage to run our tests.
android {
defaultConfig {
// ...
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
}
Dependencies Configuration
To androidTestImplementation we are going to add kotest-runner-android:
androidTestImplementation("br.com.colman:kotest-runner-android:VERSION")
Kotest Extensions Android
Dependencies Configuration
To testImplementation we are going to add kotest-extensions-android:
testImplementation("br.com.colman:kotest-extensions-android:VERSION")
Available Extensions
Robolectric Extension
The Robolectric Extension enables tests to operate under the Robolectric environment. To
utilize this feature, adorn the unit tests that you wish to run with Robolectric with the @RobolectricTest annotation.
Tests will be executed within the integrated Robolectric environment. @Config annotations can be used for Robolectric
specific configurations.
Here is a Kotlin example of how to use the annotation:
@RobolectricTest(sdk = Build.VERSION_CODES.O)
class ContainedRobolectricRunnerMergeApiVersionTest {
init {
"Get the Build.VERSION_CODES.O" {
Build.VERSION.SDK_INT shouldBe Build.VERSION_CODES.O
}
}
}
Kotest Assertions Android
Dependencies Configuration
To androidTestImplementation we are going to add kotest-assertions-android:
androidTestImplementation("br.com.colman:kotest-assertions-android:VERSION")
Available Matchers
Documentation and format to be improved.
Open Spoiler for Details
| View | |
|---|---|
view.shouldBeVisible() |
Asserts that the view visibility is VISIBLE |
view.shouldBeInvisible() |
Asserts that the view visibility is INVISIBLE |
view.shouldBeGone() |
Asserts that the view visibility is GONE |
view.shouldHaveContentDescription() |
Asserts that the view has any content description |
view.shouldHaveContentDescription(desc) |
Asserts that the view has desc as Content Description |
view.shouldHaveTag(key, value) |
Asserts that the view has a tag key with value value |
view.shouldHaveTag(any) |
Asserts that the view's tag is any |
view.shouldBeEnabled() |
Asserts that the view is enabled |
view.shouldBeFocused() |
Asserts that the view has focus |
view.shouldBeFocusable() |
Asserts that the view is focusable |
view.shouldBeFocusableInTouchMode() |
Asserts that the view is focusable in touch mode |
view.shouldBeClickable() |
Asserts that the view is clickable |
view.shouldBeLongClickable() |
Asserts that the view is long clickable |
| TextView | |
|---|---|
tv.shouldHaveText(text) |
Asserts that the text view has text text |
tv.shouldHaveTextColorId(id) |
Asserts that the text color is the same from color resource id |
tv.shouldHaveTextColor(colorInt) |
Asserts that the text color is colorInt |
tv.shouldBeAllCaps() |
Asserts that the textview is marked with the isAllCaps flag |
tv.shouldHaveTextAlignment(alignment) |
Asserts that the text alignment is alignment |