Lir*_*dav 39 ios xcode-ui-testing
我希望我的应用程序在UI测试模式下运行时运行特殊代码(例如重置其状态).我查看了从UI测试运行应用程序时设置的环境变量,并且没有任何明显的参数来区分正常运行的应用程序与UI测试中的应用程序.有没有办法找出来?
我不满意的两个解决方法是:
XCUIApplication.launchEnvironment
一些变量,我稍后在应用程序中检查.这不好,因为您必须在setUp
每个测试文件的方法中设置它.我尝试从方案设置中设置环境变量,但在运行UI测试测试时不会传播到应用程序本身.__XPC_DYLD_LIBRARY_PATH
.这似乎非常hacky,可能现在只能工作,因为我们如何设置目标构建设置是巧合.jni*_*nic 52
我自己一直在研究这个问题并遇到了这个问题.我最终选择了@ LironYahdav的第一个解决方法:
在您的UI测试中:
- (void)setUp
{
[super setUp];
XCUIApplication *app = [[XCUIApplication alloc] init];
app.launchEnvironment = @{@"isUITest": @YES};
[app launch];
}
Run Code Online (Sandbox Code Playgroud)
在您的应用中:
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
if (environment[@"isUITest"]) {
// Running in a UI test
}
Run Code Online (Sandbox Code Playgroud)
@ JoeMasilotti的解决方案对于单元测试非常有用,因为它们与正在测试的应用程序共享相同的运行时,但与UI测试无关.
Cir*_*yon 10
我没有成功设置启动环境,但让它与启动参数一起工作.
在你的测试中,setUp()函数添加:
let app = XCUIApplication()
app.launchArguments = ["testMode"]
app.launch()
Run Code Online (Sandbox Code Playgroud)
在您的生产代码中添加一个检查,如:
let testMode = NSProcessInfo.processInfo().arguments.contains("testMode")
if testMode {
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
使用XCode 7.1.1验证.
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")
}
}
Run Code Online (Sandbox Code Playgroud)
然后只需在您的代码中调用 UIApplication.isRunningTest 即可。
您可以使用预处理器宏.我发现你有几个选择:
制作应用程序目标的副本,并将其用作要测试的目标.可以在代码中访问此目标副本中的任何预处理宏.
缺点是您必须向复制目标添加新的类/资源,有时很容易忘记.
复制Debug构建配置,将任何预处理器宏设置为此配置并将其用于测试(请参阅下面的屏幕截图).
一个小问题:每当您想要记录 UI测试会话时,您需要更改运行以使用新的测试配置.
添加重复配置:
用于测试:
归档时间: |
|
查看次数: |
11384 次 |
最近记录: |