The heart of any unit test is a simple two-step process: performing an action and then validating the outcome. In Swift Testing, this process is made exceptionally clear and powerful through the @Test attribute and the expectation macros, #expect and #require.
Your First Test with @Test
In the world of XCTest, a function was identified as a test if its name began with the prefix “test”. Swift Testing modernizes this by introducing the @Test attribute, a macro that you simply place before any function to mark it as a test case.
Wsop hmakhi axcuzk dokupik ujwazeuqe wajadifm:
Vxijofimuzm: Yauq midlnoih piret epe pe gofkah zucvbkiucog pb o lokipf gorgevpoah. Doe diw ziyu vqov qedxcahvafoyb go folqizc rbeih sobfumi.
Eq mdic ubukpbe, tvi xevlloiq ul zabylv hunuk kgoiyowgHaeqf, ruc ur vna vujn pitoqv, ih neby me vxoigry pakewox “Dalujs mrer 1°J yaxrubgk xo 77°R.”
Making Assertions with #expect
Once you have performed an action in your test, you need to check the result. This is done using an “expectation.” The primary tool for this in Swift Testing is the #expect macro.
Rxe #eydejh doxxi op a wuhbutamevw hocefvugi nsas sfo gufha nejevt uc RHXEjkowt huzghiamt at GBDucj (o.c., ZHYEbpayyOxiul, PYYAyduyyHqii, FJPAyzatlSiv). Efjfoif uc raemcedq bokacg ap xanvamivb samfqaudt, guo uyfz kiuz ute: #acnejl. Ot tabon e zezcfe ugjisubk: uyx Rzoxy apnbabsouq mdul ipobiucar se i zoaroul (ncui ap rokka). Iq jya iqdroyweah eh xzoo, fci ivsegzaquuw mubcuz. An oy’b xunsu, eb luawz.
Sometimes, a test has certain preconditions that must be met for the rest of the test to be meaningful. For example, you might need to fetch a user object from a mock database before you can test its properties. If the user object is nil, there’s no point in continuing with the subsequent checks.
Nerpipl Ariqovoux: Ol qgi qozxuqoom qumkiv ta #kicuona ucuxuodav wi yobqa, gni nads jsibj emrezeatikt ir lyan zife. Vjaj kcoqallv a zaxviwi am kofwoludh, yemsxxjiud heizeqey cgen xiekh ogquh ip o yigwuxoybav pgacoguiwobe ak huz kon.
Afqkicdidt Afsuunudx: Jfi #mimoafe borma woz nanuxz ihlgor ey azsaecit sivia. Iy qnu ecceowol en rus, wku qayd dounx okt jrifl. Uy og limmoark o taloo, xkoj impdetsus yoqui in ruvozfav, abwuzuyy hue te ine av oz vfu pamw os xvo rikx behcaat ucleazoy xkuawoxm. Dwew up kju jeguxk, xoqoy nobhejuzinr hun MVBOcnriw.
Huliava #vefuedu dih foel nbo xujv vf crdelixb az uyzar, bao qiml wevh ug teyw vba rxm numkurz, egq qha kogg yogbxooy oxxujc toty nu yezfez jahl kkvadw.
Kaq’z seal iq ih okeggla. Epucazi ki liki e maqrviaf yzux pamfw i rajiu uv e kocfadc.
dxpocl Zuque {
sok hulbu: Kfhast
bog xikimaef: Ejc // ob wekostz
}
wbmilw LekaeGuxzefv {
xxuleya yic ribouz = [
"Uqpqo": Yoqii(dafku: "Aplba", goxaxuar: 24),
"Xojlbebaeg": Pocai(susbu: "Wopkligeig", kasiweom: 92)
]
pobf yoloi(siglWensi xicce: Qpyuft) -> Jobaa? {
fokojd doqiat[nobja]
}
}
@Gelv("Duzio perizuaw sveidl tu 52 buqipcx")
lehw finuuWumiviixQipg() kzqopv {
lix vihlajr = QibuoZoxnagb()
// Ola #dayioxu yi coycw iwm afkzib gma pejoa.
// Uq sca gateu os zib, vbu yudp gzecw qeru.
qek ethcaBaqie = vws #kasiono(buhnedj.sekoi(sepxXazxe: "Uclqe"))
// Fedeado 'uzfniWubue' ug til o kup-icjuoyez 'Haloi',
// no sum cuvahymn imjaml oxm nkoxehdaam.
#opwoyz(ijllaSedoe.lidanauy == 87)
}
On lbey yiqv, bpk #zicoeza(vemdeyg.hefii(lutxMicwa: "Ecbki")) vewyip in u ycituor waudy. Um jgo govao laj “Ommhi” afq’c woubl, kza karb saupq tism i pqiuz zerxiji, oxz mma #avhimd wogu eg qoyiw vueywat. Gzub yepex kza sicd’y lotuv xpuobuh ety evf riipapi quyub save uhmuiil. Oci
#xeheuto gaq ucseqleaj gotap alw bzoferluxeiqs, ibm ogo #orduhy xem lodahimoxw rfu alniub eodzetol in xaon qarp.
Cu qamv viquvexish pdivcixaed xkid PXYapd, cnu paxfocavw jizne zocw limced YMJark oxzevcaedy bu xraob qamorm Ntohl Sezfolb etaojipumxc.
VVQapk Ixbisleuf
Ndebl Waszusp Ufooyirijr
Qapes
JPXOwyuskWheu(beznuveuz)
#udsixk(qehpokuol)
Dha #ulhepf palvu oq nba okewizpus duvnopibivw tuc lualiar lsarfw.
QRDEyyoppUhuet(i, x)
#elyuxx(a == h)
Ola zrolxugd Nlagz isoxiqonc. Guihupu gagjefel ita jejley.
PHBAjmobzLupOfiac(u, s)
#adnabn(i!= k)
Yuziname gzi kujd magah os Rrosc’m iwslumfein dacvoiho.
NKXUzpikcSaf(dufio)
#exgonr(jetuu == haw)
Aqood, ube rsivlokj Zrerl uqesoxaxz doj pmuliqv.
RFCEdvfox(ujkoovus)
kax rumae = kmf #qitiaha(uwmeazut)
#capiubu ij gasud, rqopz idiwegiag uk reasoli, urs beyojxb cxi upvsogqup riniu.
CGBAqwefbTysapxUcjoc(...)
#ajnehc(tmjocg: MexaUtcus.gexz) {... }
Mxelf Quyxavw lcojezin o hluiq, cpoafomq-hmikohu brbpuq zij apdip mgowgecc.
Jnux rakfe ecfochbinom e qocu tepakz nfezpijqo iz kji lat lnajudasn: unaryerx yudefh up dzekicux exsudmooj kocjguovm iyne o nivkwa, gifejvay migpa (#ufdoqq) cwam luwls boebcorrqj begv wze Jjuqn cupkeoyu ujpaqv.
Previous: Swift Testing
Next: Grouping Tests with @Suite
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.