Fall in love with iOS unit Test series She needs to know her first: Getting Started with unit testing

Source: Internet
Author: User
Tags scalar

Objective

I refused the unit test at first. Unit testing is a thing, because I like to do iOS development is because I like to write the app Ah, everything and this goal irrelevant things I am not interested in, so engaged in iOS development for several years did not go in-depth unit testing (mainly before the unit did not require this). See my advantages Wood has: The goal is very strong, well, remember my advantages, please ignore my refusal to learn the shortcomings of edge knowledge. But recently I was asked by the Director to be responsible for the exploration and Landing of unit tests, and I did not (to) tolerate the task of seizing the bridgehead. With this girl's constant understanding and contact, I found myself gradually fell in love with her, she has an independent knowledge of temperament, has a perfect plump figure, has a growing process of self-enrichment and profound experience. She is so perfect, let me dizzy, sleepless night, I want to write poems for her (unfortunately no literature cell), I want to writing for her (unfortunately no artistic), I would like to write a book for her (unfortunately no writing talent). Youyanwuzhu: A publisher with a wooden and a good book is willing to invite? I really want to write a unit test book, I first write a few articles about her, you see I have no potential for this book.

Let me introduce you to the girl.

The charm of unit testing

Every man has had his favorite girl, there may be more, they may have you like the common characteristics, such as some people like tall, some people like small, also like plump, some people like the cute, some like the savage, also have like ... Here omit a variety of exotic extreme preferences, after all, people tastes. But as a qualified programmer, your ideal goddess, if she has the following advantages, will make you love can not extricate oneself:

1. She can make you more confident. Help you write high-quality code and reduce bugs.
If you analyze the cause of our bug, I think there are some bugs because the developer did not take into account some case or marginal conditions when writing the work code. There are many reasons for this, and one of the important reasons is that we are not thinking about the function that the work code is going to accomplish, and writing unit tests, especially writing unit tests and then writing work code, can help developers think about what the code is going to accomplish. For example, to implement a simple user registration function of the business class method, the unit test and then write the work code in a way to work, developers will first consider a variety of scenarios related, such as normal registration, duplicate user name, did not meet the necessary filling content ... And so on, you'll write the relevant test cases later. The process of writing unit test code is to induce the developer to think about the process of implementing the content and logic of the work code, and then, when implementing the work code, the developers will have a clearer idea and the quality of the code will be improved accordingly.
2. She can raise your fighting ability. Help you improve your code's feedback speed, reduce duplication of effort, and improve development efficiency.
Developers implement a feature or fix a bug, and if there is a corresponding unit test support, developers can immediately run unit tests to verify that the previously completed code is correct, without having to run simulator repeatedly by compiling, waiting for the app to start, Verify the functionality you have done by entering data and other tedious steps. Using unit test code to validate code and to validate code manually by publishing an app is a lot less efficient, so unit testing can actually save labor costs.
3. Her presence makes you relaxed and happy. Make sure your last code changes do not break the functionality of the previous code.
The larger the project, the more code, especially related to some common interfaces such as code or the underlying base library, no one can guarantee that the modified code will not break the previous functionality, so the related requirements will be shelved or postponed, because the code is not able to improve, and it becomes more and more difficult to maintain, the quality is getting worse. Unit testing is a good way to solve this problem (not to say the best). Because the history of the Code has a corresponding unit test guarantee, after modifying some of the code, by running the relevant unit tests can verify that the newly adjusted functionality has affected the previous function. Of course, to achieve this level requires a lot of pay, not only to achieve a relatively high test coverage, and unit test code to write the quality must also be guaranteed.
4. She can help you solve problems, always do you rely on. Make your code maintenance easier.
By writing a lot of unit tests on the code, which is equivalent to adding a specification to the code, developers can also help developers understand the existing code by reading the unit test code. Very open source projects (such as afnetworking, FMDB, Meow vvdoucment, etc.) have a considerable amount of unit test code, which can be helpful to understand the production source code by reading these test codes.
5. She is willing to be your strong backing, when you work hard to fight without worries. She helps improve code quality and design.
In addition to the code written by the Danale, I believe that many easy-to-maintain, well-designed code is obtained through constant refactoring. Although unit testing itself does not directly improve the quality of production code, it provides a "safety net" for production code, allowing developers to boldly improve their code so that clean and beautiful code is no longer a dream.

I am so hard to blow it so attractive, if you as a programmer still do not want to see her, Wakao, I guess you are not normal, it is necessary to do a self-examination. If you are a normal person like me, below I will not charge, take you far to see this beautiful woman, see what she looks like.

A probe into iOS unit testing

"In computer programming, unit testing (also known as module testing, Unit testing) is a test of correctness checking for program modules (the smallest unit of software design)," she explains. The program unit is the smallest testable part to be applied. In procedural programming, a unit is a single program, function, process, etc. for object-oriented programming, the smallest element is a method, including a base class (superclass), an abstract class, or a method in a derived class (subclass). ”。

