Writing XCTestCase for Mac OS Command Tool

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

When I first create a project, Xcode does not create a test group in the project navigator. When I try to add a new test target, it does not give me the opportunity to specify my target as "Target to Tested".

My question is this: is it possible to use XCTest for a command line project? Or is it just considered trivial for this due to the fact that I could just start it from the command line? I could understand this reasoning, but there is an internal functionality that I really would like to test.

+10
source share
3 answers

I'm not sure which version of Xcode you are using, but I ran into a similar problem using templates in Xcode version 6.2 (6C131e). However, I was able to get XCTests to work with the Command Line Tool project. The solution was to ignore the Target Testing field during creation and instead add the test target to the main circuit after it was created:

  • Scroll to the "Schema Management" section. You should see your basic Goal chart and the newly created test chart.
  • Select the main scheme and go to the "Edit" section.
  • Select a test action and add a new test target to the tests using the “+” in the test details panel.

From there, you should be able to run tests using cmd-U.

+12
source

In Xcode 8.2, I was able to run unit tests only in the command line application by adding the unit test target from the Test Navigator, then editing the test scheme, including this new test target in the tests list, and manually adding source files for the test target from the " Target Membership of the File Inspector panel.

(Adding a unit test target from the Add Target screen of a project will not properly communicate with the XCTest platform, even after adding a platform for the build steps.)

Following Apple Doc 's instructions for adding a unit test goal from the Test Navigator panel is as follows:

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

Remarks:

  • In the “Unit Test Target” setting, the “target for testing” drop-down still does not select the command line tool. Leave this option as “No”.
+5
source

I expand on Jeffrey Fulton's comment in the previous answer. I banged my head on this issue for three days. In the FunctionsTests.swift file, I would try to import my module using @testable import myModule but when I try to run the tests, I always get the error Undefined character: enter metadata for function Y, and the test build will fail (while the normal build will fail work like a charm).

Jeffrey's comment fixed it for me. Here is a step by step from the very beginning.

To add tests for the command line tool, I needed to go to File >> New >> Target .... In the search area or find the MacOS testing package .

Then click "Change Scheme" when the application target is active. edit diagram

In the next panel on the left, go to "Testing" and add the test target here. go to Test, click on the plus and add the goal of the test

Finally, in the membership target panel, add each file to the test target. add test target membership

Only after that the test assembly was completed.

0
source

Source: https://habr.com/ru/post/1211625/


All Articles