What is unit testing?
The word unit test is very high-end, in fact unit testing is for your method more specialized in writing a test function. To ensure that your approach is constantly modifying the development. Keep it right. If something goes wrong, the first time you know, start monitoring from the smallest unit to ensure the quality of the software.
When to use unit tests:
1, after writing the code: want to verify the code you write whether there is a problem.
2, write code before: is to write code before all the functions of the module design, test passed the re-write. (I didn't use it anyway).
3, fix a bug: Generally fixed a bug, in order to ensure that the repair is successful, will write Tests.
How to write unit tests
As if there is a lot of nonsense, or direct to the topic it.
Create a project, name it, tick the include Unit Tests directly
Qq20160129-0.png
What if I forget to tick the box? There are other ways to create File-->new-->target-->ios-->ios Unit testing bundles. Name yourself and look at it.
Qq20160129-1.png
When the project is created, how do you start testing it?
Locate 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 various functions
Qq20160129-2.png
- (void) SetUp {[SuperSETUP];//Put Setup code here. This method was called before the invocation of each test method in the class.//initialization code, 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 will be called after each test case executes[SuperTearDown];} - (void) Testexample {example of a functional test case.//Use Xctassert and related functions to verify your tests produce the correct results.///test case examples, note that test cases must begin with testing}- (void) Testperformanceexample {example of a performance test case.//test performance Examples[ Selfmeasureblock:^{//Put The code you want to measure the time of this.//code that needs to test performance}];}
Write a simple method in Viewcontroller
(int)getNum;
Realize:
- (int)getNum {return100;}
Import ViewController.h in the tested file, and define a VC attribute
#import <XCTest/XCTest.h>#import "ViewController.h"@interface ____Tests : XCTestCase@property (nonatomic,strong) ViewController *vc;@end@implementation ____Tests
Implementation of test Cases
- (void)setUp {[super setUp];// 实例化需要测试的类self.vc = [[ViewController alloc] init];}- (void)tearDown {// 清空self.vcnil;[super tearDown];}- (void)testMyFuc {// 调用需要测试的方法,int result = [self.vc getNum];// 如果不相等则会提示@“测试不通过”100,@"测试不通过");}
The Command+u shortcut runs, or produce-->test is OK,
The project is running.
Qq20160129-3.png
We can clearly see in the console that we are going to test with the example passed, the test passed by the test method will have a green hook.
At this time we changed the assertion, the 100 casually changed to a number, 120. And then Comand+u run, see what happens
Qq20160129-4.png
Obviously it can be passed, because the method we want to test the return value is 100,
The own test framework can also test the performance of a method,
- (void)testPerformanceExample {// This is an example of a performance test case.[self measureBlock:^{ // Put the code you want to measure the time of here. for (int0; i<100; i++) { NSLog(@"dd"); }}];
}
We add a for loop in the example to test its performance. Command+u run and you can see
Qq20160129-5.png
Can be very intuitive to see its invocation of time, you can use it to compare the merits of performance.
Xctest also supports asynchronous unit testing, and I'm not here to expand. Finally, the usual assertions and explanations are attached.
Xctfail (format...) Generate 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, 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...) The judgment is equal, [A1 ISEQUAL:A2] value is true when one is not empty, one does not pass; 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 when 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 .
Wen/Jay for the Winter (Jane book author)
Original link: http://www.jianshu.com/p/009844a0b9ed
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
ios-using Xcode's own unit test unittest