使用 Objective-C 实现简单的单元测试

发布于 2024-12-19 17:30:56 字数 455 浏览 3 评论 0原文

在《Ruby 编程》中介绍了单元测试的概念之后,我最近开始喜欢上了单元测试。我知道 Xcode 中有一个偏好“包含单元测试”,所以我尝试了它。首先,很难找到该框架的文档,其次,向 Foundation 命令行工具添加单元测试很麻烦。所有这些都是单独的方案和目标等。

这让我考虑编写一个极其简单且易于包含的单元测试框架,主要用于教育目的。

我的想法是,您将编写一个启动和清理方法以及从测试开始的方法。然后我会编写 5-6 个断言函数。 (像大多数框架一样)然后在 main() 中你会说:

[MyAppTests test];

或者类似的东西。然后将运行所有测试方法并记录错误或成功。然后我可以在未来以此为基础。

我的问题是,如何实现可以使用 Foundation 或者 C 标准库在 Objective-C 中运行从测试开始的所有方法?对于这样的事情来说,最佳/最小的设计是什么?

I've recently been loving unit testing after being introduced to the idea in Programming Ruby. I knew there was a preference, 'Include Unit Tests' in Xcode, so I tried it. First of all it was very hard to find documentation for the framework and secondly it's a hassle to add unit tests to a Foundation command-line tool. With all of it's separate schemes and targets etc.

This got me thinking about writing a extremely minimal and easy to include unit testing framework, mainly for educational purposes.

My idea is, you would write a startup and cleanup method and methods beginning with test. Then I would write 5-6 assert functions. (Like most frameworks) Then in main() you would say something like:

[MyAppTests test];

Or something similar. And then all the test methods would be run and the errors, or successes logged. Then I could build on that in future.

My questions are, how would one implement that you could run all methods beginning with test in Objective-C using Foundation and maybe the C standard library? And what would be an optimal / minimal design for such a thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小ぇ时光︴ 2024-12-26 17:30:56

使用 Objective-C 运行时参考来了解如何获取方法名称列表。然后,您可以过滤掉设置和清理方法,或者仅包含以“test”开头的方法。我很确定这就是内置 SenTest 框架在内部所做的事情。

http://developer.apple.com/ library/mac/#documentation/cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

当我经常使用这些 C 函数时,我通常更喜欢将它们包装在实用程序类,或者在适当的情况下,类别。

Use the Objective-C Runtime Reference to see how to get lists of method names. You could then filter out the setup and cleanup methods or only include methods that start with "test", for example. I'm pretty sure that this is what the built in SenTest framework does internally.

http://developer.apple.com/library/mac/#documentation/cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

When I make regular use of C functions like these I often prefer to wrap them in a utility class or, when appropriate, a category.

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