mannodermaus / android-junit5

Testing with JUnit 5 for Android.
Apache License 2.0
859 stars 52 forks source link

Forcefully disable concurrent test execution for ActivityScenarioExtension tests #308

Closed mannodermaus closed 11 months ago

mannodermaus commented 11 months ago

Even if parallelism is otherwise enabled, Espresso doesn't allow multiple tests to run simultaneously, and neither does the Android instrumentation. After bringing up the topic over at the JUnit 5 discussion forums, a reference to another extension with a similar use-case was given. This gave me the idea for a similar approach using ReentrantLock (ironically, the same mechanism that ActivityScenario uses to monopolize access to the Activity under test). Our use-case is much simpler than the reference implementation from JUnit Pioneer, which is nice.

With this change, if ActivityScenarioExtension detects that concurrent test execution is enabled for a UI test class, it will forcefully execute its tests sequentially anyway, using one of these ReentrantLocks. Furthermore, a warning is logged (once per test run), since this workaround will mess up the individual tests' execution times and may cause other issues that I cannot foresee right now. Still, it's great to have a solution for most people until a proper feature is built into the future of JUnit 5!