iOS 单元和 ui 测试中忽略了方案语言设置
Scheme language setting ignored in iOS unit and ui tests
我的最终目标是发布
xcodebuild test
从命令行为不同的语言选择不同的方案。
目前我有两种方案,唯一不同的是应用语言。在一个方案中是英语,在另一个方案中是西班牙语。
如果我使用 Xcode 到 运行 应用程序它运行良好,它使用我选择的方案中指定的语言启动,EN 或 ES 都可以。
如果我 运行 来自 Xcode 的测试,语言设置将被忽略。无论我选择哪种方案,都没有关系,它始终显示为设备语言。模拟器上也一样。 运行ning 测试时相同
xcode构建测试
采摘方案。 (在方案中添加回显命令可确保选择正确的命令)
在方案编辑器中选中“使用 运行 操作的参数和环境变量”。
我做错了什么?
谢谢。
是的,在 XCTest 测试中似乎忽略了方案中提供的所有环境变量和启动参数。
但是,您可以在测试中以编程方式设置语言,例如在 setUp()
方法中:
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:
的操作
2 thing have to be passed on from snapshot to the xcodebuild command
line tool:
The device type is passed via the destination parameter of the
xcodebuild parameter
The language is passed via a temporary file which
is written by snapshot before running the tests and read by the UI
Tests when launching the application
最后,为了根据模式更改语言,您可以执行以下操作:
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 将运行 与方案的预操作脚本中指定的 language/locale 进行测试。
更新
事实证明,测试 而不是 会忽略方案中提供的参数。这些参数实际上传递给测试本身,而不是传递给被测试的应用程序。这可能出乎意料,但它是有道理的。
话虽这么说,您需要做的就是:
1。为方案
中的测试设置 -AppleLanguages (en-US)
和 -AppleLocale en_US
启动参数
2。在调用 launch()
方法
之前将测试中的启动参数传递给 XCUIApplication
实例
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()
}
我的最终目标是发布
xcodebuild test
从命令行为不同的语言选择不同的方案。
目前我有两种方案,唯一不同的是应用语言。在一个方案中是英语,在另一个方案中是西班牙语。 如果我使用 Xcode 到 运行 应用程序它运行良好,它使用我选择的方案中指定的语言启动,EN 或 ES 都可以。
如果我 运行 来自 Xcode 的测试,语言设置将被忽略。无论我选择哪种方案,都没有关系,它始终显示为设备语言。模拟器上也一样。 运行ning 测试时相同 xcode构建测试 采摘方案。 (在方案中添加回显命令可确保选择正确的命令)
在方案编辑器中选中“使用 运行 操作的参数和环境变量”。
我做错了什么?
谢谢。
是的,在 XCTest 测试中似乎忽略了方案中提供的所有环境变量和启动参数。
但是,您可以在测试中以编程方式设置语言,例如在 setUp()
方法中:
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:
2 thing have to be passed on from snapshot to the xcodebuild command line tool:
The device type is passed via the destination parameter of the xcodebuild parameter
The language is passed via a temporary file which is written by snapshot before running the tests and read by the UI Tests when launching the application
最后,为了根据模式更改语言,您可以执行以下操作:
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 将运行 与方案的预操作脚本中指定的 language/locale 进行测试。
更新
事实证明,测试 而不是 会忽略方案中提供的参数。这些参数实际上传递给测试本身,而不是传递给被测试的应用程序。这可能出乎意料,但它是有道理的。
话虽这么说,您需要做的就是:
1。为方案
中的测试设置-AppleLanguages (en-US)
和 -AppleLocale en_US
启动参数
2。在调用 launch()
方法
之前将测试中的启动参数传递给 XCUIApplication
实例
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()
}