Swift Unit testing with XCTAssertThrows analogue

Is there any equivalent to check for throwing exceptions in swift language Unit tests?

For example I Have a class:

  • iOS 8 - change character spacing on UILabel within Interface Builder
  • iOS 11 UISearchBar in UINavigationBar
  • Select UITableView's row when tapping on its UISwitch in Swift
  • How to use an Swift object method as a closure?
  • Write and read file with list NSDictonary swift
  • Swift UITextFieldShouldReturn Return Key Tap
  • class Square : NSObject{
        let sideLength: Int
        init(sideLength: Int) {
            assert(sideLength >= 0, "Wrong initialization of Square class with below zero side length")
            self.sideLength = sideLength

    and Test to check it work. In objective C I can write test method like this:

    - (void)testInitializationWithWrongSideLengthThrowsExceptions{
       XCTAssertThrows([[Shape alloc] initWithSideLength: -50], "Should throw exceptions on wrong side values initialisations");

    What is Swift equal technic?

    5 Solutions Collect From Internet About “Swift Unit testing with XCTAssertThrows analogue”

    If you add the following three files to your tests:

    //  ThrowsToBool.h
    #import <Foundation/Foundation.h>
    /// A 'pure' closure; has no arguments, returns nothing.
    typedef void (^VoidBlock)(void);
    /// Returns: true if the block throws an `NSException`, otherwise false
    BOOL throwsToBool(VoidBlock block);
    //  ThrowsToBool.m
    #import "ThrowsToBool.h"
    BOOL throwsToBool(VoidBlock const block) {
        @try {
        @catch (NSException * const notUsed) {
            return YES;
        return NO;
    // xxxTests-Bridging-Header.h
    #import "ThrowsToBool.h"

    Then you can write:

    XCTAssert(throwsToBool {
       // test code that throws an NSException

    But it doesn’t work for assert or precondition 🙁

    PS I got the idea from: http://modocache.io/xctest-the-good-parts

    I think the assert()-function should only be used for debug-purposes. Not only because of the following statement from Apple’s Swift-Book (https://itun.es/de/jEUH0.l):

    „Assertions cause your app to terminate and are not a substitute for designing your code in such a way that invalid conditions are unlikely to arise.“

    Thats why I would solve this as follows:

    import Cocoa
    import XCTest
    class Square
        let sideLength: Int
        init(_ sideLength: Int)
            self.sideLength = sideLength >= 0 ? sideLength : 0
    class SquareTests: XCTestCase
        override func setUp() { super.setUp() }
        override func tearDown() { super.tearDown() }
        func testMySquareSideLength() {
            let square1 = Square(1);
            XCTAssert(square1.sideLength == 1, "Sidelength should be 1")
            let square2 = Square(-1);
            XCTAssert(square2.sideLength >= 0, "Sidelength should be not negative")
    let tester = SquareTests()

    There is no equivalent to XCTAssertThrows in swift. For now you can’t use a native function, but there is a solution with some objective-c help. You can use Quick, or only Nimble. Or to make your own assert function – see this article – http://modocache.io/xctest-the-good-parts – Potential Improvement #2: Add XCTAssertThrows to Swift

    the best way is add a bridge I think.

    please look at https://gist.github.com/akolov/8894408226dab48d3021

    it works for me.

    correct way in Swift 2:

    class Square : NSObject{
        let sideLength: Int
        init(sideLength: Int) throws { // throwable initializer
            guard sideLength >= 0 else { // use guard statement
               throw ...// your custom error type
            self.sideLength = sideLength

    and testing:

    func testInitThrows() {
            do {
                _ = try Square(sideLength: -1) // or without parameter name depending on Swift version
            } catch ... { // your custom error type
            } catch {