How do I automatically perform unit tests on each build? - xcode

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.

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.

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

Here's what I had to do to get Xcode 4 to automatically run unit tests on build.
Enable "Test After Build" for your main target. (See Mark Szymczyk's answer)
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.

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.

Related

Xcode 11, where can I manage Build Phase?

I've migrated my app into Xcode 11.
I want to remove legacy resources that copied at Build Phase, but I can't find old Build Phase menu.
Please make sure that you have selected Target instead of Project.
The problem is that you have the project selected (in the "project and targets list", which may be a collapsed pane for your Xcode).
So first, make sure the button to the far left of the "Info" tab shown in your screenshot is toggled on (so you can see your project and targets list).
Then simply select a target. Now you'll see the Build Phases tab. Only the targets of a project have build phases.
Note: If you don't like to keep the targets list shown, you can simply use the popup menu next to the pane's toggle button - which appears when the pane is not shown - to toggle from project to a target.

Forcing XCTest to run against one target

We are using xctests to run end to end ui tests and have a project with a 3 different targets and I don't want UI tests to run on two of them because they are connected to production.
is there anyway to enforce UI tests to run only against one scheme / target ?
Sure. You can set up different test suite by scheme. Click on your scheme in the top let of Xcode, select Edit Scheme and then navigate to Test in the left side bar of the menu that pops up. From here, you can select to add/remove test targets from the scheme and even enable/disable specific tests.

Invoking Actions other than Build and Clean & Build

I am new to NetBeans and am trying to define and run various custom goals for my Maven proj. My Maven proj uses a number of plugins; once for building the Java classes, another for pre-compiling JSPs, another for building an OSGi bundle, and another for deploying to my dev app server.
It seems that by default, NetBeans has Build, and Clean & Build as easily accessible "build options" in the main toolbar bar. These trigger the install goal which runs through the entire build processes (compiles everything and deploys to my dev).
In NetBeans, I can edit the Properties for the project, and define a "development" profile, and then define custom actions (or modify the default NetBeans actions).
How do I:
Invoke Actions that aren't Build or Clean & Build in NetBeans
Define only certain goals for specific plugins to run, and invokes those actions.
First go to the Project Properties > Actions and create your MVN build
The above will run:
mvn install -P initdb,initdb-qa -DskipTest=true -Dprop.name=dev
Then it will become available under "Custom"
This is really just a comment on #Ev0oD's answer, but I want to add an image so it has to be a separate answer.
After I created the toolbar button using:
Tools -> Options -> Java -> Maven -> Execution -> Edit Global Custom Goal Definitions
I then had to add the button to the toolbar using:
View -> Toolbars -> Customize (or just right click the toolbar header and select Customize)
I could then see the new button in the "Maven" group in this dialog:
I then had to drag the button onto a toolbar as shown. Note: Don't try to drag it to the blank part on the right of the toolbar header as this won't work.
If you want to use some custom goals on more projects and you want to have a quick access to them, you can use global custom goals, that are present in NetBeans 8.0.1 (not sure in which version they were introduced).
Access Tools -> Options -> Java -> Maven -> Execution -> Edit Global Custom Goal Definitions...
There you can add many of these and they will be present for all projects in the Custom section, as shown in the accepted answer.
A good thing is you can add it also to the Toolbar by clicking (Show in toolbar) and selecting one of six different colors of icon available. You will end up with something like this:
alternatively you can use the Custom popup on project node and within the Custom... action's dialog, setup goals for execution and eventually remember the setup for subsequent executions. The remembered item will appear in the same popup menu. For global (for every project) goal definitions, please see the global options.

Build Phase Tab Hidden or Missing

I'm probably asking a very elementary Xcode question, but here it goes. My layout has no 'Build Phase' tab. I might have inadvertently changed my layout. A lot of tutorials state that you should go to the Build Phase tab and link to a library. Well, I've Clicked on the Target, and the File under the Target, Searched the Toolbar, searched the project settings, Info. When I right click on the Target File I get a list including "Copy Build Phase" and Add Build Phase, but that's not giving me access to the Build Phase Tab. I've tried 'reset to default or factory layout'.....Build Phase Tab. How can I display the layout with a Build Phase tab in the toolbar, and save it to utilize in my projects? By the way I'm using XCode 3.2 maybe it's a version limitation?
It sounds like those tutorials are for Xcode 4. For your version, if you open the target in the sidebar, there should be some groups. Those are your build phases. Just drag the library into the one that says something like "Link Executable", or click the checkbox to add it to your target and it should go there automatically.

What target is build when I just Build my Xcode project

If I just build my Xcode 4 project (using the cmd-B) command, which target is built?
I'm having trouble understanding the new scheme setup. When I look at the currently selected scheme, under the "Build" tab, all my different build types are checked (ie: Analyze, Test, Run, Profile, Archive). However, when I build, it seems like only "Run" is built. And for the *.app file that is produced, is this built using the "Build Configuration" set under the "Run" tab?
Thanks!
Sunny
Whichever target that is in selection next to your Run, Stop button in your Xcode.

Resources