What is unit testing?
unit testing refers to the inspection and validation of the smallest testable unit in the software. For unit testing in the meaning of the unit, in general, according to the actual situation to determine its specific meaning, such as the C language unit refers to a function, Java unit refers to a class, graphical software can refer to a window or a menu. In general, the unit is the minimum measured function module that is artificially defined.
What is the meaning of unit testing in iOS?
Large-scale project development, the function is more cumbersome, the code is relatively large, debugging a piece of functionality requires constant Command+r run debugging, it is obvious that such a function is very very inefficient, the writing process and the application function to detect a piece of functionality, the use of unit testing more targeted testing functions. Two words-efficient!
Adding unit Tests
1. The new project is checked by default
2. Add unit tests for old projects
Click File
-–> New
——-> Target
Familiarity with unit test classes
unittestsdemotests classes are inherited and Xctestcase.
#import <xctest/xctest.h > @interface unittestsdemotests:xctestcase @end @implementation unittestsdemotests-( void ) Setup {[super Setup]; /** * Each test method executes before calling * */} -(void ) tearDown {[super TearDown]; /** * after each test method execution call */} -(void ) testexample {/** * Test method named Example */}-(void ) testperformanceexample {//Main detection code execution performance /span> [Self measureblock:^{}];} @end
Xcode7 default method with test performance It's - (void)testPerformanceExample
an interesting approach.
- (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(int i=0;i<1000;i++) { NSLog(@"%d",i); } }];}
Repeated execution of the above code, will collect each execution time, and calculate the average, each execution will be compared with the average, give you a reference hint.
When we add an interesting scene after the value of I 0
Xcode detects this run, far more than the average, gives a red warning, is it felt that Xcode is too powerful?
Recognize test symbols
The warning does not pass the symbol, which indicates that the module is having problems
By means of symbols, there is no problem with this module
Custom test methods
The custom test method must start with the test method name (TESTXXX), such as the abovetestExample
The custom method must be a void
return type
-(void)testFloatEqual{}
Common Macros for Unit tests
xctassertequalobjects (A1, A2, Description, ...)Xctassertequals (A1, A2, Description, ...)xctassertequalswithaccuracy (A1, A2, Accuracy,description, ...)xctfail (Description, ...)Xctassertnil (A1, Description, ...)Xctassertnotnil (A1, Description, ...)xctasserttrue (expr, description, ...)Xctasserttruenothrow (expr, description, ...)Xctassertfalse (expr, description, ...)Xctassertfalsenothrow (expr, description, ...)xctassertthrows (expr, description, ...)xctassertthrowsspecific (expr, specificexception, description, ...)xctassertthrowsspecificnamed (expr, specificexception, aName, Description, ...)Xctassertnothrow (expr, description, ...)xctassertnothrowspecific (expr, specificexception, description, ...)xctassertnothrowspecificnamed (expr, specificexception, aName, Description, ...)
Come on moving up general test
-(void)testFloatEqual{ NSLog(@"这是自定义的测试方法"); float a=100.00; 100.00,@"测试不通过");}
Class Test
First create a class named Personmodel in Xcode and add a property named name
@interface PersonModel : NSObject@property(nonatomic,strong)NSString *name;@end
@implementation PersonModel-(id)init{ if (self=[super init]) { [email protected]"单元测试"; } returnself;}@end
To create a class in a test cell
Right-click, New Test Class
Note that this type of inheritance is associated with xctestcase
Introducing in the header file#import "PersonModel.h"
Customizing a test method
-(void)testClassName{ PersonModel *perModel=[[PersonModel alloc]init]; XCTAssertEqual(perModel.name, @"单元测试",@"name跟预期的不相等呀");}
Today wrote a lot of module functions, detection is normal
Really happy, the use of unit test testing, are normal, happy heart to work. O (∩_∩) o
Talking about iOS unit test