单元测试 Objective-C 代码的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:44:03  来源:igfitidea点击:

What is the best way to unit test Objective-C code?

objective-ccocoaunit-testingxcode

提问by Kim

What frameworks exist to unit test Objective-C code? I would like a framework that integrates nicely with Apple Xcode.

有哪些框架可以对 Objective-C 代码进行单元测试?我想要一个与 Apple Xcode 很好地集成的框架。

采纳答案by Chris Hanson

Xcode includes XCTest, which is similar to OCUnit, an Objective-C unit testing framework, and has full support for running XCTest-based unit tests as part of your project's build process. Xcode's unit testing support is described in the Xcode Overview: Using Unit Tests.

Xcode 包括 XCTest,它类似于OCUnit,一个 Objective-C 单元测试框架,并且完全支持运行基于 XCTest 的单元测试作为项目构建过程的一部分。Xcode 的单元测试支持在Xcode 概述:使用单元测试中进行了描述

Back in the Xcode 2 days, I wrote a series of weblog posts about how to perform some common tasks with Xcode unit testing:

回到 Xcode 2 天,我写了一系列关于如何使用 Xcode 单元测试执行一些常见任务的博客文章:

Despite using OCUnit rather than XCTest, the concepts are largely the same.

尽管使用 OCUnit 而不是 XCTest,但概念基本相同。

Finally, I also wrote a few posts on how to write tests for Cocoa user interfaces; the way Cocoa is structured makes it relatively straightforward, because you don't have to spin an event loop or anything like that in most cases.

最后,我还写了几篇关于如何为 Cocoa 用户界面编写测试的文章;Cocoa 的结构使其相对简单,因为在大多数情况下您不必旋转事件循环或类似的东西。

This makes it possible to do test-driven development for not just your model-level code but also your controller-level and even view-level code.

这使得不仅可以对模型级代码进行测试驱动开发,还可以对控制器级甚至视图级代码进行测试驱动开发。

回答by Paul Robinson

Check out GHUnitby Gabriel Handford:

查看Gabriel Handford 的GHUnit

"The goals of GHUnit are:

Runs unit tests within XCode, allowing you to fully utilize the XCode Debugger. A simple GUI to help you visualize your tests. Show stack traces. Be installable as a framework (for Cocoa apps) with a simple (or not) target setup; or easy to package into your iPhone project."

“GHUnit 的目标是:

在 XCode 中运行单元测试,允许您充分利用 XCode 调试器。一个简单的 GUI,可帮助您可视化测试。显示堆栈跟踪。可作为一个框架(用于 Cocoa 应用程序)安装,具有简单(或不)目标设置;或易于打包到您的 iPhone 项目中。”

回答by Ryan Townshend

I started using the Google toolbox testing rig for iPhone, and its working out great for me.

我开始使用适用于 iPhone 的 Google 工具箱测试设备,它对我来说效果很好。

google-toolbox-for-mac

google-toolbox-for-mac

回答by Mike Caron

Check out OCUnit. Apple's developer network has a great introduction.

查看OCUnit。苹果的开发者网络有一个很好的介绍

回答by Clint Harris

Note that the Google Toolbox for Mac (GTM) project simply extends/augments Apple's SenTestingKit framework (which is, itself based on OCUnit). As they say on the project site:

请注意,Google Toolbox for Mac (GTM) 项目只是扩展/增强了 Apple 的 SenTestingKit 框架(它本身基于 OCUnit)。正如他们在项目现场所说:

GTM has several enhancement to the standard SenTestingKit allowing you to do UI unit testing, automated binding unit testing, log tracking, and unit testing on the iPhone, as well as tools for doing static and dynamic testing of your code.

GTM 对标准 SenTestingKit 进行了多项增强,允许您在 iPhone 上进行 UI 单元测试、自动绑定单元测试、日志跟踪和单元测试,以及对代码进行静态和动态测试的工具。

Note the following comment about user-interface testing:

请注意以下有关用户界面测试的评论:

GTM has extensive support for user interface unit tests. It supports testing both the imaging and/or internal state of almost all of the standard Cocoa/UIKit UI objects, and makes it easy for you to extend this support to your own UI objects.

GTM 广泛支持用户界面单元测试。它支持测试几乎所有标准 Cocoa/UIKit UI 对象的成像和/或内部状态,并使您可以轻松地将此支持扩展到您自己的 UI 对象。

See their "Code Verification and Unit Testing" page for instructions on how to use it.

有关如何使用它的说明,请参阅他们的“代码验证和单元测试”页面。

回答by Adam Milligan

I realize this is an old question, but if you prefer BDD-style testing (rspec, Jasmine, etc.) over xUnit-style testing (Test::Unit, JSUnit, JUnit, etc.), then you may consider checking out Cedar. Cedar brings BDD-style testing to Objective-C, now that the language supports closures.

我意识到这是一个老问题,但是如果您更喜欢 BDD 风格的测试(rspec、Jasmine 等)而不是 xUnit 风格的测试(Test::Unit、JSUnit、JUnit 等),那么您可以考虑查看Cedar. Cedar 为 Objective-C 带来了 BDD 风格的测试,现在该语言支持闭包。

We're happily using Cedar for our iOS projects at Pivotal Labs, and we're actively working on improving it. Any feedback or suggestions are welcome at [email protected]

我们很高兴在Pivotal Labs 的iOS 项目中使用 Cedar ,并且我们正在积极致力于改进它。欢迎在 [email protected] 提供任何反馈或建议

回答by Johannes Rudolph

I came to the conclusion that GHUnit is the most advanced testing framework for Objective-C. I have done a roundupof testing frameworks on my blog. It is the most flexible in terms of deployment (iphone, simulator or mac os native) and assert capabilities. Because it is based on GTM, it inherits all of GTM's advantages over SenTestingKit but also adds a lot more. Another bonus is that it is being maintained very actively.

我得出的结论是 GHUnit 是 Objective-C 最先进的测试框架。我在我的博客上做了一个测试框架的综述。它在部署(iphone、模拟器或 mac os native)和断言功能方面是最灵活的。因为它基于 GTM,所以它继承了 GTM 相对于 SenTestingKit 的所有优点,但还增加了很多。另一个好处是它得到了非常积极的维护。

I have conducted effort to integrate OCMockinto GHUnit, it works great!. You can get the code on github.

我已经努力将OCMock集成到 GHUnit 中,效果很好!你可以在github上获取代码。

回答by Nikita Zhuk

I would also recommend using coverage tools to see which part of the code are covered with unit tests and which are not. Basic line and branch code coverage can be generated with the GCOVtool. If you want to generate nice HTML coverage reports there are LCOVand ZCOVwhich do just that.

我还建议使用覆盖工具来查看代码的哪些部分被单元测试覆盖,哪些没有。可以使用GCOV工具生成基本的行和分支代码覆盖率。如果您想生成不错的 HTML 覆盖率报告,则可以使用LCOVZCOV

回答by Joel Levin

I recommend gh-unit, it has a nice GUI for test results.

我推荐 gh-unit,它有一个很好的 GUI 测试结果。

http://github.com/gabriel/gh-unit/tree/master

http://github.com/gabriel/gh-unit/tree/master

回答by nst

Sen:te (the creator of the testing framework included with Xcode) explains how to use OCUnit with an iPhone project: simple-iphone-ipad-unit-test.

Sen:te(Xcode 中包含的测试框架的创建者)解释了如何在 iPhone 项目中使用 OCUnit:simple-iphone-ipad-unit-test