Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 7: Can't create a Swift command line tool with a unit test target

Oh, Xcode!

I'm stymied. If I create a Mac OS Cocoa app, I get a unit test target for free. But I want to write a command line tool (or even a library that I can link into a command line tool) and write some unit tests against it. I'll be darned if I can figure it out.

I've gotten far enough that I have a command line target, and a test target. I can "@testable import" my commnd line module and use the code in the test code with no errors in Xcode. However, as soon as I try to run my test (Cmd-U), I get a link error. None of the classes in the main module can be linked. What?

I've been messing with this for hours, and the Great Google has been no help. (I'm using Xcode 7, and Xcode 6 seems very different.) Can anyone help me?

Thanks.

like image 981
George Madrid Avatar asked Dec 04 '15 20:12

George Madrid


2 Answers

So far the only solution I've found for this problem is to manually add all the files containing code that you want to test to unit test target manually:

enter image description here

This is something you wouldn't do when testing an application target. I think the fact that the command line target cannot be selected as the test host for a unit test target might be related with this issue:

enter image description here

Another option you have, which might require a bit more work, is to define all your logic into a Framework, and write the command line app as a consumer of the APIs it provides.

This way you can easily unit test the framework the usual way, and then write integration tests for the command line app in the form of scripts that call it and assert the results.

like image 162
mokagio Avatar answered Oct 23 '22 01:10

mokagio


After struggling with this here is the solution that worked for me:

  • Step 1: Add a testing bundle. Editor > Add Target, Cocoa Testing Bundle
  • Step 2: Edit the scheme. Product > Scheme > Edit Scheme. Select Test, click +. Under “Choose targets to test as part of this scheme”, select your test target.
  • Step 3: Try with you simple test
like image 39
Long Nguyen Avatar answered Oct 23 '22 01:10

Long Nguyen