OCUnit中的简化断言 [英] Simplified asserts in OCUnit
问题描述
我刚刚开始使用OCUnit,发现断言有点麻烦.在JUnit中,我可以编写一个测试来比较数字,如下所示.该测试显然会失败,但这显示了我可以写出两个数字的优美而简单的断言,并且得到的反馈是:期望< 2>但是< 3>",并且代码很少.
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.
到目前为止,我在XCode上尝试的是:
What I tried so far i XCode is:
可以工作,但是不如JUnit优雅.您是否知道是否存在XUnit的断言宏JUnit(OCUnit)?另外,是否有可能在XCode中获得红色/绿色条?
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?
推荐答案
首先要注意的是OCUnit(又名SenTestingKit.framework)已与Xcode集成在一起,但实际上不是 part Xcode正确. OCUnit最初是第三方代码,后来成为Objective-C单元测试的事实标准,因此Apple采纳了该代码,现在对其进行了维护.
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.
更重要的是,您看到的输出似乎有些奇怪.我正在使用Snow Leopard随附的Xcode 3.2.1.我尝试了以下测试:
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:
- (void) testNumbers {
int number1 = 2;
int number2 = 3;
STAssertEquals(number1, number2, nil);
STAssertEquals(4, 5, nil);
}
这是我在Xcode构建结果窗格/窗口中看到的错误:
Here's are the errors I see in the Xcode build results pane/window:
-[ExampleTest testNumbers] : '2' should be equal to '3'
-[ExampleTest testNumbers] : '4' should be equal to '5'
当我双击构建日志中的错误时,Xcode会直接跳到失败的断言所在的行.
When I double-click on the error in the build log, Xcode jumps directly to the line of the failed assertion.
OCUnit宏当然不是完美的,但是您上面使用的示例非常冗长.宏需要2+或3+自变量. (STFail
是例外,仅需要1个以上的参数.)最后一个必需参数始终是描述的可选格式字符串,并且使用任何其他参数替换这些占位符,就像您对printf()
或NSLog()
.如果通过nil
,则只会得到默认错误,而没有额外的细节.
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 with printf()
or NSLog()
. If you pass nil
, 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. :-)
要回答最后一个问题,目前还没有一种方法可以像在JUnit中看到的那样在Xcode中获得红色/绿色条.这可能是一个不错的补充,但我个人认为并不重要. YMMV.
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.
这篇关于OCUnit中的简化断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!