Goal 1: Quickly verify the correctness of a function using UnitTest Goal 2: Manipulate the UI of any App using UITest

First, UnitTes first experience

1. Create a Demo project and select itinclude Tests

Create a new Caculater file and set Max to Max

Input: string 1,2,3,5,8 output: maximum value in a string

+ (float)max:(NSString *)valueStr {
    NSArray *array = [valueStr componentsSeparatedByString:@","];
    float max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];
    return max;
}
Copy the code

In the process of actual development, it is better to add a step of data verification. For convenience’s sake, let’s assume that the valueStr input is correct.

3. Create a new test file CaculaterTests in DemoTest

#import <XCTest/XCTest.h>
#import "Caculator.h"

@interface CaculaterTests : XCTestCase

@end

@implementation CaculaterTests

- (void)testMax{
    float value = [Caculator max:@"1,2,3"];
    float expectValue = 3;
    XCTAssertEqual(value, expectValue);
}

@end

Copy the code

4. Perform tests

Method 1: Shortcut keyscmd+uTest it all in Method 2: Click the TEST button in the IDE

Test successful!

At this point, you should be confident that the function you wrote is correct! If you want to learn more, check out unit testing in iOS.

First experience of UITest

In mobile client testing we often need to manipulate the UI.

Action: Open Safari – type youku.com – swipe 5 times

-(void)testSafariSlide{
    
    XCUIApplication *safari = [[XCUIApplication alloc] initWithBundleIdentifier:@"com.apple.mobilesafari"];
    [safari launch];
    
    XCUIElement *inputTf = safari.otherElements[@"URL"];
    XCTAssertTrue([inputTf waitForExistenceWithTimeout:3]);
    [inputTf tap];
    [safari.textFields[@"URL"] typeText:@"https://youku.com"];
    [safari.buttons[@"go"] tap];
    for (int i = 0; i< 5; i++) {
        [safari swipeUp];
    }
    
    sleep(10);
}
Copy the code