如何在Xcode 4.5“命令行工具"中设置工作逻辑单元测试目标.项目? [英] How to set up working logic unit tests target in Xcode 4.5 "Command Line Tool" project?

查看:103
本文介绍了如何在Xcode 4.5“命令行工具"中设置工作逻辑单元测试目标.项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为特定场景设置单元测试时遇到麻烦.这是我正在尝试的:

Having trouble getting unit tests set up for a specific scenario. Here's what I'm trying:

  • 在Xcode 4.5中,我创建了一个简单的OSX 命令行工具" 应用程序项目(基础).
    请注意,Xcode 不提供将单元测试自动添加到命令行工具"项目的选项.因此,请不要建议在复选框中打勾.它不在那里:-/

  • In Xcode 4.5, I created a simple OSX "Command Line Tool" application project (Foundation).
    Note that Xcode does not provide the option to add unit tests to a "Command Line Tool" project automatically – so please don't suggest ticking the ticky-box; it ain't there :-/

在我的项目中,我创建了一个简单的示例类,要进行测试;例如形状".

In my project, I created a trivial example class that I'd like to test; e.g. "Shape".

我按照Apple的 Xcode单元测试指南中的说明操作,

I followed instructions in Apple's Xcode Unit Testing Guide for Setting Up Unit-Testing in a Project:

  • 我在项目中添加了一个单元测试目标,并且

  • I added a unit test target to my project, and

我编辑了测试"方案以在新目标中运行测试.

I edited the "Test" scheme to run the tests in the new target.

在测试项目的实现(.m)文件中,我为Shape.h添加了导入,并在setUp()方法中添加了代码以实例化形状并将其分配给实例变量.

In the test project's implementation (.m) file, I added an import for Shape.h and code in the setUp() method to instantiate a shape and assign it to an instance variable.

那时候,我决定看看事情是否会建立,以及默认测试是否仍将运行.但是,当我从菜单中选择 Product ... Test 时,构建失败并出现以下错误:

At that point, I decided to see if things would build and if the default test would run still. However, when I selected Product...Test from the menu, the build failed with the following error:

Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_Shape", referenced from:
      objc-class-ref in ExampleTests.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

解释此错误不是问题.我认为单元测试目标没有链接到包含Shape的实现的二进制文件.但是,我还不了解Xcode单元测试和测试.目标配置.所以:

Interpreting this error is not the issue. I grok that the unit test target isn't being linked to the binary containing Shape's implementation. However, I don't (yet) grok Xcode unit testing & target configuration. So:

我需要做什么才能使测试目标与命令行工具的输出进行链接?我可以从单元测试目标链接到命令行可执行文件吗? Apple的文档看起来特定于常规OSX应用程序(*.app)和iOS应用程序,而两者都不是.

What do I need to do in order to get the test target linking against the command line tool's resulting output? Can I link to a command-line executable from the unit test target? Apple's documentation looks specific to regular OSX applications (*.app) and iOS applications, and this is neither.

我有一些想在命令行工具设置中开发的业务逻辑类(首先),所以我想了解为使单元测试目标在命令行工具"类型的项目. 谢谢!

I have business logic classes that I'd like to develop in a command-line tool setting (to begin with), so I'd like to understand what I need to do to get a unit test target running in a "Command Line Tool" type of project. Thank you!

