Take everyone into the iOS unit test world

Source: Internet
Author: User


Today, we focus on unit testing, many people may not have heard the unit test or just heard, and no practical to practice, no relationship, today to everyone to popularize the knowledge of this area, and with everyone to practice, to experience the unit test benefits.
If a mobile developer does not pay attention to the unit test, the developer often shows a "lack of confidence", always feel that the code is very high quality, until a bug (bugs) to bite their head, when a major problem occurs, Only to find that the original code has been to the shear constantly disorderly state, and every time a bug, you need to go through the "Sunglasses maze"

Therefore, it is necessary for our developers to attach importance to unit testing, so that they do not get overwhelmed by the problem of the program.
There are also a lot of people who know unit tests or write unit tests, but just write a method that says a [Test] property, and even a lot of people unit tests labeled [Ignoretest], these are not enough, are relatively superficial. So today take everyone into the world of unit testing.

In the formal introduction of unit testing, we must understand the following concepts, as follows:

What is a unit test?

Unit testing is a small piece of code written by a developer to verify that a well-defined function in the code under test is correct. Typically, a unit test is used to determine the behavior of a particular function under a particular condition (or scenario).

Unit tests are performed to prove that the behavior of a piece of code is exactly the same as the developer expects. So what we want to test is a small, very independent feature fragment. Build confidence through the behavior of all individual parts. Then, you can start testing the entire system.

What are the benefits of two-unit testing?

1. 单元测试使工作完成的更轻松2. 经过单元测试的代码,质量能够得到保证3. 单元测试发现的问题很容易定位。4. 修改代码犯的错,经过单元测试易发现5.  单元测试可以在早期就发现性能问题6. 单元测试使你的设计更好7. 大大减少花在调试上的时间
III What happens if I don't do unit testing?
1. 代码会暗藏很多缺陷,健壮性不强2. 系统测试发现的缺陷比较难以定位3. 为了修复缺陷而修改代码,很可能会不小心犯错,但是又不能及时发现这些新错误。4. 性能问题很难定位,性能优化的时间很难控制。
Four unit test guidelines what are the 1. Keep unit tests small and fast
 理论上, 任何代码提交前都应该完整跑一遍所有测试套件. 保持测试代码执行迅捷能够缩短迭代开发周期.
2. Unit testing should be fully automated and non-interactive
  测试套件通常是定期执行的, 执行过程必须完全自动化才有意义. 需要人工检查输出结果的测试不是一个好的单元测试.
3. Make unit tests easy to run
  对开发环境进行配置, 最好是敲条命令或是点个按钮就能把单个测试用例或测试套件跑起来.
4. Evaluate the test
   对执行的测试进行覆盖率分析, 得到精确的代码执行覆盖率, 并调查哪些代码未被执行.
5. Immediate correction of failed tests
   每个开发人员在提交前都应该保证新的测试用例执行成功, 当有代码提交时, 现有测试用例也都能跑通.  如果一个定期执行的测试用例执行失败, 整个团队应该放下手上的工作优先解决这个问题.
6. Maintain the test at the unit level
    单元测试即类 (Class) 的测试. 一个 "测试类" 应该只对应于一个 "被测类", 并且 "被测类" 的行为应该被隔离测试. 必须谨慎避免使用单元测试框架来测试整个程序的工作流, 这样的测试既低效又难维护. 工作流测试 (译注: 指跨模块/类的数据流测试) 有它自己的地盘, 但它绝不是单元测试, 必须单独建立和执行.
7. From the simple into the complex
    最简单的测试也远远胜过完全没有测试. 一个简单的 "测试类" 会促使建立 "被测类" 基本的测试骨架, 可以对构建环境, 单元测试环境, 执行环境以及覆盖率分析工具等有效性进行检查, 同时也可以证明 "被测类" 能够被整合和调用.

For specific reference, refer to:

Five when should we do unit testing?
 1、写完代码以后:想要验证一下自己写的代码是否有问题。 2、写代码之前:就是写代码之前所有的功能分模块的设计好,测试通过了再写。(我反正是没用过)。  3、修复某个bug后:一般修复完某个bug,为了确保修复是成功的,会写测试。
Six how do we do unit testing?

First step: Let's create a project

The second step works well, we find the system unit test in the Testes folder. m file fancy will go to see a few methods, we look at how this several methods are called and their respective roles.

The third step is to define a simple method within the viewController.h.

Fourth step we implement it in VIEWCONTROLLER.M.

The fifth step method is created, we import ViewController.h in the test file, and declare it as a property:

Sixth Step test Case implementation

1) Normal condition

Set up, we command+u the shortcut to run, or produce–>test run.

The results show:

2) What happens if the result of the return is not equal to our set value?

Seventh Step performance test case implementation


Here today, everyone on the unit test has a preliminary impression, if you want to know more about unit testing, we go online to read some blog to further understanding. Introduce a book to you:


Take everyone into the iOS unit test world

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.