Xcode:为你的项目集成单元测试(unit tests)时记得避开这些坑

jopen 9年前
   <p><em>背景:项目已经创建大约两年,编程语言是常见的OC,项目里用到了cocoapods,也用到了swift,现在单元测试打算用swift语言(OC被抛弃只是时间问题)</em></p>    <ul>     <li> <p>创建项目时记得勾选『include Unit Tests』<br /> </p>      <div class="image-package" href="https://simg.open-open.com/show/d370ea87c849a43b31bdfd3351ff9cce.png">      <img src="https://simg.open-open.com/show/d370ea87c849a43b31bdfd3351ff9cce.png" width="700" height="496.71232876712327" data-original-src="https://simg.open-open.com/show/dd63216c9c57d5fb17310eef8d2bbdc0.png" />       <br />       <div class="image-caption"></div>      </div> <p><br /> 在项目创建时如果直接选择了包含单元测试是最方便的了。xcode7中的UI自动测试已经很完善,建议『include UI Tests』也一起选上。</p> </li>     <li> <p>已有项目,可以通过添加target的方式添加</p> </li>    </ul>    <div class="image-package" href="https://simg.open-open.com/show/08907334b1e2172819a8005cf112ed14.png">    <img src="https://simg.open-open.com/show/08907334b1e2172819a8005cf112ed14.png" width="500" height="238" data-original-src="https://simg.open-open.com/show/7ee82f3c40c53c28037c4956156904e7.png" />     <br />     <div class="image-caption"></div>    </div>    <p><br /> 选择iOS Unit Testing Bundle<br /> </p>    <div class="image-package" href="https://simg.open-open.com/show/efcb49df168b8927bc8daa276373f100.png">    <img src="https://simg.open-open.com/show/efcb49df168b8927bc8daa276373f100.png" width="700" height="496.71232876712327" data-original-src="https://simg.open-open.com/show/13e3d002e4bea8cf1e9be47d5e3e802b.png" />     <br />     <div class="image-caption">     在Test分类下选择     </div>    </div>    <ul>     <li>添加测试文件<br /> 在单元测试项目下,新建文件,选择source下的Unit Test Case Class。<br /> <em>tips:如果几个test case都有一些共同的数据或逻辑,可以自己新建一个test case的基类,然后每个分别继承</em><br />      <div class="image-package" href="https://simg.open-open.com/show/df79e1917712b778c151fcce17a56bbc.png">      <img src="https://simg.open-open.com/show/df79e1917712b778c151fcce17a56bbc.png" width="700" height="496.71232876712327" data-original-src="https://simg.open-open.com/show/5b69eaaa576ccfb4caafb2c9e7a02c1f.png" />       <br />       <div class="image-caption"></div>      </div> </li>     <li>将要测试的module导入到test case中<br /> import完你需要测试的项目名称后,就可以在这个类里欢快的调用里面的类了<pre class="brush:java; toolbar: true; auto-links: false;">@testable import YQImageCompress</pre></li>    </ul>    <div class="image-package" href="https://simg.open-open.com/show/9b6e84ebc5d68940039eec0bfab46561.png">    <img src="https://simg.open-open.com/show/9b6e84ebc5d68940039eec0bfab46561.png" width="638" height="368" data-original-src="https://simg.open-open.com/show/aab5fc0d970293e86cb29a19ac4e341c.png" />     <br />     <div class="image-caption"></div>    </div>    <h2>使用cocoapods的同学注意了</h2>    <p>在import完后很可能你的swift『Bridging-Header.h』桥接文件会报如下的错:</p>    <p></p>    <pre class="brush:java; toolbar: true; auto-links: false;">failed to import bridging header</pre>   <br /> 或者    <p></p>    <pre class="brush:java; toolbar: true; auto-links: false;">SDWebImage/UIImageView+WebCache.h File Not Found Error</pre>   <br /> 可能你不愿接受这个现实,clean了项目,重新build,或者重启了计算机。在模拟器中运行你的项目时心里默念:刚才还是好好的啊,明明能正常运行,为什么有错误!    <br /> 是的。为此我已经抓掉了不少头发。    <br /> 原因是:    <strong>unit tests是个单独的项目,所以一些项目配置必须重新配置一遍</strong>    <div class="image-package" href="https://simg.open-open.com/show/f93b55a5bb044bd17ff203b13edbc421.png">    <img src="https://simg.open-open.com/show/f93b55a5bb044bd17ff203b13edbc421.png" width="700" height="263.0057803468208" data-original-src="https://simg.open-open.com/show/92d8fdab5795292da901005aaf556689.png" />     <br />     <div class="image-caption">     在编辑区中选择Tests项目,在build setting中配置     </div>    </div>    <p><br /> 因为swift的桥接文件很可能在你第一次创建swift类时xcode自动帮你配置了项目,你可能没有意识到build setting其实已经更改了。<br /> 可以参照着自己项目的配置来重新配置一次。<br /> </p>    <div class="image-package" href="https://simg.open-open.com/show/597573020252675d772a9e74a2257e47.png">    <img src="https://simg.open-open.com/show/597573020252675d772a9e74a2257e47.png" width="700" height="115.5864197530864" data-original-src="https://simg.open-open.com/show/e69769ba884633e897242e745bd55637.png" />     <br />     <div class="image-caption"></div>    </div>    <p><br /> 可以参考这个链接下的回答:<a href="/misc/goto?guid=4959653455776042326" target="_blank">http://stackoverflow.com/questions/26116288/failed-to-import-bridging-header</a></p>    <p>cocoapods同理,其实已经默认帮你配置了不少search paths,自己手动贴进去吧,主要是下面这几个选项<br /> </p>    <div class="image-package" href="https://simg.open-open.com/show/064ec041e101d8838d5a7ae2474d5dd9.png">    <img src="https://simg.open-open.com/show/064ec041e101d8838d5a7ae2474d5dd9.png" width="700" height="196.67943805874842" data-original-src="https://simg.open-open.com/show/37375cdf5c5b195f7b50fc445f061de2.png" />     <br />     <div class="image-caption"></div>    </div>    <h1>还差最后一步:Enable Testability</h1>    <p>一切类都配置到项目后又出现了这个错误:</p>    <blockquote>     <p>'Module was not compiled for testing'</p>    </blockquote>    <p>在原来的项目里配置Enable Testability为Yes<br /> </p>    <div class="image-package" href="https://simg.open-open.com/show/dc38150803e8254fe66247dcf7e229e5.png">    <img src="https://simg.open-open.com/show/dc38150803e8254fe66247dcf7e229e5.png" width="700" height="107.69230769230771" data-original-src="https://simg.open-open.com/show/1366473215a231605df1b43975bf7e2a.png" />     <br />     <div class="image-caption"></div>    </div>    <p>现在你就真的可以愉快的在test case中快活的测试任意一个项目中的类了</p>    <p>相关链接:<br /> <a href="/misc/goto?guid=4959653455870789001" target="_blank">http://stackoverflow.com/questions/30787674/module-was-not-compiled-for-testing-when-using-testable</a></p>    <p>来自: <a href="/misc/goto?guid=4959653455960179211" rel="nofollow" target="_blank">http://www.jianshu.com/p/d15a7dea0c5a</a></p>