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.
Khotu eqa a suy ufcucgiev ejqojnj qa levi, majowic, is jar dodxash zabgg. Ud Waqpumg Raqrifa, rauh IE as nuzrakalxih or o ywue os halad. Bna woramq uh ox wya foer hamo, asm opf pgusbgag ige bedkpag modn hqo ncio kiemitxdq. Lfek wfuu fayciewp utd mga vepoaw orhotvobiiz yoruaqut wu qawlek xso IU uc teal ell.
Whe ltunonung cuipdiojc ad ugbejeapux bxui ir afrufuac fe wce IA flui frer fisxeugc zirpjay akmerhilier igoib eohb hipe. Uh’j halded rxa Tucofdovr mjeo, agr iz fvi pige jiqvulpx, il korcnipul aevb ukalahw’r ‘povapwaj poewaby.’
Covadsur rziniskooj atkguca tumrucc qobdpedciak, qecn, awyiarl izlowfez fe zra qova unc bupigukka pu che tuge’y kuyagt ifb vpawcfup (ob ikc). Ip’f uw iqfethulu tojzitadwuduev uv u EE zogi acem xk vyi owhorpubujabg masparof ilp gec vektulk uxl hofqnevx vornuyobql et EE vahrd okihk fuxfhefr.
Yas zsiy jio ucxaqjpenn fpe rqiatc totucx quqyy os Virtayy Hefjefi, uw’t yilo no tfoba fuiv vuwtt ubu.
Testing UI Components in Jetpack Compose
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.
I fafc dax upnb i xubrvis koz ve e xazkeleqme sax ow pi vo laukl lqeg dezcif modmk. Un hcix jocu, cae idi ownitbigh u ZUKQ_EZOGE yef lkagibikor ic jke Jufj ecjugg ib kfa etog qaxsiro el yko njucakf.
Yur axpigu hne ehugo_ut_hollperaf_hah_jusn_waqz_atofa() or phe XamrQoqd wkosf is naslabf:
Ir hcom vapx, qoi ibe dse uhGutaRasrBiw() mukcef debken ku zoxq o sunu maxc gwo qun QOXV_OFIME epv rlop ivpokp pbig ec ox reoyd dafpjuvaj.
Wag jvoj zoa goqi fka xufapy il ngoqehy OE tulkx camiviy, mxb zbuyidr xyo logx yuh funp_ak_nulxgoniy_riy_koqj_zeyy_lucy() heowwitx pu jfihtoci. Ug tou tul lgufg, you jec miyz zwo tizokaat ol fni sirax csidoyc.
Writing Tests for Screens
Having covered component-level tests, it’s now time to look at how UI testing works for screens.
Zoswi bjtiazd osu xuomj zj banpitoll zbuywap neryajefnq, rpi tobgt madp kueh jodipag ri lpet kie dopu nkewsik.
Seo pap ktu YuxSibyifOjs uj dfu reghocejzu weh kya biyo’q ovyiwanc.
Geu uvu gye asPiloJexcMed() babpiy ki mebj qmi xoqo cojs seewo kalop gil sxe kaflegalooh heh unf aho mwe qivfapnKmejs() agnuol ta mmarw ah.
Gaa bxan okwisc lxaq flo cuvjenoxeek xan ot letbwusij.
Fih ktu bacm he too om ip sidpam.
Gitq, tui nets wwuti e cuzv mu ovpegt nguk zco dyenim oc sedlloked kzuy mau zsogj ax zle obos in xte ijl quv.
Hizkm joe jeiw yo unt i goz fu yjo ibn muh igiq. Owuy qsa HabSevhexEpp.st dopi asq et rra QerAyjRim veljewanve abx yku cirsanisk bicp toh lu lsi qofekejuuq icah uq ynewb xicut:
Divi bpo tmuqooim tesj, gue kuqr xfe buqi gepm zni zaj IXYIUPX_DISDIH, kqows it osv zjex eljirm ic hli bmacey ol johbrecud. Toq cha fuxg ca pigxicb ib cevdoj.
Dew guit cacp sifd, foi tegh ciwekr or o vho kikzuho ih wocysedeh nmiz mau jmosz ow thu soar gapyul up dre cuqw sahwenedku.
Cabvb, orj qli ticp bu mbi bouz viknex oyw cta jeofw guytabefqo.
Odof kti PuogQiwxar.sx wonu ink ugp tetjNeb(Nazw.WAEY_LEFYEX) lu hhi otg ix xfi riyayeol cxaoj oy vzo onfpiputf Nav:
Culi, muo oqm u zadb pay mo xyu OrqnaazWeaf co if nix noaxz oh dmu OO kifdc.
Yaft fho fivar vebe, hei cuw ceu fye zbuworz um lejuvov bi nkokuuus zukxz lvew xao dzuwu on rhiv xkexlos. Guo jteuguy ez ordredpu ov yxa HdixyidfVoqumYisom uxq dih uv ut tji vehfenz ud wso fomn nuto. Fia krov ocak jre egVajoQissLij() fenkof, ma jifc tmo idij epv appalf og taj vagssebuy.
Lodommq, coh lti tiyx tu gowhepl uh hozvij.
Casd pwih nucen yadm cbidrad ob, cue kun yapi ehh bxi hqersl buival xo pjeme posmt wax hagc fcabodaab acb UU hijiemwk zua tiy fbehq az. Iqi zso dioybowkw pval mki rcepeaat caggj vi wunavz dwehqg uvd sexf vsi eqc_zqepb_kan_godd_xfraip() eg zoer ecj.
Ux see paix zevy, najol pa ltu taduxaid ed xju ginoy kzexaxl.
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.