Add More UI Tests

Kotlin and Android Development featuring Jetpack — by Michael Fazio (108 / 125)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Add UI Tests | TOC | Summary and Next Steps 👉

Create the GameFragmentTests class just like you did with PickPlayersFragmentTests, adding it to the androidTest folder of your project. Then, once again add the activityScenarioRule and annotate the class with @RunWith(AndroidJUnit4::class). In addition, add a second annotation called @ExperimentalCoroutinesApi. This annotation allows us to use, without a warning, the runBlockingTest TestCoroutineScope. This is a handy scope to use (for tests only) because it allows us to call suspend functions while skipping any delays in our code. We’re using it here for our @Before function startNewGameBeforeEachTest, as running this function normally causes timing issues with starting a game and checking values.

I would have liked to use runBlockingTest in Create an Android Unit Test, but at the time of writing, there’s a bug in the library that keeps runBlockingTest from working with transactions. Maybe by the time you’re working on this it’s fixed, since it’s a better approach for automated testing, and if so, you can swap out the functions in the last chapter.

Getting back to the startNewGameBeforeEachTest, the entire function looks like this:

​ @Before
​ ​fun​ ​startNewGameBeforeEachTest​() = runBlockingTest {
​ startGame(activityScenarioRule.scenario)
​ }

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.