Is it possible to use XCTNSNotificationExpectation in UI tests?

I'm writing a UI test for my app and am trying to determine if/when a given NSNotification is posted to the default NotificationCenter. I defined a XCTNSNotificationExpectation in my UI test:


let expectation = XCTNSNotificationExpectation(name: notificationName, object: nil, notificationCenter: NotificationCenter.default)
let result = XCTWaiter().wait(for: [expectation], timeout: 10)

I'm posting this notification in my app, but the UI test expectation is not being fulfilled (the result is .timedOut)


My guess is that the NotificationCenter that my app is posting to is not the same NotificationCenter that my UI test is listening to, because the UI test doesn't have visibility into the app's innards.


Q: Are XCTNSNotificationExpectations supposed to work in UI tests? If so, can someone provide an example of what the test code should look like?


Thanks in advance!

My guess is that the

NotificationCenter
that my app is posting to is not the same
NotificationCenter
that my UI test is listening to

Right. In a UI test the test code is running in a separate process from the app.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for confirming that the app runs in a separate process. That's good to know.


So is there a way to get XCTNSNotificationExpectations to work in UI tests, or are they only supported in unit tests?

So is there a way to get

XCTNSNotificationExpectations
to work in UI tests … ?

I don’t know for sure, but they’ve already seemed like a unit test thing to me. UI tests are support to test UI things, and

NSNotification
is an API thing, not a UI thing.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Is it possible to use XCTNSNotificationExpectation in UI tests?
 
 
Q