如何在Xcode中组织图书馆项目的单元测试? [英] How to organize unit testing of a library project in Xcode?
问题描述
我还不喜欢集成开发环境,但是我正在努力克服偏见并学习Xcode. (下一个是Eclipse/CDT;去年尝试时,我也无法使用它,但这是一个单独的问题.)
我正在一个新项目中编写一些新代码,该项目将成为一个小型库(的一部分).我也想对其进行单元测试.如何向Xcode解释我正在构建(共享)库,但我也想在测试程序中使用它,该程序是从不在共享库中的单独源编译而来的?
源代码:
- atom.c
- atom.h
- test-atom.c
生成的文件:
- libatom.dylib
- 测试原子
我已经将atom.c
和atom.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屋!