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

1

6

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?

Fillet answered 17/5, 2016 at 21:43 Comment(0)
I
3

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.

Interoceptor answered 19/5, 2016 at 23:2 Comment(2)
That's good to know, though I wish Apple would bake a better way of doing this into XCTest. Could you update your answer with a way to lob in the path to open in such a way that the path is relative? Otherwise we could end up with a brittle absolute path that only works on one developer's Mac, and nobody else's (nor the CI box)Fillet
I've actually never done it that way, but off the top of my head, are you able to embed the file as a binary in your project? Then it's just part of your workspace and you can access it through a number of ways, including by literal relative path.Interoceptor

© 2022 - 2024 — McMap. All rights reserved.