The so-called Unit Test (also known as the module test, unit testing) is for the program module (the smallest unit of software design) for the correctness of testing work.
In fact, we developed a program to implement a function of the time we also have to test. Just, you are in the project code directly write method, and in the test, is also point point, point to the corresponding interface to trigger the corresponding method, test, this test efficiency, think is also drunk, but fortunately can be normal test, but if you want to control the different input parameters? Well, you say write directly to the program. But after your test is done? All right, you say it, just comment it out. But what if you change it later and you need to test it again? In fact, said so much, is to say, in your effective engineering code to write test code, this is the unit test, after completion, a command+u directly into the entrance code test.
Next is how to test the code, this network a lot of search, but all in that test.m file directly write the rhythm, read you will certainly ask, here write some judgment 1+1=? 2 of the code? What's the situation. It's not really like that. This is the case with the current version of Xcode:
1, directly create the project, the default is already have the so-called testocunittesttests. m file, created after the following, you directly see a TESTOCUNITTESTTESTS.M file, which is the test code to put the file.
2, click on the right side of the triangle you normally run the project Testocunittest can see the new three options, a scheme, and then switch the project to testocunittesttests inside, press and hold Command+u, will pop up a test Succeeded the prompt box, indicating success.
3, next talk about how to test the method of a class in our project, we casually in one of our own class to create a new method, I chose in the Masterviewcontroller this class inside good. The method is implemented in the received string with [] wrapped, and then return, the code is as follows:
-(NSString *) getteststring: (NSString *) normalstr{return [NSString stringwithformat:@ "[%@]", Normalstr];}
Remember to declare as an externally callable Method!
4. call this method in the TESTOCUNITTESTTESTS.M file:
- (void) testexample {      //1, find a class you need to do unit testing, instantiate masterviewcontroller * controller = [[masterviewcontroller alloc]init]; nsstring *normalstr = @ "Objective-c"; nsstring *expectstr = @ "[Objective-c]"; //2, method of calling the object to be unit tested NSString *returnStr = [controller getTestString:normalStr];  //3, assert that you expect the return value and the actual return value to conform to nslog (@ "is masterviewcontroller teststr GETRIGHTSTR: "); xctassertequalobjects (expectstr,returnstr, @" Pass ");}
After you can run it, you can see the hook on the left side of the method to show that the test was passed. (PS: Written in testocunittesttests . M all methods will be executed by default, without you having to call them manually )
If you are testing other methods in your project, then directly create the test method in the testocunittesttests. m, and then assert that the test return data is the same as expected, which is the use of unit tests.
For some of the commonly used assertion test functions, I copy them directly, as shown here:
Xctfail (Format ...) generates a failed test;
Xctassertnil (A1, Format ...) For the null judgment, A1 is empty when passed, and vice versa;
Xctassertnotnil (A1, Format ...) Not for empty judgment, A1 is not empty when passed, and 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 ...) judgment is equal, [A1 ISEQUAL:A2] value is true when passed, one of which is not empty when not passed;
Xctassertnotequalobjects (A1, A2, Format ...) Judgment unequal, [A1 ISEQUAL:A2] value is false when passed;
xctassertequal (a1, A2, Format ...) judge Equality (used when A1 and A2 are C scalar, struct, or union, determine the address of the variable , return True if the address is the same, or return no);
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, and passes the test when equal within the error range (+/-accuracy);
Xctassertnotequalwithaccuracy (A1, a2, accuracy, Format ...) The judgment is unequal, (double or float type) provides an error range, which passes 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, When expression has an specificexception exception, the other exception or no exception occurs;
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 ...) An exception test, which passes the test when expression does not occur abnormally;
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.
iOS development uses OCUNIT to unit test programs (unittest)