Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing XCTestCase for Mac OS Command Line Tool

I am trying to unit test a command line tool written for Mac OS.

When I first create the project, XCode does not generate a tests group in the project navigator. When I try to add a new test target, it doesn't give me the option to specify my target as the "Target to be Tested".

My question is this: is it even possible to use XCTest for a Command Line Tool project? Or is it just considered trivial to do so by virtue of the fact that I could just run it from the command line? I could understand that reasoning, but there is internal functionality I'd really like to test.

like image 735
Jonathan Chen Avatar asked Jan 21 '15 15:01

Jonathan Chen


2 Answers

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:

  1. Go to Manage Schemes. You should see your main Target’s scheme and a newly created test scheme.
  2. Select you main scheme and go to Edit.
  3. 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.

like image 67
cprime Avatar answered Oct 21 '22 14:10

cprime


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:

Xcode navigator pane open to Test Navigator section, after having clicked the 'add new test target or class' button

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".
like image 20
savinola Avatar answered Oct 21 '22 15:10

savinola