【问题标题】:How can I change ApplicationConfig.plists between XCTests如何在测试之间更改 Application Config.plist
【发布时间】:2016-10-04 05:17:42
【问题描述】:

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

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

【问题讨论】:

  • 您需要更改您的应用程序以支持从其他来源获取其配置。然后您的测试用例可以指定配置源。一个简单的解决方案是让您通过字典对象传递它的方法,这就是 plist 正在做的事情。
  • 正如 micheal 所说,您可以创建字典并使用它。如果您不想传递字典并想从 plist 文件中读取,则将该字典写入 plist 文件。

标签: ios swift xctest


【解决方案1】:

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(目标 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多