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

时间:2022-09-02 10:19:23

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

单元测试Objective-C代码有哪些框架?我想要一个与Xcode完美集成的框架。

17 个解决方案

#1


311  

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.

这使测试驱动开发成为可能,不仅是您的模型级代码,还包括您的控制器级甚至视图级代码。

#2


50  

Check out GHUnit by Gabriel Handford:

看看加布里埃尔·汉德福德的GHUnit:

"The goals of GHUnit are:

“GHUnit的目标是:

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

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

#3


18  

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

我开始使用谷歌工具箱测试iPhone,它对我来说很好。

google-toolbox-for-mac

google-toolbox-for-mac

#4


13  

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

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

#5


12  

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:

请注意,Mac (GTM)项目的谷歌工具箱只是扩展了苹果的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有几个改进,允许您进行UI单元测试、自动绑定单元测试、日志跟踪,以及在iPhone上进行单元测试,以及对代码进行静态和动态测试的工具。

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.

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

#6


12  

I came to the conclusion that GHUnit is the most advanced testing framework for Objective-C. I have done a roundup of 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本机)和assert功能。因为它基于GTM,它继承了GTM在SenTestingKit上的所有优势,但也增加了很多。另一个好处是,它的维护非常积极。

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

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

#7


12  

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风格的测试(测试:单元、JSUnit、JUnit等等),那么您可以考虑检查雪松。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 cedar-discuss@googlegroups.com

我们很高兴地在关键的实验室里用雪松来做我们的iOS项目,我们正在积极地改进它。任何反馈或建议都欢迎在cedar-discuss@googlegroups.com。

#8


10  

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 GCOV tool. If you want to generate nice HTML coverage reports there are LCOV and ZCOV which do just that.

我还建议使用覆盖工具来查看代码中哪些部分覆盖了单元测试,哪些不是。可以使用GCOV工具生成基本行和分支代码覆盖率。如果你想要生成漂亮的HTML覆盖报告,就会有LCOV和ZCOV。

#9


7  

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

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

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

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

#10


7  

The Unit Testing support bundled within xcode (for its simple setup) combined with ocrunner (for some autotest/Growl goodness) is currently my favorite Obj-C Unit Testing setup.

在xcode中(简单的设置)和ocrunner(一些autotest/Growl的优点)结合的单元测试支持是目前我最喜欢的objc单元测试设置。

#11


7  

here is a whole lot of them

这里有很多。

List_of_unit_testing_frameworks in Objective-C

List_of_unit_testing_frameworks在objective - c中

#12


7  

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:简单- iPhone -ipad-单元测试。

#13


6  

Matt Gallagher of Cocoa with Love has a very good article on unit testing.

《爱的可可》的马特·加拉格尔有一篇很好的关于单元测试的文章。

#14


5  

I would suggest looking into Kiwi, an open source BDD testing framework for iOS: Kiwi

我建议您查看Kiwi,这是一个开放源码的BDD测试框架,用于iOS: Kiwi。

Check out the project's WIKI to start or get Daniel Steinberg's book "Test Driving iOS Development with Kiwi" test-driving-ios-development

查看项目的WIKI,以启动或获得Daniel Steinberg的“测试驱动iOS开发与Kiwi”测试驱动- iOS开发。

#15


4  

I use SimpleUnitTest works with iPhone and iPad libs.

我使用SimpleUnitTest与iPhone和iPad libs一起工作。

http://cbess.blogspot.com/2010/05/simple-iphone-ipad-unit-test.html

http://cbess.blogspot.com/2010/05/simple-iphone-ipad-unit-test.html

It comes with a unit test Xcode template to easily add a unit test class. Wraps GTM.

它附带一个单元测试Xcode模板,可以轻松添加单元测试类。包装GTM。

You can literally drop it into an active project and start adding unit tests within 3 minutes (or less).

您可以将其放入一个活动项目中,并在3分钟内开始添加单元测试(或更少)。

#16


3  

Specta is a modern TDD(Test Driven Development)/BDD(Behavior Driven Development) framework which runs on top of XCTest. It supports unit testing for iOS and Mac OS X projects.

Specta是一个现代TDD(测试驱动开发)/BDD(行为驱动开发)框架,它在XCTest之上运行。它支持iOS和Mac OS X项目的单元测试。

#17


0  

I hope u can use 'SenTestKit', from which u can test each and every method.

