Filling the gap between RxJava and Espresso
Just set the testInstrumentationRunner
to RxAndroidJUnitRunner
in your app module's build.gradle file:
defaultConfig {
...
testInstrumentationRunner "com.rosshambrick.rxespresso.RxAndroidJUnitRunner"
}
TODO
- From project root in terminal run:
git submodule add https://github.com/rosshambrick/RxEspresso.git
- Add dependency to this library module in your app module's build.gradle file:
dependencies {
...
androidTestCompile project(':RxEspresso')
}
If you typically use "cold" Observables or short lived "hot" Observables, you shouldn't have to do anything special to make this work in your project.
There is one big exception. All observables must complete before Espresso will continue. (I'm still trying to figure out a solution to this). Below are some of the common use cases that will cause problems:
- You cannot use RxJava in an "event bus" style where you have an Observable that forever remains "hot" to serve up events to interested subscribers. If you are using RxJava in this manner and want to use Espresso, you will need to migrate to another event bus option. My preference is using EventBus
- You will also run into issues if you have Observables running outside the scope of the UI being tested, such as in an Android Service. The specifics of your usage will determine the level of impact. Possibly disabling the services when being tested by Espresso could solve the issue.
If you do have a situtation where the tests aren't running as expected due to a never-finishing Observable, you can:
-
Enable logging by calling
RxEspresso.setLogLevel(LogLevel.DEBUG)
somewhere in your test setup, and filter you logcat based on the TAG:RxIdlingResource
. By looking at the Object reference IDs, you can identify any that have started but not completed. -
Once you've identified one that never completes, you can enable logging with stack traces to see where this Observer subscription originates from by calling
RxEspresso.setLogLevel(LogLevel.VERBOSE)
Better support for identifying these blocking Observables will be added in a future version of RxEspresso.