如何开始使用空核心数据为每个UI测试断言在Swift? [英] How to start with empty Core Data for every UI Test Assertion in Swift?

查看:166
本文介绍了如何开始使用空核心数据为每个UI测试断言在Swift?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用核心数据的应用程序。应用程序的行为根据数据是否已填充而有所不同。

I have an application that makes use of core data. The behavior of the application is different depending on whether that data has been populated yet.

我曾经希望,在每个测试用例之前,核心数据将被清空,使每个测试用例在一个新的应用实例上运行。测试用例不应该依赖某些测试设备的状态,特别是因为其他测试用例操纵这种状态。不幸的是,默认情况下,核心数据不仅在测试用例之间,而且在完全独立的测试运行之间。我试图修复这种行为。

I had hoped that, before each test case, the core data would be emptied, making each test case run on a fresh app instance. Test cases shouldn't depend some test device's state, especially because other test cases manipulate this state. Unfortunately, by default, the core data lingers not only between test cases, but also between completely separate test runs. I'm trying to fix this behavior.

我的第一个想法是转储所有的核心数据在拆卸功能。这种方式每个测试将清理自己。不幸的是,我无法得到一个NSManagedObjectContext与这个命令:

My first idea was to dump all core data in the teardown function. This way each test would clean up after itself. Unfortunately, I am unable to get an NSManagedObjectContext with this command:

let context = (UIApplication.sharedApplication().delegate as! MyApp.AppDelegate).managedObjectContext!

因为它有这个编译器错误:

Because it has this compiler error:

  Undefined symbols for architecture x86_64: "type metadata accessor for MyApp.AppDelegate", referenced from: MyAppUITests.MyAppUITests.tearDown

为了解决这个错误,我尝试添加UITests到AppDelegate目标成员身份,并做一个 @testable import MyApp 。都没有工作。我需要managedObjectContext来清空核心数据。

To fix this error I tried both adding the UITests to the AppDelegate target membership and doing an @testable import MyApp. Neither worked. I need the managedObjectContext to empty the core data.

现在我想,在每个测试用例运行的开始或结束时,必须有一个更好的内置方法转储所有核心数据。它最好是在结束时,因为每个测试应该在其自身之后清理。是否有人知道如何做到这一点?

Now I'm thinking there has to be a better, built in, way to dump all core data at the beginning or end of each test case run. It would preferably be at the end, as each test should clean up after itself. Does anybody know how this could be done?

提前感谢!

推荐答案

您可以使用 XCUIApplication()。launchEnvironment = [key:val]

并检查你是否AppDelegate和解析NSProcessInfo。

and checking if you AppDelegate and parsing NSProcessInfo.

我正在努力从启动过程中的测试/发送指令获取应用程序的信息。

I am battling to get information form the app to the test / sending instruction not only during startup.

这篇关于如何开始使用空核心数据为每个UI测试断言在Swift?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