Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Unit Testing with Throws in Swift 2.0

DZone's Guide to

Unit Testing with Throws in Swift 2.0

Some important things to note about functions that include a throws declaration.

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

I've been working very slowly on a Swift version of Playlist Export, and today I finally got around to updating the project to Swift 2.0.  Luckily I wrote test cases for much of the logic to manage the export process, so I had some level of confidence that I would know if I broke anything.

However, after I got the project to compile, only one of the test cases ran.  No errors or other indication why all the other cases were ignored.

I did finally figure out that if you your test case has a throws clause, it will be IGNORED.

So: 

testPlaylistNameExtension() throws {
    ...
}

will silently be ignored, which for a test case is a pretty bad scenario.  But if you handle the error in the function and change it to:

testPlaylistNameExtension() {
    ...
}

then everything is just fine.  So if you have test in Swift 2.0 that are ignored or not executed, check to see if you have a throws in the function declaration.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:
swift ,testing

Published at DZone with permission of Eric Daugherty, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}