如何在Xcode中组织图书馆项目的单元测试? [英] How to organize unit testing of a library project in Xcode?

查看:98
本文介绍了如何在Xcode中组织图书馆项目的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还不喜欢集成开发环境,但是我正在努力克服偏见并学习Xcode. (下一个是Eclipse/CDT;去年尝试时,我也无法使用它,但这是一个单独的问题.)

我正在一个新项目中编写一些新代码,该项目将成为一个小型库(的一部分).我也想对其进行单元测试.如何向Xcode解释我正在构建(共享)库,但我也想在测试程序中使用它,该程序是从不在共享库中的单独源编译而来的?

源代码:

  • atom.c
  • atom.h
  • test-atom.c

生成的文件:

  • libatom.dylib
  • 测试原子

我已经将atom.catom.h编译到了库中.我只是不确定如何组织事物,因此也可以构建test-atom来与库链接. 我假设当我完成排序后,添加用于测试支持代码的库test-atom.c会相对简单一些-即使它尚未处于Xcode的控制之下.

FWIW,我主要使用C语言而不是目标C语言.

解决方案

您的项目中需要两个目标. Xcode中的目标会生成一个 product ,它是一个库,可执行文件或其他输出.

因此,您将有一个目标来生成libatom.dylib(我怀疑您已经设置了它),以及另一个命令行可执行文件目标来生成test-atom可执行文件供您运行以测试您的库./p>

一旦添加了test-atom目标,则应获取有关test-atom.c的信息,并从libatom.dylib目标中删除其成员资格,并将其添加为新的test-atom目标.文件的目标成员资格决定了构建目标是否将尝试编译/复制/链接该文件. (目标对该文件的作用取决于它成为成员时添加到哪个构建阶段.)

您还应该在产品"组的libatom.dylib条目上获取信息,并使那个成为test-atom目标的成员.这将导致test-atom可执行文件链接到libatom.dylib.

最后,在test-atom目标(不是产品)上获取信息,然后在常规"选项卡中的libatom.dylib目标上添加依赖项.这样可以确保构建test-atom目标将始终始终首先构建libatom.dylib目标.

I'm not yet a fan of integrated development environments, but I'm trying to overcome my prejudices and learn Xcode. (Eclipse/CDT is next; I couldn't get that to work for me either when I tried last year, but that's a separate issue.)

I am writing some new code in a new project that will become (part of) a small library. I want to unit test it, too. How do I explain to Xcode that I'm building a (shared) library, but I also want to use it in a test program, compiled from separate source that won't be in the shared library?

Source code:

  • atom.c
  • atom.h
  • test-atom.c

Produced files:

  • libatom.dylib
  • test-atom

I've got atom.c and atom.h compiled into the library. I'm just not sure how to organize things so that I can also build test-atom to link with the library. I'm assuming that when I've got that sorted, adding the library for the test support code that test-atom.c would be relatively straight-forward - even though it isn't under Xcode control yet.

FWIW, I primarily work in C rather than Objective C.

解决方案

You need two targets in your project; a target in Xcode produces a product which is a library, executable, or some other output.

Thus you'd have a target to produce libatom.dylib, which I suspect you've already set up, and another command-line executable target to produce the test-atom executable for you to run to test your library.

Once you've added the test-atom target, you should Get Info on test-atom.c and remove its membership from the libatom.dylib target, and add it as a member of your new test-atom target. The target membership of a file is what determines whether building a target will try to compile/copy/link that file. (What the target does with the file depends on what build phase it gets added to when it's made a member.)

You should also Get Info on the libatom.dylib entry in your Products group, and make that a member of the test-atom target as well. That will cause the test-atom executable to link against libatom.dylib.

Finally, Get Info on the test-atom target (not the product) and in the General tab, add a dependency on the libatom.dylib target. This will ensure that building the test-atom target will always first build the libatom.dylib target.

这篇关于如何在Xcode中组织图书馆项目的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