iOS Swift: Separate AppDelegate for XCTest

前端 未结 3 810
感动是毒
感动是毒 2021-02-19 04:27

Due to a couple issues, I want the XCTest target in a project to run a separate app delegate. Using ObjC, this was a relatively straightforward process: manipulate main.m<

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-19 05:17

    It's strongly unrecommended to add conditions to normal code checking if its being tested. Instead you should mock your AppDelegate in tests to do whatever you want.

    Then you could replace delegate of UIApplication is setUp in super class of your each XCTestCase'es.

    class MockAppDelegate:NSObject, UIApplicationDelegate {
    
    }
    
    
    class BaseTest: XCTestCase {
    
        override func setUp() {
            super.setUp()
            UIApplication.shared.delegate = MockAppDelegate()
           }
    }
    class Test1: BaseTest {
    
        override func setUp() {
            super.setUp()
            // normal testing
           }
    }
    

    If you still want to stop code execution for tests this is my method that works well:

    You can add startup parameter to app which indicates that this is test run App Start execution

    These parameters are accessible from NSUserDefaults

    #define IS_TESTS [[NSUserDefaults standardUserDefaults] boolForKey:@"TESTING"]
    

提交回复
热议问题