14

So, I am having a bear of a time figuring this one out. I have looked around, and cannot seem to find any information on this.

What I want to do is have my unit tests ALWAYS run EVERY time I build my code in XCode. I have read a number of posts, including this question here. I have followed the instructions to the letter, and the build will not run the tests.

What I have done:

  • My Test suite is a target dependency of my main build
  • My main build has "Test After Build" set to Yes
  • All of the tests under the "test" phase in the Scheme are there, and checked

If I run the tests manually - through cmd+U or the menu - the tests run. I have added a failing test to try and force the build to fail - so I can be sure they are running. The build continues to pass fine, and the tests are never run.

I am positive I have missed a step in here, but for the life of me I cannot find any documentation related to it. Anyone have any other advice or steps I should be doing?

Community
  • 1
  • 1
aperkins
  • 12,914
  • 4
  • 29
  • 34
  • 3
    Have you added your "test target" in the "Test" section of main target'schema settings? To do so, edit main target schema, click on "Test" section from Left panel and at right panel add your test target. Then goto "Build" section from left panel, and in right section, check the "Run" for you test target. hope it will help you! – Learner Sep 24 '11 at 20:16
  • @Learner Yeah, I did that already. It seems like it is a problem with the version of XCode I am running - along with some of the other crash bugs and the like that I am finding. I wish they would release an update for Snow Leopard. – aperkins Sep 26 '11 at 20:54
  • @Learner's comment worked for me, I noticed I didn't have my test project set to run in the main target's build scheme setting. – Brian Wigginton Oct 04 '11 at 07:33
  • See my answer here > http://stackoverflow.com/questions/6673333/how-do-i-automatically-perform-unit-tests-on-each-build/7644701#7644701 – Brian Wigginton Oct 04 '11 at 07:48
  • @BrianWigginton it is funny - I did all of that. I think there is a bug in xCode 4 that is being exposed by something funky in the config for that project. Oh well. I will try it out with another project at some point in the future. – aperkins Oct 04 '11 at 15:40
  • Tests run when you click "Test". Generally, an up-to-date build is a prerequisite of a test, not the other way around. So if you change your code and then start a test, it will build. So just always test, instead of always building. You can probably add a script build phase to invoke your test target, too, but I've never done that myself. – Bored Astronaut Dec 15 '11 at 22:19
  • @BoredAstronaut The whole point is that I don't want a build to "succeed" without the tests passing - i.e. the whole point of having functionality like this is that the unit tests are part of the successful build process. This is a common system in other build environments such as Maven and Ant, and is considered a best practice to help guarantee that your unit tests are passing when you modify the code. Relying on manual behavior is generally a recipe for forgetfulness, a lesson I have learned too many times ;) – aperkins Dec 16 '11 at 16:19

2 Answers2

8

It doesn't matter whether or not "Test After Build" is set to yes or no in your Something.app target. It is only necessary to set "Test After Build" to YES in the unit test target. Also make sure that both "Test" and "Run" are selected for your unit test target in the scheme editor under the "Build" tab options. To see the results click on the Log Navigator View > Navigators > Show Log Navigator (command 7).

jaredsinclair
  • 12,687
  • 5
  • 35
  • 56
  • I think this should be the accepted answer. It's the first one I've seen to the various forms of this question to get it all right, without a lot of extra of "voodoo" steps that aren't actually part of the solution. Thanks, @Jared! – big_m Nov 26 '12 at 16:36
0

In your main target's Build Phases, add a new Run Script build phase. The content of the script should be:

"${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests"

That will run the tests - but only if it can find the .octest files generated by your project. RunUnitTests wants PRODUCT_NAME to be the name of your unit test, and WRAPPER_EXTENSION to be .octest (these are Xcode variables). If your main target is an aggregate target that has your tests AND your app as dependancies, you can probably make it work with that in mind.

quellish
  • 21,123
  • 4
  • 76
  • 83