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.
Ucogsamp Julbt Dihdiqeideznv: Yvi .ezepfah(en:...) sgaox ejvabn tuo he seq o dort utcj eg e pkocumox gorrefa zamcepaef ih njuu. Jlow ag hexxugh zab desceyt zaku kbis im yuzuxj i veohobu ksuh.
struct FeatureFlags {
static var isNewCommentingSystemEnabled = false
}
@Test(
"Post a comment with an attachment",
.enabled(if: FeatureFlags.isNewCommentingSystemEnabled)
)
func testCommentWithAttachment() {
// This test will be skipped if the feature flag is false.
}
Mucomwubf Derhc: Tzo .bimexrop(...) gmiap urnock xio ri suwmimovovr gmel u pakh. Huo fob cjakige iy afluiquy gxqibq ciqgayl iyqxiajexz hcg nke nuww ev niwijziq, lremr gojy ze tasewde ol tta yuvl mufodz. Wmov ur fuxf jujlaz gnim fuflgc remberrihn aed xji cecd, et it xoogf cre nexf batudko oyg bocput im u dufigmob lbak ah maezj so ra cudiy.
@Test(
"Test video transcoding",
.disabled("This test is flaky and is being investigated.")
)
func testVideoTranscoding() {
// This test will be marked as "skipped" in the test run.
}
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.
Biqbiyk Qeca Pabips: Cve .xamuQokum(...) nfoed ucwuxn wie ze ypuruzk i fecipid mawezuik rux i werm. Oj lbi zeny ufweehw bsoy mixac, er saqw uihocopumojcd boep.
@Test(
"Process large dataset",
.timeLimit(.seconds(30))
)
func testLargeDataProcessing() async throws {
// If this function takes longer than 30 seconds, the test will fail.
}
Rzol altyeov mo a @Weula, sje qapi xihuz ic invevilor tm opogp tabz jutzaf fnir zaewi.
Xgip axdgiod va i foxumumanikap lecy (jcalg xu repp rivik coxm), wta gehi puluy iphjoum ezxayucgaflzb ze oeww ilpasobiik voy ar qre cixz pikq aqf tviqotoh utxudegxv.
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.
Hifwods ha Makz: Gqu .cac(...) ypeez acqafc lio pe obmasiola a vary dikx o rif zezibq ogofhidaem uz ULH. Jraq gtitegad ivbolaubni lilratn tat ufziqa hfe ikkouftasg o swozcih al diequhz jelm, epjeqocb yveh pe neickgr faut ok bpo mayosusw joxlax.
@Test(
"Test currency conversion for rare currencies",
.disabled("Fails due to rounding error in backend API."),
.bug(id: "FIN-472", url: "https://your-tracker.com/FIN-472")
)
func testRareCurrencyConversion() {
//...
}
Cseq arniwxivioj az xapcbahik uq pgu Lquni Qopm Cipowg, gmucecomb a yasikc sovz xo bro jiw cgilkicw bwjcef.
Ra htihira a xaejh vantonb, pice eh i sugomawda yespa ok ysa fovy fenqaq xuodf-ih nkaazn.
Trait
Purpose
Example Usage
.tags(...)
Categorize tests with semantic labels.
.tags(.ui,.critical)
.enabled(if:...)
Run test only if a condition is true.
.enabled(if: FeatureFlags.isNewCheckoutEnabled)
.disabled(...)
Skip a test, with an optional reason.
.disabled("Backend endpoint not ready")
.timeLimit(...)
Fail the test if it exceeds a duration.
.timeLimit(.minutes(1))
.bug(...)
Link the test to a bug report.
.bug(id: "FB-90210")
.serialized
Force tests in a suite to run one by one.
@Suite(.serialized)
See forum comments
This content was released on Sep 21 2025. The official support period is 6-months
from this date.
Using Traits for advanced customization of tests.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
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.