Apple在Xcode 7中引入了新的UI测试,但是每当测试启动应用程序时我都会遇到困难,它从应用程序之前的数据开始。这意味着测试不能独立,并且可能受到其他测试的影响。
无法访问用户默认值和其他数据,因为运行测试的应用程序无法访问已测试应用程序的捆绑包。脚本也是不可能的,因为它们可以在测试之前或之后运行。并且无法在iOS上执行NSTask以在每个测试套件之前运行脚本。
有没有办法在每个测试套件之前重置应用程序数据?
答案 0 :(得分:21)
不是直截了当的方式。但是有一些解决方法。
adb
可以设置可以改变应用程序行为的命令行参数和环境变量。
XCUIApplication
文件的一个简单示例:
main.m
在int main(int argc, char * argv[]) {
#if DEBUG
// Reset all data for UI Testing
@autoreleasepool {
for (int i = 1; i < argc; ++i) {
if (0 == strcmp("--reset-container", argv[i])) {
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSFileManager *fm = [[NSFileManager alloc] init];
for (NSString *path in folders) {
[fm removeItemAtPath:path error:nil];
}
// Also remove documents folder if necessary...
}
}
}
#endif
@autoreleasepool {
return UIApplicationMain(argc, argv, nil,
NSStringFromClass([AppDelegate class]));
}
}
添加:
-[XCTestCase setUp]
答案 1 :(得分:6)
如果在$comment=mysqli_real_escape_string($db,$_POST['comment']);
内为UITests准备应用程序是可以的,那么您可以执行以下操作:
在扩展application:didFinishLaunchingWithOptions:
的测试类的setUp()
方法中添加以下代码:
XCTestCase
然后,在let application = XCUIApplication()
application.launchEnvironment = ["UITESTS":"1"]
application.launch()
中,您可以使用以下代码检查标记:
application:didFinishLaunchingWithOptions:
当然,如果这是你的选择。
注意:不是将func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
let env = ProcessInfo.processInfo.environment
if let uiTests = env["UITESTS"], uiTests == "1" {
// do anything you want
}
// further set up code
}
设置为"1"
标志的参数,而是可以为不同的测试用例指定不同的值 - 甚至是测试方法(但在这种情况下,您应该从test启动应用程序)方法,而不是"UITESTS"
)
注2:我建议将处理该标志的代码包装到setUp()
块中。
答案 2 :(得分:4)
我必须使用一些私有标头重置应用程序数据才能访问跳板和设置应用程序。
首先,我添加了一个Run脚本阶段,以便在测试开始时将其删除:
/usr/bin/xcrun simctl uninstall booted com.mycompany.bundleId
之后我使用我编写的here解决方案,使用在tearDown
调用上运行的测试脚本将其删除,以便在每次测试后重置它。
答案 3 :(得分:0)
在我的情况下,我还需要重置权限。还可以选择删除您的应用并重置系统权限,只需让测试删除应用并导航到设置即可。
这个S.O.已经回答了线程:Is there a way to reset the app between tests in Swift XCTest UI in Xcode 7?