(ps请注意,我对从命令行运行单元测试 不感兴趣–而是运行单元测试 在命令行工具"类型 project 上,并且仍然在Xcode中进行.)

(p.s. Note that I'm not interested in running my unit tests from the command line – Stack Overflow already suggested "similar" questions on how to do that – but rather running unit tests on a "Command Line Tool" type project, and still from within Xcode.)

推荐答案

我确定了我认为合适的解决方法,除了增加的目标之外,该解决方法似乎没有明显的缺点.

I've determined a workaround I find suitable and which doesn't appear to have significant drawbacks, other than an added target.

简而言之:该解决方案涉及添加静态库目标,以利用Xcode的创建&围绕这样的目标运行单元测试代码.然后,命令行工具目标将委托给静态库,在静态库中,定义了一个替代main()的函数,并由真实的main()入口点调用.命令行工具不包含非平凡的代码,因此单元测试目标可以访问所有值得测试的内容.

In a nutshell: the solution involves adding a static library target to take advantage of Xcode's ability to create & run unit test code around such a target. The command line tool target then delegates to the static library, where an alternate main()-like function is defined and called by the real main() entry point. The command line tool is kept free of non-trivial code, so the unit test target can access everything worth testing.

以下是步骤:

  • 从一个空的Xcode中,从菜单中选择 File ... New Project .

在出现的对话框中,选择 OS X ... Application ... Command Line Tool .出于本示例的目的,我将其命名为 SampleCmd .

In the resulting dialog, chose OS X...Application...Command Line Tool. For the purpose of this example, I'll assume it is named SampleCmd.

创建基本命令行工具项目后:

After the basic command line tool project has been created:

  • 从菜单中选择文件...新建...目标.

在出现的对话框中,选择 OS X ... Framework&图书馆...可可图书馆.出于本示例的目的,我将其命名为 SampleCmdLogic .

In the resulting dialog, choose OS X...Framework & Library...Cocoa Library. For the purpose of this example, I'll assume it is named SampleCmdLogic.

选择类型 Static ,因此命令行工具将保持为独立的可执行文件.

Choose type Static, so the command line tool will remain a stand-alone executable.

确保已选中包含单元测试框.

Make sure the Include Unit Tests box is checked.

创建静态库项目后:

  • main()函数从main.m复制到SampleCmdLogic.m,替换@implementation块. (此文件仅包含主入口点.可以为Objective-C类等添加其他文件.) 重命名功能为libMain().

  • Copy the main() function from main.m to SampleCmdLogic.m, replacing the @implementation block. (This file will hold the main entry point only. Other files can be added for Objective-C classes, etc.)  Rename the function to libMain().

SampleCmdLogic.h中,为新的libMain()添加一个声明,替换@interface块:
int libMain(int argc, const char * argv[]);

In SampleCmdLogic.h, add a declaration for the new libMain(), replacing the @interface block:
int libMain(int argc, const char * argv[]);

在命令行工具的main.m中,在顶部添加#import "SampleCmdLogic.h".

In the command line tool's main.m, add an #import "SampleCmdLogic.h" at top.

在命令行工具的main.m中,将真正的main()函数的全部内容更改为:
return libMain(argc, argv);

In the command line tool's main.m, change the entire contents of the real main() function to:
return libMain(argc, argv);

代码现已准备就绪,但需要执行链接步骤:

The code is now ready, but there are required linking steps:

  • SampleCmd的项目设置中的"构建阶段"下,展开"目标依赖项" ,然后添加(+)SampleCmdLogic作为依赖项.

  • In the project settings for SampleCmd, under Build Phases, expand Target Dependencies and add (+) SampleCmdLogic as a dependency.

SampleCmd的项目设置中的"构建阶段" 下,展开"使用库链接二进制文件" 并添加(+)libSampleCmdLogic.a

In the project settings for SampleCmd, under Build Phases, expand Link Binary With Libraries and add (+) libSampleCmdLogic.a

一切就绪.当您更改为SampleCmd目标并从菜单中选择 Product..Run 时,构建应会成功并按预期生成输出.当您更改为SampleCmdLogic目标并从菜单中选择 Product ... Test 时,构建应会成功并且单元测试将运行.报告的单个问题将是Xcode在SampleCmdLogicTests.m中插入的初始默认失败的单元测试断言.这是预期的.

Everything is now ready. When you change to the SampleCmd target and choose Product..Run from the menu, the build should succeed and output generated as expected. When you change to the SampleCmdLogic target and choose Product...Test from the menu, the build should succeed and the unit tests will run. The single issue reported will be the initial default failing unit test assertion inserted by Xcode, in SampleCmdLogicTests.m. This is expected.

从这一点开始,继续将所有逻辑和相应的测试添加到SampleCmdLogic目标. SampleCmd目标是保持琐碎,仅提供命令行工具入口点.

From this point on, proceed to add all logic and corresponding tests to the SampleCmdLogic target. The SampleCmd target is to remain trivial and providing the command line tool entry point only.

这篇关于如何在Xcode 4.5“命令行工具"中设置工作逻辑单元测试目标.项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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