如何从SenTestingKit/OCUnit迁移到XCTest? [英] How do I migrate from SenTestingKit/OCUnit to XCTest?

查看:73
本文介绍了如何从SenTestingKit/OCUnit迁移到XCTest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将项目从Xcode 4.6.3迁移到Xcode 5.0.2.该项目的单元测试是使用SenTestingKit/OCUnit开发的.现在,当我在Xcode 5中运行测试时,我从RunUnitTests脚本得到一个错误,告诉我

I am in the process of migrating my project from Xcode 4.6.3 to Xcode 5.0.2. The project's unit tests were developed with SenTestingKit/OCUnit. Now when I am running the tests in Xcode 5, I get an error from the RunUnitTests script telling me that

RunUnitTests已过时.

RunUnitTests is obsolete.

可能与Xcode 5发行说明中的​​此说明相关:

Possibly related is this note in the Xcode 5 release notes:

SenTestingKit和OCUnit已弃用.使用迁移器移至XCTest.

SenTestingKit and OCUnit are deprecated. Use the migrator to move to XCTest.

不幸的是,我无法找到有关这个神秘的移民"的更多信息.可能我的google-fu再次缺少,所以我的主要问题是:如何将单元测试从SenTestingKit/OCUnit迁移到新的XCTest(带有或不带有"migrator")?

Unfortunately, I have not been able to find out more about this mysterious "migrator". Possibly my google-fu is lacking [again], so my main question is: How do I migrate unit tests from SenTestingKit/OCUnit to the new XCTest (with or without the "migrator")?

第二个问题是,如果迁移是一项复杂的业务:是否可以让Xcode 5运行仍基于SenTestingKit/OCUnit的单元测试?毕竟这些都是不推荐使用的,所以它们仍然应该存在并且可以正常工作.

A secondary question, in case migrating is a complicated business: Is it possible to get Xcode 5 to run unit tests that are still based on SenTestingKit/OCUnit? After all these are merely deprecated, so they should still be around and functional.

推荐答案

感谢Shaggy Frog的回答,我们知道Xcode发行说明中提到的神秘的迁移器"是通过选择编辑>重构>转换为XCTest"启动的向导. ".我将分两部分来介绍我对该向导的使用经验.第一部分是对主要问题的不完整答案,第二部分是对次要问题的回答.

Thanks to Shaggy Frog's answer we know that the mysterious "migrator" mentioned in the Xcode release notes is a wizard launched by selecting "Edit > Refactor > Convert To XCTest". I am going to write about my experience with this wizard in two parts. The first part is an incomplete answer to the primary question, the second part answers the secondary question.

您需要意识到的第一件事是,要使向导正常工作,您需要选择一个单元测试目标.如果选择了主要目标,则向导不会列出任何要转换的目标.

The first thing you need to realize is that for the wizard to work, you need to select a unit test target. If you have the main target selected, the wizard simply does not list any targets to convert.

一旦了解到这一点,便可以逐步完成向导,但就我而言,最终结果仍然是一个惊人的失败!该向导声称不需要进行任何源代码更改,只需要更新构建设置即可迁移到XCTest.最后,该向导甚至没有设法正确地做到这一点:它 did 删除了对SenTestingKit框架的引用,但是却没有 not 放入了对XCTest的引用.框架.

Once I found out about this, I was able to step through the wizard, but in my case the end result was still a spectacular failure! The wizard claimed that no source changes were necessary and that only build settings needed to be updated to migrate to XCTest. In the end, the wizard did not even manage to do that correctly: It did remove the reference to the SenTestingKit framework, but it did not put in a reference to the XCTest framework.

无论如何,以下是由于向导无法为我进行手动更改的列表.如果该向导更适合您,则您可能不需要做所有这些事情.

Anyway, what follows is a list of the changes that I had to manually make because the wizard failed to make them for me. If the wizard works better for you, you may not need to do all of these things.

  1. 从单元测试目标中删除运行脚本"构建阶段
  2. 将所有测试用例类的基类从SenTestCase更改为XCTestCase
  3. 将导入的标头从<SenTestingKit/SenTestingKit.h>更改为<XCTest/XCTest.h>
  4. 在测试目标的构建设置"中,将包装扩展程序"从octest更改为xctest.
  5. 将所有断言宏从ST*重命名为XCT*(例如STAssertTrue变为XCTAssertTrue)
  6. 上述例外:STAssertEquals需要重命名为XCTAssertEqual(请注意末尾缺少的"s").如果您收到以下编译器警告,就会知道您已经忘记了这一点:warning: implicit declaration of function 'XCTAssertEquals' is invalid in C99
  7. 新的XCTest断言宏不允许将nil作为故障描述传递.例如,XCTAssertNotNil(anObject, nil)是不可能的,必须将其更改为XCTAssertNotNil(anObject).出现此编译器错误时,您将知道遇到此问题:error: called object type 'NSString *' is not a function or function pointer.
  8. 如果要做需要传递失败描述,则新的XCTest断言宏需要格式说明符的常量表达式,就像NSString类方法stringWithFormat:一样.当您收到以下编译器错误时,您将知道遇到此问题:error: expected ')'.一些例子:
  1. Remove the "Run Script" build phase from the unit test target
  2. Change the base class of all test case classes from SenTestCase to XCTestCase
  3. Change the imported header from <SenTestingKit/SenTestingKit.h> to <XCTest/XCTest.h>
  4. In test target's Build Settings, change Wrapper Extension from octest to xctest.
  5. Rename all assert macros from ST* to XCT* (e.g. STAssertTrue becomes XCTAssertTrue)
  6. Exception to the above: STAssertEquals needs to be renamed to XCTAssertEqual (note the missing "s" at the end). You will know that you have forgotten about this if you get this compiler warning: warning: implicit declaration of function 'XCTAssertEquals' is invalid in C99
  7. The new XCTest assert macros do not allow nil to be passed as the failure description. For instance, XCTAssertNotNil(anObject, nil) is not possible and must be changed to XCTAssertNotNil(anObject). You will know that you have this problem when you get this compiler error: error: called object type 'NSString *' is not a function or function pointer.
  8. If you do need to pass a failure description, the new XCTest assert macros require a constant expression for the format specifier, just as the NSString class method stringWithFormat: does. You will know that you have this problem when you get this compiler error: error: expected ')'. Some examples:

