It is impossible to test runtime exceptions in Swift

Number:rdar://17126888 Date Originated:04-Jun-2014 05:16 AM
Status:Closed: Duplicate of 16453199 (Open) Resolved:
Product:Developer Tools Product Version:
Classification: Reproducible:
Swift tests do not have access to the XCTAssertThrows family of macros. In addition, Swift does not provide a try-catch mechanism. This makes testing any code that raises a runtime exception impossible.

Steps to Reproduce:
1. Define a function that asserts a user is logged in:

func assertUserIsLoggedIn(isLoggedIn: Bool) {
	if(!isLoggedIn) {
			format: "Programmer error. This function should only be called after authentication.",
			arguments: CVaListPointer(fromUnsafePointer: UnsafePointer()))

2. Attempt to write a unit test that confirms that the function behaves as expected. That is, the test should pass if the function raises when given a false value as a parameter.

Expected Results:
Expected (1):

Using XCTAssertThrows, I am able to test the function:

XCTAssertThrows(assertUserIsLoggedIn(false), "expected function to raise")

Expected (2):

Using try-catch, I am able to test the function:

var raised = false
@try {
} @catch {
    raised = true
XCTAssertTrue(raised, "excpected function to raise")

Actual Results:
There is no way to test the function. The following test will fail:

class UserAssertionTests: XCTestCase {
    func testAssertUserIsLoggedInRaisesWhenNotLoggedIn() {
		assertUserIsLoggedIn(false) // Exception causes test failure.

Xcode Version 6.0 (6A215l), OS X 10.10 (14A238x)

In WWDC 2013, Apple developers appeared to be enthusiastic about improving unit testing and continuous integration in Xcode. But without a way to test runtime errors, developers writing in Swift are severely limited in how they can test their applications.

Related thread on the Apple Developer Forums:

This problem does not exist when writing tests in Objective-C.


Apple's Reponse

Engineering has determined that your bug report is a duplicate of another issue and will be closed.

The open or closed status of the original bug report your issue was duplicated to appears in the yellow "Duplicate of XXXXXXXX" section of the bug reporter user interface. This section appears near the top of the right column's bug detail view just under the bug number, title, state, product and rank.

If you have any questions or concerns, please update your report directly here:

About your answer

Dear "Apple's Reponse" (I guess you tried to type "Response"),

Your "response" is as useful as giving a bucket full of gasoline to a fireman trying to extinguish a fire. Perhaps next time, you could provide a link to the duplicate issue you speak off, which would be more useful.

When people take their time to write these posts they are already on last ropes. They are tired, frustrated and literally are screaming for someone to help them.

I am glad that this person isn't about to fall to the grand cannon because the conversation with you would go like this:

Person> HELP! I can't pull myself up! HELP!!!

Apple> What do we have here? Are you asking for help?

Person> Yes, please! Help me out! I can't hold for much longer

Apple> Engineering has determined that your request for help is a duplicate so I will ignore you.

Person> What?! Are you kidding me?

Apple> Next time, please don't scream for help. If you have any question or concern...

Person> I am concern that I am about to fall and I am asking what is wrong with you!

Apple> Engineering has determined that your concern and question are duplicates of another issue. We could point you to the right direction for salvation; however, we have decided to give you the finger.

By B.Gesiak aka "I don't care" at June 13, 2014, 9:17 a.m.

By axllaruse at May 15, 2015, 7:27 p.m. (reply...)

Don't shoot the messenger

Hey, I just copy-and-pasted Apple's response to me (I'm B. Gesiak, I filed the original Radar) and submitted it as a comment. I did so in case people reading this were curious whether this Radar was resolved or not. I understand you're frustrated, but it ain't my fault! :)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!