iOS Testing with Xcode 阅读笔记

官方文档直通车

Performance Testing

A baseline is a combination of the average time performance in ten runs of the test method with a measure of the standard deviation of each run.

执行一次性能测试时,measureBlock内的代码会被执行十次!

Where to Start When Testing

When you start to create tests, keep the following ideas in mind:

  • When creating unit tests, focus on testing the most basic foundations of your code, the Model classes and methods, which interact with the Controller.  单元测试需要对每个细小的代码单元进行测试,单元的划分需要合理。
  • When creating UI tests, start by considering the most common workflows. Think of what the user does when getting started using the app and what UI is exercised immediately in that process. Using the UI recording feature is a great way to capture a sequence of user actions into a UI test method that can be expanded upon to implement tests for correctness and/or performance.

    UI tests of this type tend to start with a relatively coarse-grained focus and might cut across several subsystems; they can return a lot of information that can be hard to analyze at first. As you work with your UI test suite, you can refine the testing granularity and focus UI tests to reflect specific subsystem behaviors more clearly.UI测试,按照最平常的操作流程写测试代码,考虑界面应该如何正确响应,多次测试之后再慢慢去细化测试的每个部分。

Flow of Test Execution

For each class, testing starts by running the class setup method. For each test method, a new instance of the class is allocated and its instance setup method executed. After that it runs the test method, and after that the instance teardown method. This sequence repeats for all the test methods in the class. After the last test method teardown in the class has been run, Xcode executes the class teardown method and moves on to the next class. This sequence repeats until all the test methods in all test classes have been run.

每个测试类开始执行时,都会先执行setUp类方法,然后开始执行每个测试方法。

执行每个测试方法时,都会先执行这个测试类实例对象的setUp方法,然后开始执行这个测试方法,在这个测试方法执行结束后,会执行这个测试类实例对象的tearDown方法。

这个类里面的所有测试方法执行结束之后,会执行这个测试类的tearDown类方法。

然后一直重复这个过程,直到所有的测试类都执行完测试方法位置。

简单来说,这个顺序从前往后就是:class func setUp() ,  func setUp() ,  func testFunc()  , func tearDown()  , class func tesrDown() 。

Writing Tests with Swift

The Swift access control model, as described in the Access Control section of The Swift Programming Language (Swift 4), prevents an external entity from accessing anything declared as internal in an app or framework. By default, to be able to access these items from your test code, you would need to elevate their access level to at least public, reducing the benefits of Swift’s type safety.

Xcode provides a two-part solution to this problem:

  1. When you set the Enable Testability build setting to Yes, which is true by default for test builds in new projects, Xcode includes the -enable-testing flag during compilation. This makes the Swift entities declared in the compiled module eligible for a higher level of access.
  2. When you add the @testable attribute to an import statement for a module compiled with testing enabled, you activate the elevated access for that module in that scope. Classes and class members marked as internal or public behave as if they were marked open. Other entities marked as internal act as if they were declared public.

Note: @testable provides access only for internal functions; file-private and private declarations are not visible outside of their usual scope when using @testable.

对于Swfit代码的测试存在读取控制权限问题,所以苹果提供了两步解决办法来提升代码当前的存取控制权限。

第一步是去Xcode里面设置Enable Testability,第二步是在测试代码里添加@testable。对于fileprivate和private,@testable无法提升他们的访问权限。

Using Assertions with Objective-C and Swift

  • For Objective-C, assertions marked for scalar types can be used with the types that can be used with the equality comparison operators: ==!=<=<>=, and >. If the expression resolves to any C type, struct, or array comparison that works with these operators, it is considered a scalar.  使用Objc写测试代码时,要注意值类型和引用类型的区别,而Swift不用。
  • Using XCTest assertions in your tests also differs between Objective-C and Swift because of how the languages differ in treating data types and implicit conversions.
    • For Objective-C, the use of implicit conversions in the XCTest implementation allows the comparisons to operate independent of the expressions’ data types, and no check is made of the input data types.
    • For Swift, implicit conversions are not allowed because Swift is stricter about type safety; both parameters to a comparison must be of the same type. Type mismatches are flagged at compile time and in the source editor.

      Swift对于类型不匹配会报编译错误,进行Assert比较的变量必须类型匹配;Objc不会检查类型!

Test Debugging Workflow

Here are some common issues to keep in mind:

  • Is the logic of the test correct? Is the implementation correct? 逻辑是否正确?实现是否正确?字面量有没有打错?

    It’s always a good idea to check for typos and incorrect literal values that you might be using as the reference standard that the test method is using as a basis of comparison.

  • What are the assumptions? 多定义一些错误类型,写测试的时候也有可能传错数据

    For example, you might be using the wrong data type in the test method, creating a range error for the code you’re testing.

  • Are you using the correct assertion to report the pass/fail status? 有没有用错Assetion?

    For example, perhaps the condition of the test needs XTCAssertTrue rather than XCTAssertFalse. It’s sometimes easy to make this error.

 



Ficow原创,转载请注明出处:http://www.cnblogs.com/ficow/p/7859646.html

时间: 2024-10-14 07:41:46

iOS Testing with Xcode 阅读笔记的相关文章

IOS测试框架之:athrun的InstrumentDriver源码阅读笔记

