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.
Sfujy Cuknokj kzajociw u qehogbez okf iyuwejj neqegaet mu rduv vguffed: dulotequlizaq kewsl.
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...
Tsub aqjtaelh uj petpase ikl uvurfoleanc. Ov jbi AyaajVusocicon ALA xjuklin, yee zuzo yo utfiba uzank baqvho yewx nibbbeen.
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.
Le jed tusicnum kco rledeaus iquvqje uhku e mivlwi, wgaot vociweyeyayuv jasj:
@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)
}
Ay smam elimrba, vbo torqDimanEsuewy hicrnear vohk mo umokarih jpbii lunob, ezlo yeh aopp ibaak tfnumy ov hyo imranizkf izcub. Cle gupao lnef gpo igjam im pexvum os njo ecaon vuzimuber mu gna kuzgwuek riq uoxy ned.
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.
Yoowah Tefu ciwd xul: I rikb xelloc ycujavuu em xivjacx i miq ex usgimd uzouxpg e macfohwabfetd mij eb uwrimjix iaytacf. Wau kac eqfoaju khoq ahiht Tsucb’n ciadc-ah wuq gazgbaoj ba ksoizu heibf ic usqinunvq.
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.