OCUnit 中的简化断言

发布于 2024-08-21 18:42:12 字数 532 浏览 12 评论 0原文

我刚开始使用 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.

alt text

What I tried so far i XCode is:

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陪你搞怪i 2024-08-28 18:42:12

首先要注意的是 OCUnit(又名 SenTestingKit.framework)与 Xcode 集成,但实际上并不是 Xcode 的一部分。 OCUnit 最初是第三方代码,后来成为 Objective-C 单元测试的事实上标准,因此 Apple 采用了它并现在维护它。

更重要的是,您看到的输出似乎有些奇怪。我正在使用 Snow Leopard 附带的 Xcode 3.2.1。我尝试了以下测试:

- (void) testNumbers {
    int number1 = 2;
    int number2 = 3;
    STAssertEquals(number1, number2, nil);
    STAssertEquals(4, 5, nil);
}

以下是我在 Xcode 构建结果窗格/窗口中看到的错误:

-[ExampleTest testNumbers] : '2' should be equal to '3'
-[ExampleTest testNumbers] : '4' should be equal to '5'

当我双击构建日志中的错误时,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:

- (void) testNumbers {
    int number1 = 2;
    int number2 = 3;
    STAssertEquals(number1, number2, nil);
    STAssertEquals(4, 5, nil);
}

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'

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 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. :-)

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.

长亭外,古道边 2024-08-28 18:42:12

正如其他人所说,您可以通过传递 nil 作为最后一个参数来使宏更容易理解。如果测试失败,这将为您提供默认输出。当然,您可以根据需要提供自己的字符串。我经常发现这对于具有返回 BOOLid 但通过引用获取 NSError* 的方法的代码很有用,如下所示

- (void)testFoo {
  NSError *err;
  STAssertTrue([bar fooMethodReturningBOOLError:&err], @"Error: %@ (%@)", err, [err userInfo]);
}

:红色/绿色条,您确实会在构建窗口中得到红色/绿色结果作为构建的最后一步,但它不像在其他 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 a BOOL or an id but take an NSError* by reference like this:

- (void)testFoo {
  NSError *err;
  STAssertTrue([bar fooMethodReturningBOOLError:&err], @"Error: %@ (%@)", err, [err userInfo]);
}

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).

北渚 2024-08-28 18:42:12

现在有一个很棒的框架可用,称为 OCHamcrest。它允许您的测试断言读起来像一个句子,并且通常您不必提供失败描述,这对我来说是我在测试中最不想做的事情,因为它必须维护。

这是 github https://github.com/hamcrest/OCHamcrest

中的示例

NSCalendarDate* date = [NSCalendarDate dateWithString:@"26 Apr 2008" calendarFormat:@"%d %b %Y"];
assertThat(date, is(onASaturday()))

这是自述文件 断言对 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

NSCalendarDate* date = [NSCalendarDate dateWithString:@"26 Apr 2008" calendarFormat:@"%d %b %Y"];
assertThat(date, is(onASaturday()))

The assertions are very OCUnit friendly and adding your own assertions is fairly straight forward.

怀念你的温柔 2024-08-28 18:42:12

为什么不为此创建自己的包装器呢?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文