简体   繁体   English

iPhone Xcode是否可以在不加载模拟器的情况下测试方法?

[英]iPhone Xcode Is there a way to test methods without loading up the simulator?

I'm trying to test some simple string manipulation methods I need for my iPhone project. 我正在尝试测试我的iPhone项目所需的一些简单的字符串操作方法。 I was wondering if there was anyway of testing the results of methods without loading the simulator each time.. 我想知道是否有在不加载模拟器的情况下测试方法结果的方法。

for instance in my main.m method: 例如在我的main.m方法中:

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {
        testMethod();
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}


void testMethod() {
    NSString *body = @"{\"username\":\"name\",\"password\":\"123\"}";
    NSLog(@"body=%@",body);
}

All I want to do is run this code and see what the console outputs (no simulator needed). 我要做的就是运行此代码,并查看控制台输出的内容(无需模拟器)。 Is this possible in xCode? 在xCode中可以吗?

Not really, no. 不是,不是 If you really want to, you could create a new Xcode project as just a "command line utility" (under the Mac OS X templates) that'd run faster because it doesn't have to load the simulator. 如果确实需要,您可以创建一个新的Xcode项目,作为一个“命令行实用程序”(在Mac OS X模板下),因为它不必加载模拟器,因此运行速度更快。 If you're just doing NSString manipulation, the same code will work on Mac OS X. 如果您只是在进行NSString操作,则相同的代码将在Mac OS X上工作。

This is a perfect situation for Unit Testing . 这是单元测试的理想情况。 For iPhone development, I've been using GH-Unit . 对于iPhone开发,我一直在使用GH-Unit

There's going to be some overhead setting up GH-Unit and learning how to write proper unit tests, but the payoff will be that: 设置GH-Unit并学习如何编写正确的单元测试将有一些开销,但是这样做的好处是:

  1. You'll be able to quickly write and test your method and be confident that it's working correctly. 您将能够快速编写和测试您的方法,并确信它可以正常工作。
  2. The tests will exist in perpetuity, so you can refactor your test method and be sure that you aren't breaking anything. 测试将永久存在,因此您可以重构测试方法并确保您没有破坏任何东西。

The biggest benefit of unit testing that I never hear anyone talk about is how quickly it allows you to fix bugs. 我从未听过任何人谈论过的单元测试的最大好处是,它使您能够快速修复错误。 When you or a tester finds bug in your app, you can quickly write a unit test to recreate the situation that causes the bug, re-write the code to handle the bug, then run the tests, all without ever compiling and running your app. 当您或测试人员在您的应用程序中发现错误时,您可以快速编写单元测试来重新创建导致该错误的情况,重新编写代码以处理该错误,然后运行测试,而无需编译和运行您的应用程序。 For bugs buried deep in a navigation hierarchy, this is a huge win. 对于深埋在导航层次结构中的错误,这是一个巨大的胜利。

One other benefit I just remembered, good unit tests let you walk through using your classes/methods in practice mode, before you write a whole bunch of code that depends on a specific interface. 我刚刚记得的另一个好处是,良好的单元测试可以让您逐步学习练习模式下的类/方法,然后再编写一堆取决于特定接口的代码。 This is a great way to ferret out those awkward little workflows that seemed like a great idea until you had to actually start using them. 这是找出那些笨拙的小工作流程的好方法,这些工作流程似乎是一个好主意,直​​到您必须真正开始使用它们为止。

Yet another benefit: self documentation. 另一个好处是:自我记录。 If you ever need to give someone sample code on how to use your classes, just copy and paste code out of a unit test. 如果您需要向某人提供有关如何使用类的示例代码,只需将代码复制并粘贴到单元测试中即可。

I don't do test-driven-development and I'm not anal about 100% coverage, but as you can probably tell from this post, I love me some unit tests. 我不进行测试驱动的开发,也不是100%的覆盖率,但是正如您可能从这篇文章中可以看出的那样,我爱我一些单元测试。 I used to jump through all kinds of awkward hoops, setting up little terminal-based projects to let me test my classes without the overhead of running the entire app. 我过去经常经历各种尴尬的事情,建立了一些基于终端的小项目,以使我能够测试类,而无需运行整个应用程序。 After a while I realized how stupid it all was and put my mind to learning how to test my classes. 一段时间后,我意识到这一切都是多么愚蠢,并下定决心要学习如何测试我的课程。 It's made a huge difference in my productivity. 这极大地提高了我的生产力。


@jlehr suggested OCUnit, which does have better integration with Xcode and is a lot easier to set up. @jlehr建议使用OCUnit,它与Xcode的集成更好,并且设置起来容易得多。 I would stay away from OCUnit on the iPhone, though. 不过,我会远离iPhone上的OCUnit。 I had a lot of issues running my unit tests in the debugger while in the simulator with OCUnit. 在使用OCUnit的模拟器中时,在调试器中运行单元测试时遇到很多问题。 If you can write perfect unit tests first try, maybe this isn't a problem :-D GHUnit lets you debug your unit tests right out of the box. 如果您可以尝试先编写完美的单元测试,也许这不是问题:-D GHUnit可让您立即调试单元测试。

This is just for iPhone dev, though. 不过,这仅适用于iPhone开发人员。 If you're on OS X, than OCUnit is a great choice for your unit tests. 如果您使用的是OS X,那么OCUnit是单元测试的绝佳选择。 There's a lot of other test frameworks out there, but GHUnit is the only one I've tried to use that works very well with iPhone development. 还有很多其他测试框架,但是GHUnit是我尝试使用的唯一一个可以与iPhone开发良好配合的测试框架。

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

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