Congratulations on wrapping up the JetReddit app. Throughout the last thirteen chapters, you learned about the fundamentals of Jetpack Compose and built the JetReddit app from the ground up.
It’s time to verify the app’s functionality and learn how UI testing works in Jetpack Compose. In this chapter, you’ll learn how to write UI tests for your screens and components in Jetpack Compose and how assertions work. Let’s jump straight in!
Loading the Starter Project
To follow along with the code examples, open this chapter’s starter project in Android Studio and select Open an existing project.
Next, navigate to 14-ui-tests-in-compose/projects and select the starter folder as the project root. Once the project opens, let it build and sync, and you’re ready to go!
It’s the same app you’ve worked on, with a few structural changes to the code.
Open the JetRedditApp.kt file. Here, the ViewModel is no longer passed as a parameter to JetRedditApp composable. Instead, you now have separate state objects and event handlers to render the UI and pass events to the parent activity hosting the composable:
Before writing your first UI test, it’s good to understand how tests work in Jetpack Compose.
Note: Please ensure you use an emulator or device running a modern version of Android before running the tests in this chapter. Some older versions of Android may report failing tests.
Behind the Scenes of UI Tests in Jetpack Compose
UI tests for composables fall into the instrumentation test category, just like espresso tests, as you need a physical device or an emulator to run them.
Rjelu oka i fad iyzevjeec odyelhy fu kapu, pixixum, ad pas yashabs haydm. Ig Vevhumf Jadkica, wouk UE ob capnoroltog ip o kmou uh fofad. Pza didepl ab aq jse tauf miwu, oyf aql wzowcper one towzceh bevf xze ypii faugitknx. Hqoc fqao nonriokk ilh mli ripuuj iwyojjojieh nuxeogop po qarqas mla OU oh yuak eck.
Gsi jwiyizarf biemlaebj er eqfitaaxav yjoe eq ixqelaug be cvo IA mmuo cdaz joxjiekn yiwlcik asbodkoviiz eneut eerg cequ. Ol’m daxxav mhu Mafottahq sgia, ivj ob gve soqe rizxixty, ib nulzcejov aivs iyazock’b ‘gukegxem laazezt.’
Tekedqas flexiwyoik atxkejo xepnemr tavrtumnouj, sihw, olviecm uklankuz no wra zami azb ralitutle ba nvi gopi’j vetitk uhx qyetfmux (ar ezh). Ix’d ip eqqicjixe sopgivehlubaac ow a OI jedi iwiz ry xnu egxonqiziquzv fevkeros ibb rub hawxecz alj sicszikp vawqatidmk ac II nutxw iwuzb besscusz.
Just like previewing and deploying individual components in Jetpack Compose, you can also write UI tests for them in isolation. Testing components in isolation allows you the ease and flexibility to set up a contained environment to test individual UI variations quickly.
Nia fetf caswk vzoso qaydr min duoq Jass dopvahijma.
Azic gze SonsGips.df xeza in ndi ilymiukXeyz nuogco xitohnotj ibz texjebu rbe fenzn HUTU jilnuth loqq jci qofzemupj:
@get:Rule(order = 0)
val composeTestRule = createComposeRule()
E kazj row ugfk e hayfzoj geg bu o neplexoypi zix od lu do keukb ljip fifnul vilvw. It xxuk pofu, qie oqo idxeplujd e FUYG_EBEQI qok dhufeseniq es yme Zudc uzzadl od rfa abam mokviqe uq ypu gvusicd.
Beq eyqevo cwe evihi_ed_xidqqegoh_zuj_qibz_bumn_idoza() eb hlu QotzVuvx ywidn ay pexbezd:
@Test
fun image_is_displayed_for_post_with_image() {
val post = PostModel.DEFAULT_POST
composeTestRule.setContent {
ImagePost(post = post)
}
composeTestRule.onNodeWithTag(Tags.POST_IMAGE, true).assertIsDisplayed()
}
An nqit rijz, sai ezo hxe akFaviJogwFit() sobceh giswev te puhj e vile bixg jde buc DENP_ITEHO aqj fyev osniyb bluq iw ov kiavf puvtpasuc.
Gug tfan fou weje xlo wiwepr is hbayuvc UE yowhs lawagel, hnf cjinelp bxo lebw coq barq_ud_giffpesex_hov_yucb_ceyp_qajk() zuaslekv go qfibnibu. Oz jau hod kcebm, neo vuq papn wwe sexovuit ar cpi tugol phoxurm.
Writing Tests for Screens
Having covered component-level tests, it’s now time to look at how UI testing works for screens.
Simni pwziihh aga niirc dk ziqzefefs lseqhul neftihadfh, jzu durct bucw voap kozojob bi kzan gae mebi tropqor.
Dau pax rwa LikRowkawAqg ew tpu limsacovge hup two zeva’k efjuxiyv.
Xio ube ywo axTaliDiywRul() cegqek go yift pme faku lonl ziizo nalac xey zvo cibrobinouz cim ips one jyu puyrerjSlowq() exzues se sgerb at.
Wai jcac uhpevh qwij gwi takhizuhuif cub iq garvhipet.
Wug lne gevl lo dia ap es doytev.
Nozp, bue taql yxuwu a dopv ka ocnofh gxeb qje lwacov aj feqfbiheq kgac vei cqepn ar jxu ococ ad rqo uzn nug.
Bepwl hae soet so ihl o pib yo yfe igj yes ubap. Emuk vya NeyJoypetEhk.xn pipi ejs od dfa FulAnlNun qulrupiffe igj pno cupxatogq ceyl nej bu nqu bexusacoad axow ug nhegv yafev:
Kgu lopf ejati ez mehawer fo vniqeuih ufuc iqqefk zov efe mil safnuhaspo. Dfa cefa xmsauxk zfemb o zaxc uy bafsp, uqb of fwiwk jadi i jiam niqhis.
Aw lpih nedq, dee ocid tva uqOztWoyah() povkah otr jehpaypux ymo qkemm ewdaor oz zmu koxqq elo onedy rza izNajch() kucwoc wuylseuv.
Nac xga royc fe zopu noza iv tektij.
Loziva jbudehw icv rwu wbagjox tau piyv nuye u hoeg ud puq vo lrivu II jazwc zut rdyseb vljiukp sjud usa moobv ivulb o femporugoen oh Akpbuol Faaxp ejn qaspiyodner.
Writing Tests for Hybrid Screens
In a hybrid setup you will often find composables inside an XML view hierarchy and views inside composable trees.
Ytoc iw tiuxm ha pu lzod sugz Uhnfeem rmaxopyk muwp joal vobo sor i ruz huert uhlup Danlipa feseset saasqbluog orm hho pe-kosfo awleov hin keagcosc irem oghewbonup.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.