/blog/2015/01/29/xctestdan-yuan-ce-shi/index.html
HTML | 309 lines | 247 code | 58 blank | 4 comment | 0 complexity | deb93ece6c4cb61af25bee12624b43a9 MD5 | raw file
- <!DOCTYPE html>
- <!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
- <!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
- <!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
- <head>
- <meta charset="utf-8">
- <title>XCTest单元测试 - 北武灵尊</title>
- <meta name="author" content="lingzuer">
-
- <meta name="description" content="QA的同事说帮他们讲一下OC,我自己都是个搓鸟,还给别人讲,就当是玩笑拒绝了,那么多大牛都在,找我讲?后来又说让我讲,是认真的,那我想,反正他们也不会,忽悠一下他们还是OK的了,简单的写个Hello World还是不成问题的,于是就去讲了,讲到后来才明白他们想的最终需求是:单元测试。 …">
-
- <!-- http://t.co/dKP3o1e -->
- <meta name="HandheldFriendly" content="True">
- <meta name="MobileOptimized" content="320">
- <meta name="viewport" content="width=device-width, initial-scale=1">
-
- <link rel="canonical" href="http://lingzuer.github.io/blog/2015/01/29/xctestdan-yuan-ce-shi">
- <link href="/favicon.png" rel="icon">
- <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
- <link href="/atom.xml" rel="alternate" title="北武灵尊" type="application/atom+xml">
- <script src="/javascripts/modernizr-2.0.js"></script>
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
- <script>!window.jQuery && document.write(unescape('%3Cscript src="/javascripts/libs/jquery.min.js"%3E%3C/script%3E'))</script>
- <script src="/javascripts/octopress.js" type="text/javascript"></script>
- <!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
- <link href="//fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
- <link href="//fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
-
- </head>
- <body >
- <header role="banner"><hgroup>
- <h1><a href="/">北武灵尊</a></h1>
-
- <h2>记录一些事情,方便查阅。</h2>
-
- </hgroup>
- </header>
- <nav role="navigation"><ul class="subscription" data-subscription="rss">
- <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
-
- </ul>
-
- <form action="https://www.google.com/search" method="get">
- <fieldset role="search">
- <input type="hidden" name="sitesearch" value="lingzuer.github.io">
- <input class="search" type="text" name="q" results="0" placeholder="Search"/>
- </fieldset>
- </form>
-
- <ul class="main-navigation">
- <li><a href="/">Blog</a></li>
- <li><a href="/blog/archives">Archives</a></li>
- </ul>
- </nav>
- <div id="main">
- <div id="content">
- <div>
- <article class="hentry" role="article">
-
- <header>
-
- <h1 class="entry-title">XCTest单元测试</h1>
-
-
- <p class="meta">
-
- <time class='entry-date' datetime='2015-01-29T22:14:13+08:00'><span class='date'><span class='date-month'>Jan</span> <span class='date-day'>29</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>10:14 pm</span></time>
-
- </p>
-
- </header>
- <div class="entry-content"><p>QA的同事说帮他们讲一下OC,我自己都是个搓鸟,还给别人讲,就当是玩笑拒绝了,那么多大牛都在,找我讲?后来又说让我讲,是认真的,那我想,反正他们也不会,忽悠一下他们还是OK的了,简单的写个Hello World还是不成问题的,于是就去讲了,讲到后来才明白他们想的最终需求是:单元测试。作为开发我从来没有了解过这一块,我说自己回去先写个Demo。于是上网各种搜索,还是有些收获的,昨天晚上到12:40,对UI和属性的测试有了点思路,完成一个简单的Demo,就当是单元测试的Hello World吧,今天上午又搞定了异步请求的单元测试,结合自己项目(方便写登录的请求)写了一个Demo,这也是列为今天todo list的第一个任务。现在记录一下。</p>
- <p>1、新建一个测试的Target</p>
- <p><img src="/images/blog/test-target.png" alt="Test Target" /></p>
- <p>2、设置Search Paths
- 保持和项目的Target设置一致,不然在引用文件的时候,可能会提示有些文件找不到,比如:LoginManager.h:10:9: fatal error: ‘biz/service/auth/auth_protocol.h’ file not found</p>
- <p><img src="/images/blog/search-paths.png" alt="Search Paths" /></p>
- <p>3、新建Test Class</p>
- <p>见图一。</p>
- <p>4、引入需要的文件开始测试
- 比如要写一个登录的单元测试,那要引入LoginManager.h之类的文件吧,我大概写一下,有些是项目的代码,不知道写在博文里面好不好。</p>
- <pre><code>#import <UIKit/UIKit.h>
- #import <XCTest/XCTest.h>
- #import "LoginManager.h"
- #import "LoginCallBack.h"
- @interface MyTests : XCTestCase {
- XCTestExpectation *_expectation;
- }
- @end
- @implementation MyTests
- - (void)setUp {
- [super setUp];
- [self addListenEvents];
- }
- - (void)tearDown {
- [self removeListenEvents];
- [super tearDown];
- }
- #pragma mark - 通知
- - (void)addListenEvents {
- extern NSString *kYIXINNotificationLoginResult;
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onGetLoginResult:) name:kYIXINNotificationLoginResult object:nil];
- }
- - (void)removeListenEvents {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark - Test Methods
- - (void)testUserLogin {
- _expectation = [self expectationWithDescription:@"Login request"];
- NSString *username = @"userID";
- NSString *password = @"111111";
- [LoginManager sharedManager].currentLoginData.userName = username;
- [LoginManager sharedManager].currentLoginData.userPassword = [XXUtil encytePassword:password];
- [LoginManager sharedManager].currentLoginData.type = kAccountYid;
- [[LoginManager sharedManager] beginLogin];
- [self waitForExpectationsWithTimeout:5
- handler:^(NSError *error) {
- // handler is called on _either_ success or failure
- if (error != nil) {
- XCTFail(@"timeout error: %@", error);
- }
- }];
- }
- #pragma mark - LoginResultProtocol
- - (void)onGetLoginResult:(NSNotification*)aNotification {
- extern NSString *kLoginStepKey;
- extern NSString *kLoginResultKey;
- NSDictionary *data = aNotification.userInfo;
- NSInteger step = [[data objectForKey:kLoginStepKey] intValue];
- NSInteger errorCode = [[data objectForKey:kLoginResultKey] intValue];
- if (step == kLoginStepLogin) {
- [_expectation fulfill];
- if (errorCode == kResSuccess) {
- XCTAssert(YES, @"Pass");
- } else {
- XCTAssert(NO, @"No Pass");
- }
- } else {
- NSLog(@ "login result: step is %@, code is %@",@(step), @(errorCode));
- }
- }
- @end
- </code></pre>
- <p>5、异步测试要点</p>
- <pre><code>_expectation = [self expectationWithDescription:@"Login request”];
- [_expectation fulfill];
- XCTAssert(YES, @"Pass");
- </code></pre>
- <p>6、其他</p>
- <p>其他的异步测试比如Block、Delegate的方式也都如此(采用XCTestExpectation)。</p>
- <p>7、参考资料</p>
- <ul>
- <li><a href="http://objccn.io/issue-15-2/">http://objccn.io/issue-15-2/</a></li>
- <li><a href="http://www.objc.io/issue-15/xctest.html">http://www.objc.io/issue-15/xctest.html</a></li>
- <li><a href="http://www.bignerdranch.com/blog/asynchronous-testing-with-xcode-6/">http://www.bignerdranch.com/blog/asynchronous-testing-with-xcode-6/</a></li>
- <li><a href="http://blog.dadabeatnik.com/2014/07/13/asynchronous-unit-testing-in-xcode-6/">http://blog.dadabeatnik.com/2014/07/13/asynchronous-unit-testing-in-xcode-6/</a></li>
- <li><a href="http://www.pumpmybicep.com/2014/08/22/objective-c-http-stubbing-libraries/">http://www.pumpmybicep.com/2014/08/22/objective-c-http-stubbing-libraries/</a></li>
- <li><a href="http://iosunittesting.com/asynchronous-tests-using-xctestexpectation/">http://iosunittesting.com/asynchronous-tests-using-xctestexpectation/</a></li>
- <li><a href="http://www.pumpmybicep.com/2014/08/20/asynchronous-unit-testing-with-xctest/">http://www.pumpmybicep.com/2014/08/20/asynchronous-unit-testing-with-xctest/</a></li>
- <li><a href="http://nshipster.com/xctestcase/">http://nshipster.com/xctestcase/</a></li>
- <li><a href="http://ocmock.org">http://ocmock.org</a></li>
- </ul>
- </div>
- <footer>
- <p class="meta">
-
-
- <span class="byline author vcard">Posted by <span class="fn">lingzuer</span></span>
-
- <time class='entry-date' datetime='2015-01-29T22:14:13+08:00'><span class='date'><span class='date-month'>Jan</span> <span class='date-day'>29</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>10:14 pm</span></time>
-
- </p>
-
- <div class="sharing">
-
-
-
- </div>
-
- <p class="meta">
-
- <a class="basic-alignment left" href="/blog/2015/01/28/hello-to-myself/" title="Previous Post: Hello To Myself">« Hello To Myself</a>
-
-
- <a class="basic-alignment right" href="/blog/2015/01/30/ji-lei-de-jiao-ben/" title="Next Post: 积累的脚本">积累的脚本 »</a>
-
- </p>
- </footer>
- </article>
- </div>
- <aside class="sidebar">
-
- <section>
- <h1>Recent Posts</h1>
- <ul id="recent_posts">
-
- <li class="post">
- <a href="/blog/2015/02/06/yi-xin-nian-hui/">易信年会</a>
- </li>
-
- <li class="post">
- <a href="/blog/2015/02/05/alassetslibraryxuan-ze-tu-pian-you-hua/">ALAssetsLibrary选择图片优化</a>
- </li>
-
- <li class="post">
- <a href="/blog/2015/02/02/assetslibraryzi-ding-yi-tu-pian-xuan-ze-qi/">AssetsLibrary自定义图片选择器</a>
- </li>
-
- <li class="post">
- <a href="/blog/2015/02/01/chang-yong-gong-ju/">常用工具</a>
- </li>
-
- <li class="post">
- <a href="/blog/2015/01/31/xctestce-shi-shu-xing-bian-hua/">XCTest测试属性变化</a>
- </li>
-
- </ul>
- </section>
-
- </aside>
- </div>
- </div>
- <footer role="contentinfo"><p>
- Copyright © 2015 - lingzuer -
- <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
- </p>
- </footer>
-
- </body>
- </html>