OCUnit 中的简化断言
我刚开始使用 OCUnit,发现断言有点麻烦。在 JUnit 中,我可以编写一个测试来比较数字,如下所示。这个测试显然会失败,但这显示了我可以为两个数字编写的漂亮、简单的断言以及我得到的反馈:“预期为 <2>,但为 <3>”用很少的代码。
到目前为止我在 XCode 中尝试的是:
它可以工作,但不如 JUnit 优雅。您知道 XCode (OCUnit) 是否存在断言宏 alà JUnit 吗?另外,是否可以在 XCode 中获取红/绿条?
I just started jusing OCUnit and find the asserts a bit cumbersome. In JUnit I can write a test to compare numbers like below. This test will obviously fail, but this shows the nice, simple assert I can write for two numbers and the feedback I get: "expected <2> but was <3>" with very little code.
What I tried so far i XCode is:
Which works, but is not as elegant as JUnit. Do you know if it exists assertion macros alà JUnit for XCode (OCUnit)? Also, is it possible to get the red/green bar in XCode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先要注意的是 OCUnit(又名 SenTestingKit.framework)与 Xcode 集成,但实际上并不是 Xcode 的一部分。 OCUnit 最初是第三方代码,后来成为 Objective-C 单元测试的事实上标准,因此 Apple 采用了它并现在维护它。
更重要的是,您看到的输出似乎有些奇怪。我正在使用 Snow Leopard 附带的 Xcode 3.2.1。我尝试了以下测试:
以下是我在 Xcode 构建结果窗格/窗口中看到的错误:
当我双击构建日志中的错误时,Xcode 直接跳转到失败断言的行。
OCUnit 宏当然并不完美,但上面使用的示例非常冗长。宏需要 2 个以上或 3 个以上参数。 (
STFail
是例外,只需要 1+ 个参数。)最后一个必需的参数始终是描述的可选格式字符串,任何其他参数都用于替换这些占位符,就像您一样可以使用printf()
或NSLog()
。如果您传递nil
,您只会得到默认错误,而没有额外的详细信息。我通常只在测试确实需要上下文时添加描述。例如,测试和/或断言的主题的实际含义是什么。通常,我只是将此信息作为断言周围的注释包含在内。越简单越好。 :-)
为了回答你的最后一个问题,目前没有办法在 Xcode 中获得红色/绿色条,就像你在 JUnit 中看到的那样。这可能是一个很好的补充,但我个人认为并不是至关重要的。 YMMV。
The first thing to be aware of is that OCUnit (aka SenTestingKit.framework) are integrated with Xcode, but not really a part of Xcode proper. OCUnit started as third-party code and became the de facto standard for Objective-C unit testing, so Apple adopted it and now maintains it.
More to the point, the output you're seeing seems somewhat odd. I'm using Xcode 3.2.1 which comes with Snow Leopard. I tried the following test:
Here's are the errors I see in the Xcode build results pane/window:
When I double-click on the error in the build log, Xcode jumps directly to the line of the failed assertion.
The OCUnit macros certainly aren't perfect, but the example you used above was incredibly verbose. The macros require either 2+ or 3+ arguments. (
STFail
is the exception, and only requires 1+ arguments.) The last required argument is always an optional format string for a description, and any other parameters are used to substitute in those placeholders, just like you'd do withprintf()
orNSLog()
. If you passnil
, you just get the default error without extra detail.I generally only add a description when the test really requires context. For example, what the test and/or the subject(s) of the assertion actually mean. More often than not, I just include this information as comments around the assertion. Simpler is better. :-)
To answer your last question, there's not currently a way to get a red/green bar in Xcode like you'd see with JUnit. That might be a nice addition, but not something I'd personally consider critical. YMMV.
正如其他人所说,您可以通过传递 nil 作为最后一个参数来使宏更容易理解。如果测试失败,这将为您提供默认输出。当然,您可以根据需要提供自己的字符串。我经常发现这对于具有返回
BOOL
或id
但通过引用获取NSError*
的方法的代码很有用,如下所示:红色/绿色条,您确实会在构建窗口中得到红色/绿色结果作为构建的最后一步,但它不像在其他 IDE 中那样可见。有很多优秀的心理学文献表明,让它更加突出将是一个好主意。请务必在 bugreport.apple.com 提交增强请求。您可以参考
rdar://7685315
(我的票证)。As others have said, you can make the macros a little easier to stomach by passing
nil
as the last argument. This will get you the default output if the test fails. You can, of course, supply your own string when you want. I often find this is useful for code that has methods that return aBOOL
or anid
but take anNSError*
by reference like this:Regarding the red/green bar, you do get a red/green result as the last step of the build in the build window, but it's not as visible as in other IDEs. There's lots of good psychology literature suggesting that making it much more prominent would be a good idea. Definitely file an enhancement request at bugreport.apple.com. You can reference
rdar://7685315
(my ticket to this effect).现在有一个很棒的框架可用,称为 OCHamcrest。它允许您的测试断言读起来像一个句子,并且通常您不必提供失败描述,这对我来说是我在测试中最不想做的事情,因为它必须维护。
这是 github https://github.com/hamcrest/OCHamcrest
中的示例
这是自述文件 断言对 OCUnit 非常友好,添加您自己的断言也相当简单。
There is a great framework available now called OCHamcrest. It allows your test assertions to read like a sentence and in general you don't have to provide a failure description, which for me is the last thing I wan't to do in a test because it has to be maintained.
Here is the github https://github.com/hamcrest/OCHamcrest
Here is a sample from the readme
The assertions are very OCUnit friendly and adding your own assertions is fairly straight forward.
为什么不为此创建自己的包装器呢?
OCUnit 将运行结果转储到控制台窗口中,抱歉,没有 GUI 集成。我觉得这很方便,但我已经习惯了。
Why not create your own wrapper for that?
OCUnit dumps the results of the run into the console window, there's no GUI integration, sorry. I find it's fairly convenient, but I'm used to it.