Dev: Adding unit tests to command line projects

Wanted to add unit tests to a command line project. Realized that this isn’t an option in the creation dialog like it is with normal apps. So putting this here for the googles.

1. Add a testing bundle. Editor > Add Target, Cocoa (Touch) Testing Bundle, Next, etc.

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.

3. Try. Check that you set it up right with Product > Test (Command-U).

Tips:

  • When using Swift, don’t forget that to use @testable in your import to expose all the classes, whether or not marked public.
  • When converting your target name for import, use underscores to replace spaces: “Hola Svift” becomes “Hola_Svift”.

Comments are closed.