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:
When you set the
Enable Testability
build setting toYes
, 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.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 thanXCTAssertFalse
. It’s sometimes easy to make this error.
Enable Code Coverage
Code coverage data collection incurs a performance penalty. Whether the penalty is significant or not, it should affect execution of the code in a linear fashion so performance results remain comparable from test run to test run when it is enabled. However, you should consider whether to have code coverage enabled when you are critically evaluating the performance of routines in your tests.
代码覆盖率会影响性能测试的结果。
Writing Testable Code
Define API requirements. It is important to define requirements and outcomes for each method or function that you add to your project. For requirements, include input and output ranges, exceptions thrown and the conditions under which they are raised, and the type of values returned (especially if the values are instances of classes). Specifying requirements and making sure that requirements are met in your code help you write robust, secure code.
See the Unit Testing Apps and Frameworks sample-code project for an example of using exceptions to identify and report incorrect library usage by client code. 尽可能避免使用全局变量,传参到方法里然后返回某种类型的结果
Write test cases as you write code. As you design and write each method or function, write one or more test cases to ensure that the API’s requirements are met. Remember that it’s harder to write tests for existing code than for code you are writing. 尽早写测试代码,写一个方法就写对应的测试代码,为已有的代码写测试极其痛苦!
Check boundary conditions. If a parameter for a method must have values in a specific range, your tests should pass values that include the lowest and highest values of the range. For example, if a procedure has an integer parameter that can have values between
0
and100
, inclusive, the test code for that method should pass the values0
,50
, and100
for the parameter. 测试用例使用的测试值要覆盖全面的情况Use negative tests. Negative tests ensure that your code responds to error conditions appropriately. Verify that your code behaves correctly when it receives invalid or unexpected input values. Also verify that it returns error codes or raises exceptions when it should. For example, if an integer parameter must have values in the range
0
to100
, inclusive, create test cases that pass the values-1
and101
to ensure that the procedure raises an exception or returns an error code. 测试用例甚至需要使用各种异常的值,比如越界、类型异常等等都可以考虑在内Write comprehensive test cases. Comprehensive tests combine different code modules to implement some of the more complex behavior of your API. Although simple, isolated tests provide value, stacked tests exercise complex behaviors and tend to catch many more problems. These kinds of tests mimic the behavior of your code under more realistic conditions. For example, in addition to adding objects to an array, you could create the array, add several objects to it, remove a few of them using different methods, and then ensure that the set and number of remaining objects are correct. 将测试用例综合起来进行测试,有些测试用例分开进行时没问题,结合起来就发生了故障
Cover your bug fixes with test cases. Whenever you fix a bug, write one or more tests cases that verify the fix. 修复BUG之后,完善BUG对应的测试用例
Ficow原创,转载请注明出处:http://www.cnblogs.com/ficow/p/7859646.html
iOS Testing with Xcode 阅读笔记的更多相关文章
- [阅读笔记]Software optimization resources
http://www.agner.org/optimize/#manuals 阅读笔记Optimizing software in C++ 7. The efficiency of differe ...
- CI框架源码阅读笔记2 一切的入口 index.php
上一节(CI框架源码阅读笔记1 - 环境准备.基本术语和框架流程)中,我们提到了CI框架的基本流程,这里再次贴出流程图,以备参考: 作为CI框架的入口文件,源码阅读,自然由此开始.在源码阅读的过程中, ...
- QCon 2015 阅读笔记 - 其他精选主题
QCon 2015阅读笔记 QCon 2015 阅读笔记 - 移动开发最佳实践 QCon 2015 阅读笔记 - 团队建设 QCon 2015 阅读笔记 - 其他精选主题 以前分享过两个主题:移动开发 ...
- QCon 2015 阅读笔记 - 移动开发最佳实践
所有ppt下载地址:http://pan.baidu.com/s/1mg9o4TM 下面是移动开发实践部分的阅读笔记. 移动开发网络性能优化实践 - 陈浩然 (携程) 携程是非常标准的移动App架构, ...
- iOS开发之Xcode常用调试技巧总结
转载自:iOS开发之Xcode常用调试技巧总结 最近在面试,面试过程中问到了一些Xcode常用的调试技巧问题.平常开发过程中用的还挺顺手的,但你要突然让我说,确实一脸懵逼.Debug的技巧很多,比如最 ...
- UI Testing in Xcode 7
参考文章: UI Testing in Xcode - WWDC 2015https://developer.apple.com/videos/play/wwdc2015-406/ Document ...
- CI框架源代码阅读笔记2 一切的入口 index.php
上一节(CI框架源代码阅读笔记1 - 环境准备.基本术语和框架流程)中,我们提到了CI框架的基本流程.这里再次贴出流程图.以备參考: 作为CI框架的入口文件.源代码阅读,自然由此開始. 在源代码阅读的 ...
- 《iOS开发全然上手——使用iOS 7和Xcode 5开发移动与平板应用》之Objective-C新手训练营
编写Hello World应用程序通常被觉得,是学习不论什么编程语言的第一步.在这一章,你将创建iOS版的Hello World应用程序作为起步,高速了解Xcode这个开发iOS应用程序的主要工具. ...
- 阅读笔记 1 火球 UML大战需求分析
伴随着七天国庆的结束,紧张的学习生活也开始了,首先声明,阅读笔记随着我不断地阅读进度会慢慢更新,而不是一次性的写完,所以会重复的编辑.对于我选的这本 <火球 UML大战需求分析>,首先 ...
随机推荐
- oracle spm使用1
oracle11g new feature SPM 有助于保持sql的语句特性,仅仅同意运行性能提高的运行计划. 它不同于stored outlines, spm在于稳定sql性能,而store ou ...
- Bean定义并注册到spring
1.XML配置文件 2.Annotation注解 3.Java Code 配置方式 BeanDefinitionRegistryPostProcessor
- Centos修改静态IP
vim /etc/sysconfig/network-scripts/ifcfg-eth0代开配置文件 写入 DEVICE=eth0 #描述网卡对应的设备别名,例如ifcfg-eth0的文件中它为et ...
- Form Template Method
<重构>中此方法叫做塑造模板函数,在设计模式中,对应的模式就是模板模式.重构中的很多变动比较大的方法都会导致重构,但重构中有非常多的小重构手法.就好像建筑一个房子,设计模式教你厨房客厅怎么 ...
- createDocumentFragment 文档碎片提升dom增删的性能
原理: 操作dom会使得页面进行重新渲染,如果 经常性的对dom就行操作或者一次性操作dom较多,每一次操作都会使页面进行重新渲染,降低页面加载性能. 针对IE9以下,可以使用文档碎片(documen ...
- javascript 阻止事件冒泡 cancelBubble
javascript简单的阻止事件冒泡,可以使用事件的cancelBubble方法为true: html部分 <button id="btn1">点击显示div< ...
- ZOJ 3706 Break Standard Weight 解题报告
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5009 题目意思:给出两个mass:x 和 y,问如何将其中一个 ma ...
- html5--6-40 CSS边框
html5--6-40 CSS边框 实例 div动态阴影 学习要点 掌握CSS边框属性的使用 元素的边框就是围绕元素内容和内边距的一条或多条线. 元素的边框属性: border 简写属性,用于把针对四 ...
- hdu 4763 Theme Section(next数组找串中三段相等)
题意:在一个串中找 EAEBE 的形式的最长的E,其中E为一个字符串,也就是说找到前缀与后缀相同,并且串中还存在相同的一段,它们不能重复. 思路:利用next数组,next[len]代表的即是最大的相 ...
- Lucene 的四大索引查询 ——bool 域搜索 通配符 范围搜索
Lucene 的四大索引查询 清单1:使用布尔操作符 Java代码 //Test boolean operator blic void testOperator(String indexD ...