Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I UI test a document-based app with XCTest?

Given the following setup:

  • A document-based OS X app written in Swift
  • OS X El Capitan dev machine
  • Xcode 7.x
  • The new / upgraded XCTest framework

How can the document-based parts of the app be UI-tested with the XCTest framework?


Regular unit or UI tests not related to the document-based functionality are easy, but I cannot see how to do UI tests on the document-based bit, given that in UI tests we are forbidden from reaching into the app and, for example, mocking the NSDocument class.

One suggestion I've heard is to create fixture files / folders (depending on what your document-based app needs) in the test resources folder, and somehow get the test to open that. But is there any better solution?

like image 992
chriskilding Avatar asked Oct 19 '22 09:10

chriskilding


1 Answers

The person who made the fixtures suggestion is pretty much spot-on. Since the UI Tests are supposed to exercise exactly the functionality of the app without reaching into the internals, you'll have to load the documents externally.

XCTest is not (as of Xcode 7.x + El Capitan) able to drive the File -> Open... -> Select a file -> Click 'Open' button flow - it gets as far as the Open... dialog and then the test crashes because it cannot proceed further. This means you won't be able to open the document via the UI.

This leaves only one other option - you should try to initialize the app through some sort of passed argument in init to lob the necessary local state in.

like image 92
Aaron Sofaer Avatar answered Oct 31 '22 18:10

Aaron Sofaer