Ramis Ramis - 1 year ago 87
Swift Question

Methods with @available(iOS 9.0, *) gets executed on 8.x

I wrote unit test. Some method is marked that supports only iOS 9v.
But some methods marked with @available(iOS 9.0, *) gets executed on device which have iOS 8.4.1. When this method executed unit test execution crashes.

Why unit test executing foo() function on iOS 8.x when it is marked for iOS 9.x?

@available(iOS 9.0, *)
class func foo() {
// Some code...
if !TargetUtility.isRunningSimulator {
// Crash happens when executed on iOS 8.4.1
parameters[kSecAttrTokenID] = kSecAttrTokenIDSecureEnclave

// In the test file:

@available(iOS 9.0, *)
func testFoo() {

Answer Source

The testing framework probably does not honor @available for test definitions. That might be caused by the fact that XCUnit is an Objective-C framework and it essentially just grabs all the methods starting with test...

Moving the @available check to runtime should do the trick, e.g.:

guard #available(iOS 9.0, *) else {
   print("Test skipped")

Unfortunately, XCUnit does not really have support for skipping tests.