在 UITest 中模拟推送通知
Simulate Push Notification in UITest
我们想在 UI 测试中测试推送通知的接收(当应用程序处于活动状态时)。发送真实推送通知的 goto 解决方案听起来有点矫枉过正(并且只适用于真实设备):参见 http://www.pixeldock.com/blog/testing-push-notifications-with-xcode-uitests/
我的问题是:是否可以(直接或间接)从 (UI) XCTestCase
调用 UIAppicaltionDelegate 的 didReceiveRemoteNotification
?
您不能从 UITest 调用应用程序代码中的函数。 UITests 运行 在单独的进程中,无法访问您应用的功能和方法。
来自 Apple 的文档:
UI testing differs from unit testing in fundamental ways. Unit testing
enables you to work within your app's scope and allows you to exercise
functions and methods with full access to your app's variables and
state. UI testing exercises your app's UI in the same way that users
do without access to your app's internal methods, functions, and
variables. This enables your tests to see the app the same way a user
does, exposing UI problems that users encounter.
在 UnitTest 中可以从测试中直接调用 didReceiveRemoteNotification
。我不知道您的应用如何处理推送通知,但如果您可以使用单元测试来测试所需的结果,那可能是一个有效的替代方案。
您可以从 UITest 中触发应用中的事件。我之前通过将按钮放在我的视图上来完成它(一个限制是我必须将按钮添加到我希望事件发生的每个视图)。
为了确保只在 UITesting 时添加按钮,在 setUp()
函数中通过将元素附加到 launchArguments
数组来传递 launchArguments。
launchArguments.append("push_notification_button")
要从应用内访问:
if ProcessInfo.processInfo.arguments.contains("push_notification_button") {
// Add button
}
我们想在 UI 测试中测试推送通知的接收(当应用程序处于活动状态时)。发送真实推送通知的 goto 解决方案听起来有点矫枉过正(并且只适用于真实设备):参见 http://www.pixeldock.com/blog/testing-push-notifications-with-xcode-uitests/
我的问题是:是否可以(直接或间接)从 (UI) XCTestCase
调用 UIAppicaltionDelegate 的 didReceiveRemoteNotification
?
您不能从 UITest 调用应用程序代码中的函数。 UITests 运行 在单独的进程中,无法访问您应用的功能和方法。
来自 Apple 的文档:
UI testing differs from unit testing in fundamental ways. Unit testing enables you to work within your app's scope and allows you to exercise functions and methods with full access to your app's variables and state. UI testing exercises your app's UI in the same way that users do without access to your app's internal methods, functions, and variables. This enables your tests to see the app the same way a user does, exposing UI problems that users encounter.
在 UnitTest 中可以从测试中直接调用 didReceiveRemoteNotification
。我不知道您的应用如何处理推送通知,但如果您可以使用单元测试来测试所需的结果,那可能是一个有效的替代方案。
您可以从 UITest 中触发应用中的事件。我之前通过将按钮放在我的视图上来完成它(一个限制是我必须将按钮添加到我希望事件发生的每个视图)。
为了确保只在 UITesting 时添加按钮,在 setUp()
函数中通过将元素附加到 launchArguments
数组来传递 launchArguments。
launchArguments.append("push_notification_button")
要从应用内访问:
if ProcessInfo.processInfo.arguments.contains("push_notification_button") {
// Add button
}