NSString* formatSpecifier = @"%@";
NSString* failureDescription = @"foo";
// These are OK
XCTAssertNotNil(anObject, @"foo")
XCTAssertNotNil(anObject, @"%@", failureDescription)
// These are not OK
XCTAssertNotNil(anObject, failureDescription);
XCTAssertNotNil(anObject, formatSpecifier, failureDescription);

最后但并非最不重要的是,正如后面已经提到的,需要将对XCTest框架的引用添加到单元测试目标中.如果您遇到链接器错误(例如Undefined symbols for architecture i386: "_OBJC_CLASS_$_XCTestCase", referenced from: foo).

Last but not least, as already mentioned further up, the reference to the XCTest framework needs to be added to the unit test target. You will know that you have forgotten this if you get linker errors such as Undefined symbols for architecture i386: "_OBJC_CLASS_$_XCTestCase", referenced from: foo.

Xcode 6更新:Xcode 6中不再需要与XCTest链接(实际上XCTest甚至都不再列为可用框架).而是将构建设置CLANG_ENABLE_MODULES设置为YES(在UI中显示为启用模块(C和Objective-C)").这将导致clang在看到#import <XCTest/XCTest.h>语句时自动针对XCTest进行链接.有关详细信息,请参见clang文档的模块"部分.

Xcode 6 update: Linking against XCTest is no longer required in Xcode 6 (in fact XCTest is not even listed as an available framework anymore). Instead set the build setting CLANG_ENABLE_MODULES to YES (exposed in the UI as "Enable Modules (C and Objective-C)"). This will cause clang to automatically link against XCTest when it sees an #import <XCTest/XCTest.h> statement. Details are available in the "Modules" section of the clang documentation.

这时,我收到一个链接器错误,使我意识到迁移到XCTest的任务失败了.原因:XCTest不是SDK 6.1的一部分,但我仍在使用基础SDK iOS 6.1构建我的项目(此SO答案说明如何将SDK 6.1集成到Xcode 5).

At this point I got a linker error that made me realize that my mission to migrate to XCTest had failed. The reason: XCTest is not part of SDK 6.1, but I am still building my project with base SDK iOS 6.1 (this SO answer explains how to integrate SDK 6.1 into Xcode 5).

由于我无法继续进行迁移,因此目前的解决方案是保持基于SenTestingKit/OCUnit的单元测试,直到找到时间将应用程序升级到iOS 7.为了使单元测试运行而要做的事情:

Since I am unable to continue with the migration, my solution for the moment is therefore to keep my unit tests based on SenTestingKit/OCUnit, until I find the time to upgrade my app to iOS 7. This is what I had to do in order to get the unit tests to run:

  1. 从单元测试目标中删除运行脚本"构建阶段.这就是在选择单元测试目标时让Xcode通过测试"操作( + U )执行单元测试所需要的全部操作..
  2. 但这不是理想的,因为我不想仅仅为了执行单元测试而切换目标.相反,我想在选择主要目标时执行单元测试.因此,第二步是修改主要目标的Xcode方案,以便当我运行"Test"操作时,将改为执行单元测试目标的测试.
  1. Remove the "Run Script" build phase from the unit test target. This is all that is required to let Xcode execute unit tests via the "Test" action ( + U) while the unit test target is selected.
  2. This is not ideal, though, because I don't want to switch targets just to execute unit tests. Instead I want to execute unit tests while the main target is selected. The second step therefore is to modify the main target's Xcode scheme so that when I run the "Test" action, the unit test target's tests are executed instead.

最终的解决方案不像Xcode 4.x那样好,因为每次我运行主要目标的运行"或构建"操作时,单元测试都会自动执行.不幸的是,如果没有运行脚本"构建阶段,似乎无法实现这一目标.

The final solution is not as good as in Xcode 4.x where the unit tests were executed automatically every time that I ran the main target's "Run" or "Build" action. Unfortunately, it seems that I can't get this to work without a "Run Script" build phase.

这篇关于如何从SenTestingKit/OCUnit迁移到XCTest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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