我希望你能使用“SenTestKit”,你可以用它来测试每一个方法。

#1


311  

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.

这使测试驱动开发成为可能,不仅是您的模型级代码,还包括您的控制器级甚至视图级代码。

#2


50  

Check out GHUnit by Gabriel Handford:

看看加布里埃尔·汉德福德的GHUnit:

"The goals of GHUnit are:

“GHUnit的目标是:

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

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

#3


18  

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

我开始使用谷歌工具箱测试iPhone,它对我来说很好。

google-toolbox-for-mac

google-toolbox-for-mac

#4


13  

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

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

#5


12  

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:

请注意,Mac (GTM)项目的谷歌工具箱只是扩展了苹果的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有几个改进,允许您进行UI单元测试、自动绑定单元测试、日志跟踪,以及在iPhone上进行单元测试,以及对代码进行静态和动态测试的工具。

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.

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

#6


12  

I came to the conclusion that GHUnit is the most advanced testing framework for Objective-C. I have done a roundup of 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本机)和assert功能。因为它基于GTM,它继承了GTM在SenTestingKit上的所有优势,但也增加了很多。另一个好处是,它的维护非常积极。

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

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

#7


12  

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风格的测试(测试:单元、JSUnit、JUnit等等),那么您可以考虑检查雪松。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 cedar-discuss@googlegroups.com

我们很高兴地在关键的实验室里用雪松来做我们的iOS项目,我们正在积极地改进它。任何反馈或建议都欢迎在cedar-discuss@googlegroups.com。

#8


10  

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 GCOV tool. If you want to generate nice HTML coverage reports there are LCOV and ZCOV which do just that.

我还建议使用覆盖工具来查看代码中哪些部分覆盖了单元测试,哪些不是。可以使用GCOV工具生成基本行和分支代码覆盖率。如果你想要生成漂亮的HTML覆盖报告,就会有LCOV和ZCOV。

#9


7  

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

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

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

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

#10


7  

The Unit Testing support bundled within xcode (for its simple setup) combined with ocrunner (for some autotest/Growl goodness) is currently my favorite Obj-C Unit Testing setup.

在xcode中(简单的设置)和ocrunner(一些autotest/Growl的优点)结合的单元测试支持是目前我最喜欢的objc单元测试设置。

#11


7  

here is a whole lot of them

这里有很多。

List_of_unit_testing_frameworks in Objective-C

List_of_unit_testing_frameworks在objective - c中

#12


7  

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:简单- iPhone -ipad-单元测试。

#13


6  

Matt Gallagher of Cocoa with Love has a very good article on unit testing.

《爱的可可》的马特·加拉格尔有一篇很好的关于单元测试的文章。

#14


5  

I would suggest looking into Kiwi, an open source BDD testing framework for iOS: Kiwi

我建议您查看Kiwi,这是一个开放源码的BDD测试框架,用于iOS: Kiwi。

Check out the project's WIKI to start or get Daniel Steinberg's book "Test Driving iOS Development with Kiwi" test-driving-ios-development

查看项目的WIKI,以启动或获得Daniel Steinberg的“测试驱动iOS开发与Kiwi”测试驱动- iOS开发。

#15


4  

I use SimpleUnitTest works with iPhone and iPad libs.

我使用SimpleUnitTest与iPhone和iPad libs一起工作。

http://cbess.blogspot.com/2010/05/simple-iphone-ipad-unit-test.html

http://cbess.blogspot.com/2010/05/simple-iphone-ipad-unit-test.html

It comes with a unit test Xcode template to easily add a unit test class. Wraps GTM.

它附带一个单元测试Xcode模板,可以轻松添加单元测试类。包装GTM。

You can literally drop it into an active project and start adding unit tests within 3 minutes (or less).

您可以将其放入一个活动项目中,并在3分钟内开始添加单元测试(或更少)。

#16


3  

Specta is a modern TDD(Test Driven Development)/BDD(Behavior Driven Development) framework which runs on top of XCTest. It supports unit testing for iOS and Mac OS X projects.

Specta是一个现代TDD(测试驱动开发)/BDD(行为驱动开发)框架,它在XCTest之上运行。它支持iOS和Mac OS X项目的单元测试。

#17


0  

I hope u can use 'SenTestKit', from which u can test each and every method.

我希望你能使用“SenTestKit”,你可以用它来测试每一个方法。