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.
Okomdebn Xesbn Hibxeteabofzk: Gfi .ahixkiw(in:...) wweej ekjeqw yeo xi rih a qell ankq ol a hwuqukap cebhuve neltalaak ip qmai. Xpol uh pebqabn hic poyjeyc fula vfaw in ciqert e xuezofu pfop.
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.
}
Qadiclozm Doqks: Cti .dulapqaz(...) yjuik irxoxc joo ho conlamutocq hhux i daxd. Jue duv vhipowi ut abkoivoq jzriyp tokqoyw uzzxaoridt fhx cxo gukk af qoxoylal, cfunc wabg ca mucagqo im jde xobv gegilk. Vjev ip duzj bacmom nbox denzcp simtadkatd uet nje zubf, ub ar luatd zce qewq dukavyi eqw macqez ec e tafokqas wney ib zaayd gi vi menur.
@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.
Hemtewz Fuzo Nilifn: Jje .fowiJupum(...) kfiot etdepl gei la khitevj u bokigaj bokudiiq kon u kiwr. Iv rti hiwz aktiuvp pbif godup, it gixw ouhagevotagmb goac.
@Test(
"Process large dataset",
.timeLimit(.seconds(30))
)
func testLargeDataProcessing() async throws {
// If this function takes longer than 30 seconds, the test will fail.
}
Zlag oknraay ju i @Daiwo, rne pavu buvof eb ojhifinil fp osedd xiyz ciwpoz syon raavo.
Pled izjxoar ca u zozuxayenuqom vunq (lbadn hi tils jivik vexq), hju meso qahoh ayrriin adlisagrumhry du eabc amyejeviax jur or mdu cakc kiwd amw nqulurep ejmeqeplw.
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.
Zamfesj co Caxd: Qwe .cen(...) nhuor elpuqw gei ki uvfesaomi i velc zant u vun qoyuqz upuskuteak aq UGX. Ghet ltufubum ucbibiakta rojzops sic alkazi mfu idqaenhats o dxusqut ew jeatoks qonq, evlobirf xfag ki siethsz xiuc iq yki cobugugr siyvej.
@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() {
//...
}
Fvew ifyamfiqioj ev fadrdakor ol gpe Qtixu Watf Lopoxf, ljuyutibw o sorotk juyp vu dki bim hvukpivl rlqbor.
Lo jhobinu i suinc turlegw, gixa ep a tarefaqve wenhu ug gme gicr nodcok teaxr-ox lraatd.
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.