Teresa Peters Teresa Peters - 1 month ago 6
iOS Question

How can I change ApplicationConfig.plists between XCTests

Our app retrieves content from a service specified in our ApplicationConfig.plist file. I need 4 XCTestCases to use four different ApplicationConfig.plist files. What's the best way to set that up?

Because these tests are performance tests, I can't change services once the tests start up, so I need the services to be set (via the config file) before the first launch.

Answer

Swift helper test code

// You will need to get the bundle path of the test app to pass to the main app
func getBundlePath() -> String
{
    let bundle = Bundle.main
    let bundlePath = bundle.builtInPlugInsPath! + "/" + ((bundle.infoDictionary?["CFBundleName"])! as! String) + ".xctest"
    return bundlePath
}

// Launch the app and pass it the path to the test app and the name of the plist you want to use
func launchAppWithPlist(_ plistName: String) -> XCUIApplication
{
    // Launch the app between tests
    let app = XCUIApplication()
    app.launchEnvironment = ["use_custom_plist" : plistName, "path_to_test_app" : getBundlePath()]
    app.launch()

    return app
}

Swift test

launchAppWithPlist("YourPlistFileName")

ApplicationDelegate (Objective C)

static NSString* const kCustomAppConfigKey = @"use_custom_plist";
static NSString* const kPathToTestAppKey = @"path_to_test_app";

NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *pathToConfigFile = [environment[kCustomAppConfigKey] lastPathComponent];
NSString *pathToTestBundle = [[[environment[kPathToTestAppKey] pathComponents] valueForKey:@"description"] componentsJoinedByString:@"/"];
NSBundle *bundle = [NSBundle bundleWithPath:pathToTestBundle];

From there, you can use the (test) bundle to get the plist inside