18

How do I automatically perform unit tests on each build?

I tried to add the Unit Tests target to the Project Target as a dependency, but that doesn't seem to run the actual tests.

AWF4vk
  • 5,810
  • 3
  • 37
  • 70
  • Instead of building, do unit test, which takes care of compiling both target and test-target. – Saran Nov 27 '18 at 07:50

4 Answers4

8

Set the Test After Build build setting to Yes. Choose Product > Build For > Build For Testing to build the project and run the tests.

Xcode 5 Update

Xcode 5 does not support the Test After Build build setting. I don't know of any way to automatically run tests on each build in Xcode 5. From the OS X menu bar choose Product > Test or press Command + u to run unit tests in Xcode 5.

BrightIntelDusk
  • 4,577
  • 2
  • 25
  • 34
Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
  • Select the project from the project navigator to open the project editor. Select the project or target on the left side of the project editor. Click the Build Settings button at the top of the editor. The Test After Build build setting is in the Unit Testing collection. – Swift Dev Journal Jul 13 '11 at 19:17
  • It still doesn't test after the project's build. – AWF4vk Jul 16 '11 at 21:56
  • Are you sure? Xcode doesn't show the test results to you if the tests pass. You have to open the log navigator (Choose View > Navigators > Log) to see the results of the tests. Open the log navigator and select the build from the list to see the build results, which should show that the tests were run. – Swift Dev Journal Jul 17 '11 at 01:53
  • Yes sure. I purposefully edited the UT to make sure it fails, but it doesn't fail. – AWF4vk Jul 17 '11 at 02:22
  • Which targets did you set the Test After Build build setting to Yes? I checked my projects, and the build setting is set to Yes in both the application target and the unit testing target. – Swift Dev Journal Jul 17 '11 at 02:42
  • Correct. Same for me. I think it might be a XCode bug. Like this: http://stackoverflow.com/questions/6721554/unit-testing-isnt-being-performed-in-xcode – AWF4vk Jul 17 '11 at 02:43
  • In your original question you said you added the unit testing target to the project target as a dependency. The unit testing target should have one target dependency: the application target (or whatever the main project target is). The unit testing target should not be one of the application target's target dependencies. – Swift Dev Journal Jul 17 '11 at 02:53
  • Note that this does not work in XCode 5. If somebody can edit the question to clarify that the correct answer is only valid for XCode 4 it would be nice... –  Dec 21 '13 at 08:47
  • @tea The question predates Xcode 5 by two years and is tagged xcode4, but I'll add an update for Xcode 5 to my original answer. – Swift Dev Journal Dec 23 '13 at 05:54
  • My bad I didn't know tagging was meant to be used in this way. Or rather, I took the habit of putting XCode N.x in posts because solutions often vary from one major version to the next. Well if you know how to do this gracefully in XCode5 [I am specifically asking about this](http://stackoverflow.com/questions/20605509/how-do-i-automatically-perform-unit-tests-on-each-build-and-run-action-in-xcod) so I am definitely interested! –  Dec 24 '13 at 07:12
3

To establish the relationship between your targets:

  • In the Scheme picker, select "Edit Scheme…"
  • Select the Test phase
  • Make sure you're seeing the Info tab, not the Arguments tab
  • Click '+' and specify your testing target
  • You should see a list of tests. Click 'OK'

Then to run the tests, choose Product > Test, or simply ⌘U from the keyboard. This will:

  • Build your main target
  • Build your test target
  • Execute your main target according to its type, running the tests
Jon Reid
  • 20,545
  • 2
  • 64
  • 95
3

Here's what I had to do to get Xcode 4 to automatically run unit tests on build.

  1. Enable "Test After Build" for your main target. (See Mark Szymczyk's answer)
  2. Enable your Test target to run during the build phase of your main target's build scheme.
  • Click Product > Manage Schemes menu item
  • Double click your target's scheme (I only had one in my list)
  • Unfold the Build accordion on the left
  • Click the Build option in the accordion
  • You should see you test project on the right, make sure the Run checkbox is clicked.
shim
  • 9,289
  • 12
  • 69
  • 108
Brian Wigginton
  • 2,632
  • 3
  • 21
  • 28
  • This step was the missing piece to get it working for me. Also, my unit tests are in a separate project and I had to make sure "Test After Build" was enabled for both my static lib project and my unit test project. –  Feb 22 '12 at 18:30
  • 1
    @Brian, I think the "Test After Build" setting needs to be set on the test target, not the main target. (The Quick Help message for the setting mentions that it is read by the RunUnitTests script and ignored otherwise.) Also, I think the above steps could be simplified to: Choose **Product > Edit Schemes...** and choose the scheme from the pop-up at the top. Then just click the **Build** action tab (no need to expand it). The last step is fine. [This answer](http://stackoverflow.com/a/12569164/686385), while not as detailed, explains it well. – big_m Nov 26 '12 at 16:31
1

I thought I'd give a high-level explanation of the steps that worked for me. (OS X 10.7.4, Xcode 4.3.3) If you are unfamiliar with things like Build Phases and Schemes, here's a great resource from Apple: http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Conceptual/Xcode4UserGuide/Building/Building.html

For a new project, make sure you check the "Include Unit Tests" checkbox on the page where you enter the name of the new project. Click on the project icon in the Project Navigator to see the two targets that have been created automatically. They'll be called something like "MyCocoaApp" and "MyCocoaAppTests". Select the MyCocoaAppTests target (NOT the MyCocoaApp one!) and select "Build Settings" from the choices at the top of the view. Enter "test after build" in the search bar near the top of the view. You should see a single setting for "Test After Build" and it's set to NO by default. Click on the NO setting and change it to YES. One more step. Bring up the scheme for the project. (Product Menu -> Edit Scheme...) Select "Build" in the column on the left. You should see your two targets and some check boxes to the right. On the line for "MyCocoaAppTests", check the "Run" checkbox and hit "OK". You're done. Type Command-B. Your app will build, the unit tests will build, the unit tests will run and fail on an error that is there by default.

If you have to add unit testing to an existing project, it's a bit more involved. First, follow these instructions by Apple: https://developer.apple.com/library/mac/#documentation/developertools/Conceptual/UnitTesting/02-Setting_Up_Unit_Tests_in_a_Project/setting_up.html#//apple_ref/doc/uid/TP40002143-CH3-SW1

If you don't miss any steps, you should now have unit tests set up that run when you select (Product Menu -> Test). Now, do the two steps described above: 1) Set the "Test After Build" setting to YES for the MyCocoaAppTest target; 2) Check the "Run" checkbox for the MyCocoaAppTest target in the Build section of the Scheme for the app. LAST STEP: You have to add a new "Run Script" build phase to the MyCocoaAppTest target (NOT the MyCocoaApp one!). Select the test target, click on "Build Phases", click on the "Add Build Phase" icon in the lower right, select "Add Run Script". A "Run Script" section will open up below the other build phases. The script field has this text in it: "Type a script or drag a script file from your workspace". In that field, enter: "${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests". (Including the quotation marks.)

That's it. Hit Command-B. Your app should build. Your tests should build and then run. Good luck. Phew.

onaquest
  • 63
  • 4