IOS-use unit test provided by XCODE
What is unit testing?
When I hear the word unit test, it feels very high. In fact, unit test is dedicated to writing a test function for your method. To ensure that your methods are constantly being modified and developed. Always correct. If an error occurs, let you know immediately, so that the minimum unit starts to monitor to ensure the quality of the software.
When to use unit testing:
1. After writing the code: Check whether the code you have written is correct.
2. Before writing the code: All functions are designed in modules before writing the code. After the test is passed, write again. (I have never used it ).
3. Fixed a bug: After fixing a bug, you can write a test to ensure the fix is successful.
How to Write Unit Tests
It seems that there is a lot of nonsense. Let's go to the theme directly.
Create a project and select include Unit Tests directly.
What if I forget to check it? You can create File --> new --> target --> iOS Unit Testing Bundle in other ways. Let's take a look at the name.
After the project is created, how can we start testing?
Find the. m file in the system unit test Testes folder and you will see several methods. Let's take a look at how these methods are called and their functions.
-(Void) setUp {[super setUp]; // Put setup code here. this method is 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 is called after the invocation of each test method in the class. // release the resource code of the test case. This method calls [super tearDown] after each test case is executed;}-(void) testExample {// This is an 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 start with test}-(void) testPerformanceExample {// This is an example of a performance test case. // test performance example [self measureBlock: ^ {// Put the code you want to measure the time of here. // code for Performance Testing}];}
Write a simple method in ViewController
- (int)getNum;
Implementation:
- (int)getNum {return 100;}
Import ViewController. h to the test file and define a vc attribute.
#import
#import "ViewController.h"@interface ____Tests : XCTestCase@property (nonatomic,strong) ViewController *vc;@end@implementation ____Tests
Implementation of Test Cases
-(Void) setUp {[super setUp]; // instantiate the class self to be tested. vc = [[ViewController alloc] init];}-(void) tearDown {// clear self. vc = nil; [super tearDown];}-(void) testMyFuc {// call the method to be tested, int result = [self. vc getNum]; // if not equal, the system prompts @ "test failed" XCTAssertEqual (result, 100, @ "test failed ");}
Command + u shortcut run, or produce --> test,
The project is running.
We can clearly see in the console that the example we want to test has passed, and the test method that passes the test will have a green hook.
At this time, let's change the assertion to 100 as the number, and then run comand + u to see what the situation is.
Obviously, it can pass, because the return value of the method to be tested is 100,
The built-in testing 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 (int i = 0; i<100; i++) { NSLog(@"dd"); }}];
}
In this example, we add a for loop to test its performance. Command + u run to see
QQ20160129-5.png
We can intuitively see the call time and use it to compare the performance advantages and disadvantages.
In addition, XCTest also supports asynchronous unit testing and I will not start it here. The commonly used assertions and explanations are attached.
XCTFail (format ...) Generate a failed test. If XCTAssertNil (a1, format...) is null, it is passed when a1 is null, otherwise it fails. XCTAssertNotNil (a1, format ...) If it is not null, a1 is passed when it is not null, and vice versa; XCTAssert (expression, format ...) when expression is evaluated as TRUE, it is passed; XCTAssertTrue (expression, format ...) when expression is evaluated as TRUE, it is passed; XCTAssertFalse (expression, format ...) when expression is evaluated as False, it is passed; XCTAssertEqualObjects (a1, a2, format ...) when the value of [a1 isEqual: a2] is TRUE, it is passed. If one of them is not empty, it is not passed. XCTAssertNotEqualObjects (a1, a2, format ...) when the value of [a1 isEqual: a2] is False, it is passed. XCTAssertEqual (a1, a2, format ...) equal judgment (when a1 and a2 are C language scalar, Struct, or consortium, determines the address of the variable. if the address is the same, TRUE is returned; otherwise, NO is returned); XCTAssertNotEqual (a1, a2, format ...) unequal judgment (used when a1 and a2 are c scalar, struct, or consortium); XCTAssertEqualWithAccuracy (a1, a2, accuracy, format ...) equality judgment. (double or float type) provides an error range, which is tested when the error range (+/-accuracy) is equal. XCTAssertNotEqualWithAccuracy (a1, a2, accuracy, format ...) unequal judgment, (double or float type) provides an error range, which is tested when the error range is not equal to; XCTAssertThrows (expression, format ...) exception test: When expression is abnormal, it is passed; otherwise, it fails ;( Abnormal) XCTAssertThrowsSpecific (expression, specificException, format ...) exception test: This method is used when expression specificException exception occurs. Otherwise, other exceptions or no exceptions fail. XCTAssertThrowsSpecificNamed (expression, specificException, exception_name, format ...) exception test: If expression encounters an exception or a name exception, the test is passed. If expression fails, XCTAssertNoThrow (expression, format ...) Exception test: test the expression when no exception occurs. XCTAssertNoThrowSpecific (expression, specificException, format ...) exception test: If expression does not have an exception or a specific exception name, the test is passed. Otherwise, the test fails. XCTAssertNoThrowSpecificNamed (expression, specificException, exception_name, format ...) exception test: If expression does not have an exception or a specific exception name, the test is passed. Otherwise, the test fails.