我希望我的应用程序在UI测试模式下运行时运行特殊代码(例如重置其状态)。我查看了从UI测试运行应用程序时设置的环境变量,并且没有任何明显的参数来区分正常运行的应用程序与UI测试中的应用程序。有没有办法找出来?
我不满意的两个解决方法是:
XCUIApplication.launchEnvironment
的一些变量,我稍后在应用程序中检查。这不好,因为你必须在每个测试文件的setUp
方法中设置它。我尝试从方案设置中设置环境变量,但在运行UI测试测试时不会传播到应用程序本身。__XPC_DYLD_LIBRARY_PATH
是否存在。这似乎非常hacky,可能现在只能工作,因为我们如何设置目标构建设置是巧合。我自己一直在研究这个问题并遇到了这个问题。我最终选择了@ LironYahdav的第一个解决方法:
在您的UI测试中:
- (void)setUp
{
[super setUp];
XCUIApplication *app = [[XCUIApplication alloc] init];
app.launchEnvironment = @{@"isUITest": @YES};
[app launch];
}
在您的应用中:
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
if (environment[@"isUITest"]) {
// Running in a UI test
}
@ JoeMasilotti的解决方案对于单元测试非常有用,因为它们与正在测试的应用程序共享相同的运行时,但与UI测试无关。
我没有成功设置启动环境,但让它与启动参数一起工作。
在你的测试中,setUp()函数添加:
let app = XCUIApplication()
app.launchArguments = ["testMode"]
app.launch()
在您的生产代码中添加一个检查,如:
let testMode = NSProcessInfo.processInfo().arguments.contains("testMode")
if testMode {
// Do stuff
}
使用XCode 7.1.1验证。
我刚添加了此扩展程序
@available(iOS 9, *)
extension XCUIApplication {
func test(){
launchEnvironment = ["TEST":"true"]
launch()
}
}
所以我可以使用test()而不是launch()
Swift 3基于之前的答案。
class YourApplicationUITests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
let app = XCUIApplication()
app.launchArguments = ["testMode"]
app.launch()
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// Use recording to get started writing UI tests.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
}
extension UIApplication {
public static var isRunningTest: Bool {
return ProcessInfo().arguments.contains("testMode")
}
}
然后在代码中调用UIApplication.isRunningTest。
在Swift 3中,您可以检查XCInjectBundleInto
键,或以XC
开头的内容。
let isInTestMode = ProcessInfo.processInfo.environment["XCInjectBundleInto"] != nil
这也适用于OS X.
我的解决方案几乎与上面的that of Ciryon完全相同,只是对于我的macOS基于文档的应用程序,我必须在参数名称前加一个连字符:
let app = XCUIApplication()
app.launchArguments.append("-Testing")
app.launch()
...否则,“测试”最终被解释为启动应用程序时要打开的文档的名称,因此我收到错误提示: