I've just started building a little CLI tool in Xcode on macOS, but I'm trying to make it as platform-agnostic as possible. To that end I found the nice ArgumentParser package.
But I'm finding it would be nice to be able to run automated XCTestCase cases against the argument parsing, to make sure I’ve specified it correctly. How might I go about doing this?
It's a bit odd, since Xcode didn't create any unit tests when I instantiated the macOS CLI template, and even after adding a couple of Swift Package Dependencies, it the project doesn't have a Package.swift file. I suppose to be properly cross platform I should add one, but I don't want to fight Xcode right now and I'm worried about rocking its little boat.
What's the best way to unit test the parsing? Thanks!
For those of us who are still for whatever reason tightly bound to Xcode-centric building and testing, how do we add a test target that can access .parseAsRoot()? Are we forced, as others have suggested, to move all our code into a static library target, now testable, with the CLI executable target as just a thin shim to call it?
Furthermore, if using parseAsRoot(), how can we test assertions about standard and error output from the tool?
Or is there another way? I see that ArgumentParser's TestHelpers.swift extends XCTest adding AssertExecuteCommand which executes the CLI binary directly, but I have no idea how to add an Xcode test target that would invoke it - seems like SPM only.