Writing XCTestCase for Mac OS Command Line Tool
Solution 1:
I'm not sure what version of Xcode you are using, but I ran into a similar problem using the templates in Xcode Version 6.2 (6C131e). That said, I was able to get XCTests to work with a Command Line Tool project. The solution was to ignore the "Target to be Tested" field during creation and instead add the Test target to the main scheme after creating it:
- Go to Manage Schemes. You should see your main Target’s scheme and a newly created test scheme.
- Select you main scheme and go to Edit.
- Select the Test action and add your new Test target to the tests list using the “+” in the Test action detail panel.
From there you should be able to run the tests using cmd-U.
Solution 2:
In Xcode 8.2, I was only able to run unit tests in a command line app by adding a unit test target from the Test Navigator, then editing the testing scheme to include that new test target under the "tests" list, and manually adding testable source files to the test target from the "target membership" section of the File Inspector pane.
(Adding a unit test target from the project "add target" screen wouldn't link properly with XCTest framework, even after adding the framework to build phases.)
Following the Apple doc directions for adding a unit test target from the Test Navigator pane looks like this:
Note:
- In the Unit Test Target setup, the "target to be tested" dropdown still won't select the command line tool. Leave this option as "None".