如何在XCTests之间更改ApplicationConfig.plists

时间:2016-10-04 05:17:42

标签: ios swift xctest

我们的应用程序从ApplicationConfig.plist文件中指定的服务中检索内容。我需要4个XCTestCases来使用四个不同的ApplicationConfig.plist文件。最好的方法是什么?

由于这些测试都是性能测试,因此我无法在测试启动后更改服务,因此我需要在首次启动之前设置服务(通过配置文件)。

1 个答案:

答案 0 :(得分:0)

Swift辅助测试代码

// 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测试

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];

从那里,你可以使用(测试)包来获取里面的plist