Description
Library to help you with testing your LiveData and making your tests effective.
https://android.jlelse.eu/effective-livedata-and-viewmodel-testing-17f25069fcd4
LiveData Testing alternatives and similar packages
Based on the "Test" category.
Alternatively, view LiveData Testing alternatives based on common mentions on social networks and blogs.
-
powermock
PowerMock is a Java framework that allows you to unit test code normally regarded as untestable. -
selendroid
"Selenium for Android" (Test automate native or hybrid Android apps and the mobile web with Selendroid.) Join us on IRC #selendroid on freenode. Also confirm you have signed the CLA http://goo.gl/pAvxEI when making a Pull Request. -
android-junit-report
A custom instrumentation test runner for Android that generates XML reports for integration with other tools. -
Android UI testing utils
A set of TestRules, ActivityScenarios and utils to facilitate UI and screenshot testing under given configurations: FontSizes, Locales...
WorkOS - The modern identity platform for B2B SaaS
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of LiveData Testing or a related project?
README
LiveData Testing
TestObserver to easily test LiveData and make assertions on them.
Read Medium Article for more info.
[Explanatory Diagram](img/livedata-testing.png)
Usage
Having LiveData<Integer>
of counter from 0 to 4:
Kotlin - see ExampleTest.kt
liveData.test()
.awaitValue()
.assertHasValue()
.assertValue { it > 3 }
.assertValue(4)
.assertHistorySize(5)
.assertNever { it > 4 }
// Assertion on structures with a lot of nesting
viewLiveData.map { it.items[0].header.title }
.assertValue("Expected title")
Java - see ExampleTest.java
TestObserver.test(liveData)
.awaitValue()
.assertHasValue()
.assertValue(value -> value > 3)
.assertValue(4)
.assertHistorySize(5)
.assertNever(value -> value > 4);
Don't forget to use InstantTaskExecutorRule
from androidx.arch.core:core-testing
to make your LiveData test run properly.
Download
Kotlin users:
testImplementation 'com.jraska.livedata:testing-ktx:1.2.0'
Java users:
testImplementation 'com.jraska.livedata:testing:1.2.0'
Philosophy
This library is created in a belief that to effective and valuable test should be fast to write and model real code usage. As by Architecture components spec Activity should communicate with its ViewModel only through observing LiveData. TestObserver in this case simulates the Activity and by testing LiveData, we could test our whole logic except the View where the responsibility belongs to Activity. Key ideas:
- Test pretends to be an Activity
- No Android framework mocking or Robolectric - just standard fast JUnit tests
- Fluent API inspired by RxJava TestObserver
- Easy to write fast executing tests - possibly TDD
*Note that all licence references and agreements mentioned in the LiveData Testing README section above
are relevant to that project's source code only.