15

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.

Jonathan Chen
  • 716
  • 8
  • 19

3 Answers3

14

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.

cprime
  • 141
  • 4
  • 5
    I also had to manually add test target membership to all code files I wished to test. – Jeffrey Fulton Dec 25 '16 at 07:45
  • 2
    @JeffreyFulton's comment about adding the files to the test bundle was what made this answer work. I would consider adding that to the answer, then marking it as the correct solution. – horseshoe7 Feb 27 '19 at 12:33
6

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".
savinola
  • 380
  • 4
  • 6
0

For adding unit test target (XCTest) for Mac OS Command Line project which has(main.swift and other swift files), to make this work, 1.Add UnitTests target to scheme by editing in manage schemes 2.Make your functions and classes has PUBLIC access

This solved my linking errors. Hope it helps for you guys as well

  • If by `public`, you mean instead of adding the sources to the test target, then no, it will still be unable to find the source code. – bauerMusic Sep 19 '22 at 05:31