WishIHadThreeGuns
WishIHadThreeGuns

Reputation: 1469

XCTest for a Mac command line application

I want to test a command line application, but import XCTest gives me a message that the underlying module cannot be loaded.

How can I test a simple "hello world" command line App?

Upvotes: 2

Views: 675

Answers (2)

Radhika Chilukuri
Radhika Chilukuri

Reputation: 1

After adding unitest for macOS command line we will end up with many linking errors. to avoid that,

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 all my linking errors.Hope it helps you as well

Upvotes: 0

olha
olha

Reputation: 2262

So, you have a simple command line app with main.swift/main.m and maybe some other code.

By default you have only one target with the same name as your product.

Press the "project" file, and you'll get smth like this:

The blue icon in the left-top corner is your project's file, and in the right-bottom part you have a list of targets.

enter image description here

Press the "+" button, and select MacOS Unit testing Bundle: enter image description here

Name your testing bundle somehow, e.g. test: enter image description here

Now your project contains 2 targets: one "main" (named the same as the project) and one "testing".

When adding a new file, don't forget to add it to the testing target (the checkbox under the Targets section ): enter image description here


Please note that this type of testing target is Logic tests, not Application tests, for more info look here: https://forums.developer.apple.com/thread/52211

Upvotes: 4

Related Questions