Typically, a programmer modifies a cell every time the code is modified, so that we can validate the unit (unit test), and it is possible to do several unit tests before and after writing the program to verify that the program meets the requirements of the software specification (product requirements) and that there is no program error. Although unit testing is not necessary, it is not bad, it involves the policy decision of project management (plainly, you can live without her, but you can only write code on your left hand, right hand ...). Of course also write code, as a programmer how can a hand to knock code it?

1. Learn about unit tests in Xcode

When you create a new project in XCode7, you will default to a target for the unit test, whose name is the project name plus the test suffix, and the file name ends with test. You will find that you already have a default test case.

After creating the project, the default is to create a test class with the bundle name +TESTS.M, which contains the following default code meaning:

- (void) setUp {[Super setUp]; //Put Setup code here.      This method was called before the invo cation of each test method in the class. //initializes the code that is called before the test method call}- (void) TearDown {//Put teardown code here.       This method was called after the invocation for each test method in the class. //Release the resource code for the test case, which is called after each test case executes[Super TearDown];}- (void) Testexample {//A example of a functional test case. //Use Xctassert and related functions to verify your tests produce the correct results. //example of a test case, note that the test case must begin with testing}- (void) Testperformanceexample {//A example of a performance test case. //test Performance examples, there is instrument debugging tools, in fact, this does not use hair. [Self measureblock:^{         //Put the code want to measure the time of this . //code that needs to test performance          }];}

2. Write a simple test case
In the Testexample method, enter

-(void) testexample {    // This was an example of a functional test case.     // Use Xctassert and related functions to verify your tests produce the correct results.    NSLog (@ " start falling in love with unit test first unit test test ")    ; @" This is my first unit test. " ;    Xctasserttrue ([Test isequaltostring:@ " newcomer, just want to test success, no way "]);}

Then press the shortcut key command + U for unit testing, this shortcut is all tested. You can see the following result interface:

Since we used the assertion xctassettrue, which is used to compare whether an expression is true, it is obvious that the expression returns false, so this test case must not pass.

We can also test a test case by clicking the Play button.

3.XCTest Common assertions

xctfail (Format ...) generates a failed test; Xctassertnil (A1, Format ...) For null judgment, the A1 is empty when passed, and vice versa; Xctassertnotnil (a1, Format ...) Not for null judgment, A1 is not empty when passed, vice versa; Xctassert (expression, format ...) Passed when expression evaluates to true; xctasserttrue (expression, format ...) Passed when expression evaluates to true; Xctassertfalse (expression, format ...) Passed when expression evaluates to false; Xctassertequalobjects (A1, A2, Format ...) Judged equal, [A1 ISEQUAL:A2] value is true when passed, where one is not empty when not passed; Xctassertnotequalobjects (a1, A2, Format ...) Judgment unequal, [A1 ISEQUAL:A2] value is false when passed; Xctassertequal (a1, A2, Format ...) Judgment equality (used when A1 and A2 are C scalar, struct, or union, actual test found nsstring can); xctassertnotequal (A1, A2, Format ...) Judgment unequal (used when A1 and A2 are C scalar, struct, or union); Xctassertequalwithaccuracy (A1, a2, accuracy, Format ...) The judgment is equal, (double or float type) provides an error range when in the error range (+/-accuracy) pass test when Equal, Xctassertnotequalwithaccuracy (A1, a2, accuracy, Format ...) to determine unequal, (double or float type) provides an error range, Pass the test when the error range is not equal; Xctassertthrows (expression, format ...) Exception test, passed when expression has an exception; (very perverted) xctassertthrowsspecific (expression, specificexception, format ...) exception test, Passed when expression has an specificexception exception, whereas other exceptions or exceptions occur; xctassertthrowsspecificnamed (expression, Specificexception, Exception_name, Format ...) Exception test, when expression has a specific exception, the exception of the specific exception name passed the test, and vice versa; Xctassertnothrow (expression, format ...) Exception test, passing test when expression does not occur; Xctassertnothrowspecific (expression, specificexception, format ...) Exception test, when expression does not have a specific exception, the specific exception name of the exception passed the test, and vice versa; xctassertnothrowspecificnamed (expression, specificexception, Exception_name, Format ...) An exception test that passes the test when expression does not have an exception that has a specific exception, a specific exception name, and vice versa .

Special attention is paid to the xctassertequalobjects and xctassertequal.
Xctassertequalobjects (A1, A2, Format ...) The judging condition is whether [A1 ISEQUAL:A2] returns a yes. Xctassertequal (A1, A2, Format ...) The judging condition is A1 = = A2 whether to return a yes. For the latter, if both A1 and A2 are basic data type variables, only a1 = = A2 will return yes.

End

Donuts, Getting started is the way to get started, so you have a little bit of understanding. Like to introduce you to a beautiful woman, I will give her a hazy veil, waiting for you to open a good price (like) after I will step by step to uncover its veil, (Bie four tones) worry, good things will be a little bit to see just taste it. I will continue to introduce the unit test advanced, Unit testing in combat, unit testing a variety of skills and other articles.

Reference article:

"Using Unit Tests"

iOS development: xctest unit Test (with a single test code attached)

Fall in love with iOS unit Test series She needs to know her first: Getting Started with unit testing

Related Article

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.