-1

so I'm a seasoned iOS developer and I noticed something that I didn't notice before which is when you make a new XCTestCase class it's example test functions all has this new word "throws" at the end, and I was just wondering why (because I never added it before, I just went func testThisThing() without the "throws" at the end), and I was wondering if it's now a new standard practise? I looked at the apple docs and they seem to use it in their examples too...

So here is what a generated XCTestCase class looks like when you generate it from Xcode:

class BasicTests: XCTestCase {

    override func setUpWithError() throws {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }

    func testExample() throws {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
        // Any test you write for XCTest can be annotated as throws and async.
        // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
        // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
    }

    func testPerformanceExample() throws {
        // This is an example of a performance test case.
        self.measure {
            // Put the code you want to measure the time of here.
        }
    }

}

As you can see at the end of testExample() and testPerformanceExample() it says "throws". Why is this the new practise and what does it mean, and what does it mean if I don't include "throws"?

Cheers

jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • https://developer.apple.com/documentation/xctest/xctestcase/set_up_and_tear_down_state_in_your_tests ? If you want to test your method with a throws, you can do it if needed. Just another way to do so. – Larme Feb 20 '23 at 22:34
  • The last three comments in the `testExample()` function seem relevant to your question. – HangarRash Feb 20 '23 at 22:55
  • This is just a convenience for developers. If it didn't exist, and you had setup code that might throw (e.g. loading in a file containing test data), you'd have to handle that yourself. All the terse options would suck (`try?` would hide a potential error and be hard to find, `try!` would crash your test runner and prevent all other tests from running), so you'd need to write your own `catch` block to handle it. They do this for you. You just rethrow the error, and they have a `catch` block which will call `XCTestFail` for you, without crashing every other test suite. – Alexander Feb 20 '23 at 22:56
  • So does that mean there is no need to put "throws" after a test function declaration unless you are intending on using a try? within the function? – Morgan.J.Kennedy Feb 20 '23 at 23:07

1 Answers1

0

Declaring a test method as throws allows you to use try. This is especially useful for XCTUnwrap, XCTSkipIf, and custom helpers.

There is no penalty for declaring all test methods as throws. The downside of not doing so is having to add them back in whenever you want to try something. Why bother? Just do it everywhere.

I use throws as my standard test code snippet.

Jon Reid
  • 20,545
  • 2
  • 64
  • 95