如何在 XCTest 之间更改 ApplicationConfig.plists

How can I change ApplicationConfig.plists between XCTests

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

因为这些测试是性能测试,一旦测试启动我就不能更改服务,所以我需要在第一次启动之前设置服务(通过配置文件)。

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")

应用程序委托(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