athrun的InstrumentDriver源码阅读笔记 作者:唯一 athrun是淘宝的开源测试项目,InstrumentDriver是ios端的实现,之前在公司项目中用过这个框架,没有深入了解,现在回来记录下. 官方介绍:http://code.taobao.org/p/athrun/wiki/instrumentDriver/ 优点:这个框架是对UIAutomation的java实现,在代码提示.用例维护方面比UIAutomation强多了,借junit4的光,我们可以通过junit4的

《iOS应用逆向工程》学习笔记(六)使用dumpdecrypted砸壳

本来是打算用AppCrackr砸壳的,结果砸壳都是失败的,开始以为是App的加密太厉害了,后来才知道是因为AppCrackr太暴力了,引起公愤,结果被人投诉招致核心功能被迫关闭了. 幸好在RE官网搜到一个用dumpdecrypted砸壳的帖子.下面是我砸壳的经历. 一.造锤 1.下载dumpdecrypted源码 下载地址:https://github.com/stefanesser/dumpdecrypted/archive/master.zip,接着在Mac中解压. 2.确认iOS设备的版本

&lt;游戏开发中的人工智能&gt; -- 阅读笔记

到家已经几天了, 休息了一阵, 是时候重新学习知识了. 接下去一段时间, 会啃<游戏开发中的人工智能>这本书, 顺便写写笔记. 马上就大三了, 想想自己选的游戏方向, 现在还蛋疼. 选了一个自己喜欢的方向, 但是确实最忙的一个,这也意味着少时间继续我的iOS学习. 也不知道是对是错. 既然选了,就学吧. 好不,不扯多了.接下去是该系列的笔记.(持续更新) 第一章: 游戏人工智能简介 1. 定性AI与非定性AI 定性行为或其表现是特定的,而且是可预测的,没有不确定性. 非定性行为有某种程度的不确

CI框架源码阅读笔记2 一切的入口 index.php

上一节(CI框架源码阅读笔记1 - 环境准备.基本术语和框架流程)中,我们提到了CI框架的基本流程,这里这次贴出流程图,以备参考: 作为CI框架的入口文件,源码阅读,自然由此开始.在源码阅读的过程中,我们并不会逐行进行解释,而只解释核心的功能和实现. 1.       设置应用程序环境 define('ENVIRONMENT', 'development'); 这里的development可以是任何你喜欢的环境名称(比如dev,再如test),相对应的,你要在下面的switch case代码块中

《iOS开发全然上手——使用iOS 7和Xcode 5开发移动与平板应用》之Objective-C新手训练营

编写Hello World应用程序通常被觉得,是学习不论什么编程语言的第一步.在这一章,你将创建iOS版的Hello World应用程序作为起步,高速了解Xcode这个开发iOS应用程序的主要工具. 下一步.你将学习Objective-C的基础知识.在此基础之上.将探索类(class)与对象(object)的知识.它们是构建应用程序的主要基石.与此同一时候,你将创建CarValet应用程序,练习一些类的编写.并学习属性(property)的知识.在本章末尾,你将在指导下完毕编程挑战题以探索子类扩

CI框架源代码阅读笔记2 一切的入口 index.php

上一节(CI框架源代码阅读笔记1 - 环境准备.基本术语和框架流程)中,我们提到了CI框架的基本流程.这里再次贴出流程图.以备參考: 作为CI框架的入口文件.源代码阅读,自然由此開始. 在源代码阅读的过程中,我们并不会逐行进行解释.而仅仅解释核心的功能和实现. 1.       设置应用程序环境 define('ENVIRONMENT', 'development'); 这里的development能够是不论什么你喜欢的环境名称(比方dev,再如test).相相应的,你要在以下的switch c

《iOS开发完全上手——使用iOS 7和Xcode 5开发移动与平板应用》之Objective-C新手训练营

编写Hello World应用程序通常被认为,是学习任何编程语言的第一步.在这一章,你将创建iOS版的Hello World应用程序作为起步,快速了解Xcode这个开发iOS应用程序的主要工具. 下一步,你将学习Objective-C的基础知识.在此基础之上,将探索类(class)与对象(object)的知识,它们是构建应用程序的主要基石.与此同时,你将创建CarValet应用程序,练习一些类的编写,并学习属性(property)的知识.在本章末尾,你将在指导下完成编程挑战题以探索子类扩展的知识

QCon 2015 阅读笔记 - 移动开发最佳实践

所有ppt下载地址:http://pan.baidu.com/s/1mg9o4TM 下面是移动开发实践部分的阅读笔记. 移动开发网络性能优化实践 - 陈浩然 (携程) 携程是非常标准的移动App架构,基础是各种Infrastructure Frameworks, 基于上面是UI的控件库,运行时的库(猜测用于动态配置).最上层是业务层面,各个App层可以相对独立形成业务模块化.同时也是Hybrid的架构,有Web Container来实现WebApp的模块. 网络服务 Native TCP连接 +

QCon 2015 阅读笔记 - 其他精选主题

QCon 2015阅读笔记 QCon 2015 阅读笔记 - 移动开发最佳实践 QCon 2015 阅读笔记 - 团队建设 QCon 2015 阅读笔记 - 其他精选主题 以前分享过两个主题:移动开发最佳实践和团队建设,有兴趣可以通过上面传送门进入.这次我的阅读笔记会比较分散,希望能够把一些我认为不错的主题介绍一下. 论DevOps式思维方式 - Chris Van Tuin 分三个维度介绍如何加速软件研发,表明软件研发的趋势 How: 开发模式 - 瀑布流.敏捷.DevOps What: 软件