开发者

I have an Xcode static library project, how do I add a test target to it so I can run it there? (Instead of in a project that links to it.)

I want to be able to test library code in the library target so I don't have to switch over to a separate project to run it.

I see how to add a target, but I'm not sure how to set it up to run like the "Command Line Tool" project template does. I tried adding a new "Shell Tool" target, but I don't know how to make it run like one. What build settings do I have to add to that target? What files (main.m?) do I need to start it up?

EDIT (Added my comments from below to hopefully make the question more answerable.)

My library has two targets, one for iOS and one for MacOS. I'm only including it in an iOS project so far, so that's the one I want work with presently. I tried adding a Cocoa Touch Unit Test Bundle target, but my build fails and I get command /bin/sh/ failed with exit code 1 and Failed tests for architecture 'i386' (GC OFF). I don't think I want to run on i386 (at least not for a test of my iOS target) and I can't find anything that says i386 in the target settings.

I see that my active architecture for the project is i386 (in the Project > Set Active Architecture menu), but it doesn't let me change it there. My TestApp target has Standard (armv6) as the architec开发者_StackOverflow中文版ture set and I tried searching the settings and nothing has i386 in it. How do I change that?


There's a template for a unit-test bundle target in the Mac OS X SDK.

Note that the “bundle” is the build product of the test target: The target produces a unit-test bundle. It does not require that your actual product (library, app, etc.) be a bundle; in fact, it doesn't matter what your actual product is.

That said, you can do it a little differently when you have a static library:

  1. Right-click on anything in your group tree. From the “Add” submenu, choose “New Target”.
  2. Under the Mac OS X header, choose Cocoa. Scroll down in the list of templates and choose Unit Test Bundle. Click OK.
  3. Name your unit-test bundle target.
  4. Add your library build product (from the Products group) to your unit-test bundle target's Link Binary with Libraries phase.
  5. Add your library target (from the Targets group) to your unit-test bundle target, outside of any build phase. This makes it a dependency, so that the test bundle target will rebuild the library when necessary.
  6. Set your active target to be the test bundle target.

Normally, you would just add the source code files for your code under test to both the library target and the test bundle target, but doing things this way also tests your ability to link the test bundle against the library. If the library has any problems that would prevent linking against it, this may detect them.


Can you change the Active Executable? (it is under the Project menu in Xcode: Set Active Executable)

Does your Shell Tool target contain the int main(int argc, char* argv[]) function in one of its built files?


For Mac, you can add a new target. Choose the command-line tool template.


In XCode 9.1:

  • Click File -> New -> Target...
  • Select MacOS Unit Testing Bundle
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