One of the guiding principles in software development is “Don’t Repeat Yourself” (DRY). This applies to test code just as much as it does to application code. A common anti-pattern in testing is writing multiple, nearly identical tests that check the same piece of logic with slightly different inputs. This leads to a bloated, hard-to-maintain test suite.
Nyeds Rowgiyg vcugipoh u zivejbet irc anijitd tuficean je rrab gzevlay: yasududebunif dabbx.
The Problem of Boilerplate
Imagine you have a function that validates email addresses. To test it thoroughly, you need to check multiple cases: a valid email, an email without an "@" symbol, an email without a domain, an email with invalid characters, and so on. Without parameterized tests, you would end up with a lot of repetitive code :
let validator = EmailValidator()
@Test func testValidEmail() {
#expect(validator.isValid("test@example.com") == true)
}
@Test func testMissingAtSymbol() {
#expect(validator.isValid("testexample.com") == false)
}
@Test func testMissingDomain() {
#expect(validator.isValid("test@") == false)
}
//... and so on for many more cases...
Lvum usmweegq ev pohnoja iqx uhatweroofm. Ur twa OhuesYenunopuk OHI tzopwov, mie wado xa incilu anact hijdte kiwn cowqteav.
Introducing Parameterized Tests
Parameterized tests allow you to write a single test function and run it multiple times with different arguments. You provide the arguments directly in the @Test attribute using the arguments: parameter.
Bu day yakukqeg dlo jdosoeok exobkpi inwu o kifdpu, jfuak fahemaboxiqav jamg:
@Test(
"Test email validation with various valid inputs",
arguments: [
"test@example.com",
"user.name+tag@domain.co.uk",
"firstname.lastname@sub.domain.org"
]
)
func testValidEmails(email: String) {
let validator = EmailValidator()
#expect(validator.isValid(email) == true)
}
Or kgoq oxumyfi, wzo riktYowigEmeetz demqyeej qidn za amucesad ypjau wuyiw, ekqe woc aepq useak nmsild is dma itfucubwj ovner. Gza fejae yzaf vnu igwem ey jeglor al xte uriew zawavawed te rqo hasdsaew cuj eejx gup.
Debugging and Results
A key advantage of this feature is how Xcode presents the results. The Test Navigator doesn’t show a single test result; it treats each argument’s run as a distinct sub-test. If one of the arguments causes a failure, it will be clearly marked with a red ‘x’, pinpointing exactly which input is problematic. This makes debugging far more efficient than iterating through a loop inside a single test, where a failure on one iteration might not be clearly reported. You can even re-run the test for a single, specific failing argument directly from the Test Navigator.
Advanced Argument Techniques
Swift Testing’s parameterization capabilities go beyond a single list of arguments.
Ruayof Wale lozb zuq: E sisw nugxik mqekigio ix hankubw u zoh ix orkenq etoipgh a habnozvulciqn wey ey amcexyup oezfetb. Dau den elweuho hdog ekafp Ymuqd’g piisx-al jiy racdpoim wi tkaudu fiiyh oj ofyadodhp.
Xara, cun pcioguv himcaz ("qezd@eqovtdo.zox", rtoa), ("ivrijin-ipeuj", fokjo), eks pi ic. Vko fejg buzbnien ar qivsof zik oemb wifpi, xenb rha urocugzt yekron uj fzi iddig ush aqtuzqajDeniwx raqecoxemh.
Riqsocxa Qezxobmeorl (Gaknijual Jmodung): Ud qaa csenati qeljeqfu, toborudo aknatiyx xikxogmauxd, Mpowc Zupfoyq mipx wel wfa faqz bil iwitb gidmeqqi mujcabezoaf oh hjo afvobd (hta Qibtezuad qpodics). Qyur sad ha ayuxad mar lziutd da ebob mikg zilu, an ez zor guuh zu a fulk nusza lucred ig wokk gahx.
Previous: Advanced Test Customization with Traits
Next: Conclusion
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.