Unit Testing in Xcode, does it run the app?

后端 未结 10 977
陌清茗
陌清茗 2020-11-29 21:54

I\'m running into a strange problem that I haven\'t run into before.

When you do cmd+U to run your Unit Tests (OCUnit for example) does it actually call the main.m,

10条回答
  •  庸人自扰
    2020-11-29 22:03

    If you're using Swift (you probably don't have a main.c), you have to do these steps :

    1: remove @UIApplicationMain in AppDelegate.swift

    2: Create an empty TestingAppDelegate.swift

    import UIKit
    class TestingAppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?
    }
    

    3: Create a file called main.swift :

    import Foundation
    import UIKit
    
    let isRunningTests = NSClassFromString("XCTestCase") != nil
    
    if isRunningTests {
       UIApplicationMain(C_ARGC, C_ARGV, nil, NSStringFromClass(TestingAppDelegate))
    } else {
       UIApplicationMain(C_ARGC, C_ARGV, nil, NSStringFromClass(AppDelegate))
    }
    

提交回复
热议问题