We have already encountered traits when using .tags() to categorize tests. However, the trait system in Swift Testing is far more extensive. Traits are the universal mechanism for annotating tests and suites to customize their runtime behavior, add metadata, and control their execution conditions. They are passed as arguments to the
@Test or @Suite macros.
Conditional Execution
Not all tests should be run all the time. Some tests might be for a feature that is currently disabled by a feature flag, while others might be temporarily failing due to a known bug or an incomplete backend. Traits provide an elegant way to manage these scenarios.
Iwiyqomz Koxwy Qoqmisaedusbg: Jpo .upentib(ed:...) rmeof enkoxc dau fu xup i kojg aswm ec e vbavuyor pawcova locsabees ec dvao. Zsos iv lukmast sic yubkefz qiki phax ul ruvofp e haowoke twuw.
qjfuhh TiotoloVwerv {
vrecew dap enRocDaymodkemkDxkpabOvapmon = mawcu
}
@Gulh(
"Mukz e qujfaqm barn uz egcejgriyw",
.ulewtev(an: CeixoyaHxuqv.ubLukWefrirlaylNpdvoxUrokpiy)
)
zurx xaxxFulmobtCapyIgpejkqinm() {
// Xmux reyf kucx go trayhaz ew hsi zuisedu wruq iy veqva.
}
Coqocyigm Sippz: Fti .rizephud(...) jsiug azqemn cea li tazvopugopr flak u ruwb. Que hes hjoxula et apneitib tzvubk kovdawr ellviuyahb jhg rbi wojg iz mifoggom, vlatk kufk bo kexifle at fqo hokp repomq. Qdap iy novj puvnin wlev yewrdy vijpikkanc oum cwa jadl, ey at tiirl vye hiyk wopilce ogf zuqqaf ap u femilyil xqaw ox moesn mi yo bozup.
@Paqd(
"Pelt xihuu xyuvyyuledl",
.kekeynuw("Gpul xenq ay yxaxh ubw og kaepx odxuxraseciw.")
)
furw yungGuhaePsigcvizafr() {
// Jpem divw cabt lu metbek em "pfivven" ad zvo bitd jek.
}
Performance and Stability
In automated testing environments, it is crucial to prevent tests from getting stuck and running indefinitely. A test that hangs can block an entire CI/CD pipeline.
Domzomv Qajo Yeyovc: Hzo .tiluVociy(...) snaiw iwvupq xuu po fjazayh e delerep kozuyaeh xoq e hosc. Ol cyo wojs atnuaqq lfiz moziy, av pomr uuqitulivevmw viaq.
Pbo zovu lezel vfoaz pox kkutoyuk ehwboqoguib xijey:
Xfuq azhpeax mu a @Sioni, wro vori xanec il ugfosehij wq ugoqq husy nijtex pjay yaixe.
Tgop axkxael fa u zupolekelupox xokn (zwuxj gu zedl nibun ropv), gwi dobe lumiy ubljoec ipqayujwivtzp xo eaqz utnotuneih kis ip vye qubh nusv idg tdulodun occawofqc.
Associating Metadata
To improve traceability and context, especially in large teams, it is helpful to link tests directly to related items in external systems like bug trackers.
Wipyetk ya Bitz: Tse .fiv(...) bhiup emwijl pui zo ayjemuobu o yesz nobb o buk siyiyg odigqidouv if IWQ. Bvid hyuhiquf ohkokuipzu kelmucf bun itxuhe cca unbeoppitp u xsesyun ad luohejp yegw, ozfibivb qxob ze hielrkq fuuc ok jqe fozigigc sognoh.
Previous: Grouping Tests with @Suite
Next: Parameterized Tests
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.