Generally speaking, unit test is to test the functions in the software, and the coverage of unit test also indirectly represents the maturity of the code

So here’s a quick look at unit testing in ios —- case demo

The preparatory work

When creating a project, the system automatically creates a folder with the Tests and UITests endings on the same level as our normal development project. This is the unit Tests directory folder, and you can use Command + U to run the written unit test code

You can also run a block of code by clicking on the box in front of the block, as shown below, and clicking on the execute button will appear to test the block (more on that later).

The folders at the end of Tests are for normal test logic, and UITest is for writing UI test code. The following classes inherit from XCTestCase classes and can be used to write test code

Unit testing

M file, using the setUp, tearDown, testExample methods to test the normal code

SetUp for initialization work, tearDown for case destruction, testExample for testing content, and testPerformanceExample for performance testing

The logical test

Here is the code for a logical test. XCTAssertEqual checks to see if the result is the same as the specified result, or an error is reported

// Logic test
- (void)test1 {
    NSInteger a = 10, b = 20;
    NSInteger r = [Store plus:a with:b];
    //XCTAssertEqual(r, 30, @" logic ok ");
    XCTAssertEqual(r, 20The @"The logic is broken.");
}
Copy the code

An error is reported if the condition is not met

Meet the conditions

Asynchronous, execution time tests

Can be updated by expectationWithDescription timeout hint text, when asynchronous tests need waitForExpectationsWithTimeout way open timeout monitoring, The timeout field is the maximum time limit (the system will allow a slight time error)

After the asynchronous logic is executed, the fulfill method of XCTestExpectation needs to be called to fill the time to mark the completion time, otherwise it will time out

XCTestExpectation * ec = [self expectationWithDescription: @ "did not meet the requirements of the time"]. [Store loadData:^(id data){XCTAssertNotNil(data); // Check whether data is empty [ec fulfill];// Time padding}]; // Timeout, More than this time fail, the system will have certain error range [self waitForExpectationsWithTimeout: 2 handler: ^ (NSError * _Nullable error) {NSLog (@ "% @", the error); }]; }Copy the code

The performance test

Performance testing has to refer to the testPerformanceExample method, which has a block of code that is averaged multiple times during execution to test performance

// Performance tests, only relativistic, which enable asynchronous multiple tests to take an average
- (void)testPerformanceExample {
    // This is an example of a performance test case.
    // You can click on the left of the following line to set the average execution time that can be received
    [self measureBlock:^{
        // Put the code you want to measure the time of here.
        // Test performance code can be placed here
        [Store loop];
    }];
}
Copy the code

Once the code has finished executing, a dark tick appears in front of measureBlock. Click On It and click Edit to set a satisfactory time for the code block to execute, so you can see the result

The last

So that’s the end of unit testing. You can click on the XCTestCase class, look at the document document, and see other ways to do it