是的,似乎方案中提供的所有环境变量和启动参数在 XCTest 测试中都被忽略。
但是,您可以在测试中以编程方式设置语言,例如setUp()
method:
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
let app = XCUIApplication()
app.launchArguments += ["-AppleLanguages", "(en-US)"]
app.launchArguments += ["-AppleLocale", "\"en-US\""]
app.launch()
}
现在,您可以扩展这种方法并执行类似的操作Snapshot does https://github.com/fastlane/fastlane/tree/master/snapshot#how-does-it-work:
必须将 2 件事从快照传递到 xcodebuild 命令
直线工具:
最后,为了更改基于架构的语言,您可以执行以下操作:
1. 为测试编写一个创建临时文件的预操作脚本:
mkdir -p ~/Library/Caches/xcode-helper
echo "en-US" > ~/Library/Caches/xcode-helper/language.txt
2.加载文件setUp()
并设置应用程序语言:
override func setUp() {
super.setUp()
let app = XCUIApplication()
let path = NSProcessInfo().environment["SIMULATOR_HOST_HOME"]! as NSString
let filepath = path.stringByAppendingPathComponent("Library/Caches/xcode-helper/language.txt")
let trimCharacterSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()
let language = try! NSString(contentsOfFile: filepath, encoding: NSUTF8StringEncoding).stringByTrimmingCharactersInSet(trimCharacterSet) as String
app.launchArguments += ["-AppleLanguages", "(\(language))"]
app.launchArguments += ["-AppleLocale", "\"\(language)\""]
app.launch()
}
从现在开始,Xcode 将使用方案的预操作脚本中指定的语言/区域设置运行测试。
UPDATE
事实证明,测试确实如此not忽略方案中提供的参数。这些参数实际上传递给测试本身,而不是传递给被测试的应用程序。这可能出乎意料,但却是有道理的。
话虽如此,您所需要做的就是:
1. Set -AppleLanguages (en-US)
and -AppleLocale en_US
方案中测试的启动参数
2. 将测试中的启动参数传递给XCUIApplication
调用前的实例launch()
method
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
let app = XCUIApplication()
app.launchArguments += NSProcessInfo().arguments
app.launch()
}