简体   繁体   English

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

[英]Implementing Simple Unit Tests with Objective-C

I've recently been loving unit testing after being introduced to the idea in Programming Ruby. 在Ruby编程中被介绍后,我最近一直喜欢单元测试。 I knew there was a preference, 'Include Unit Tests' in Xcode, so I tried it. 我知道Xcode中有一个首选项,“包含单元测试”,所以我尝试了。 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. 首先,很难找到该框架的文档,其次,将单元测试添加到Foundation命令行工具很麻烦。 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. 我的想法是,您将编写一个启动和清除方法以及以test开头的方法。 Then I would write 5-6 assert functions. 然后,我将编写5-6个断言函数。 (Like most frameworks) Then in main() you would say something like: (就像大多数框架一样)然后在main()中,您会说:

[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? 我的问题是,如何实现一个可以使用Foundation和C标准库在Objective-C中运行从测试开始的所有方法的工具? And what would be an optimal / minimal design for such a thing? 对于这样的事情,最佳/最小的设计是什么?

Use the Objective-C Runtime Reference to see how to get lists of method names. 使用《 Objective-C运行时参考》查看如何获取方法名称列表。 You could then filter out the setup and cleanup methods or only include methods that start with "test", for example. 然后,您可以过滤掉设置和清除方法,或者仅包含以“ test”开头的方法。 I'm pretty sure that this is what the built in SenTest framework does internally. 我很确定这是内置的SenTest框架在内部执行的操作。

http://developer.apple.com/library/mac/#documentation/cocoa/Reference/ObjCRuntimeRef/Reference/reference.html 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. 当我经常使用此类C函数时,我通常更喜欢将它们包装在实用工具类或适当的类别中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM