IOS native UI testing framework based on UITests, Xcode uses simple case

Title: iOS native UI testing framework based on UITests, Xcode uses simple case, tags:, [iOS, test, APP]

Preface

With the increase of APP function, just rely on black box testing to cover the artificial test undoubtedly has great limitations, the effect is not satisfactory, especially the new function can be increased after regression testing of the old function, produced a lot of duplication of work, and these are some repetitive work can be completed through the automated test the emergence of UITests is to solve this problem.

UItests introduction

What is UITests?

UItests is an automated test automation framework for testing UI interaction developed by Apple Corp

What can UItests do?

UItests can help us solve some duplication of work on testing, save labor costs and improve test efficiency

< –More–>

UITests usage

The newly created project can add
by hooking include UI Tests

IOS native UI testing framework based on UITests, Xcode uses simple case

Existing projects can be added through the following steps.

IOS native UI testing framework based on UITests, Xcode uses simple case
IOS native UI testing framework based on UITests, Xcode uses simple case

After you successfully add the UITests component, you can see the corresponding directory in the project file navigation.

IOS native UI testing framework based on UITests, Xcode uses simple case

UITestDemoUITests.m is the file used to write test cases

Note: the test methods start with test

Test code can be generated by recording and directly writing two methods. It is recommended to automatically generate and then modify the generated code automatically

Declare a new test method, click on the dot, automatically open the emulator, and the operation of the project will be recorded in the form of code,

IOS native UI testing framework based on UITests, Xcode uses simple case

automatic recording code is also very good understanding, click the login button and then click on the ID logo for prismatic Detail navigation bar on the Mune
button, click on the upper left corner is represented by the implementation of this test method, automatically open the simulator execution.
test method executed successfully in a green check mark, it is the red fork.
test will be in the console printing parameters, including the operation time, complete test will print a test log to save the address.

IOS native UI testing framework based on UITests, Xcode uses simple case

Simple test case

- (void) testLogi{XCUIElement *account [[XCUIApplication alloc] init].textFields[@ = "account"]; XCUIElement *pwd = [[XCUIApplication alloc] init].textFields[@ "password"]; XCUIApplication *app = [[XCUIApplication alloc] init]; / / judge whether the account password is not even jump directly to the next test / jump after waiting for 3S to return success = int login interface 0; int failed for (int = 0; I = 0; i< 10; i++) {int ACC = arc4random (PW = 1000)%; int arc4random ([account)% 1000; tap] [account; typeText:[NSString stringWithFormat:@%d, acc]] [pwd tap]; [pwd typeText:[NSString; stringWithFormat:@ pw]]; / / "%d". Click on the login [app.buttons[@ "login" t Ap]; / / simulation log normal network access for 3S [app.windows.navigationBars.element pressForDuration:3]; / / whether to jump to the login interface if it is successful login if ([app.navigationBars.element.identifier isEqualToString:@ "Detail"]) {[app.navigationBars[@ "Detail"].buttons[@ "Mune" tap]; NSLog (@ "login successful"); success}else{(NSLog = 1; @ "logon failure"); failed + = 1;}} (NSLog @ 10 test, successfully logged in%d times,%d times, success failure ", failed);}

Test results:

IOS native UI testing framework based on UITests, Xcode uses simple case

Assertion

Note: the assertion results will be recorded in the test log file, and the assertion fails, the test method does not pass

/ / assert that the most basic test, if expression is true through, or behind the print format string XCTAssert (expression, format...) //Bool test: XCTAssertTrue (expression, format...) XCTAssertFalse (expression, format...) / XCTAssertEqual (expression1, equility test expression2, format...) XCTAssertNotEqual (expression1, expression2, format... //double) float data comparison test using XCTAssertEqualWithAccuracy (expression1, expression2, accuracy, format...) XCTAssertNotEqualWithAccuracy (expression1, expression2, accuracy, format...) //Nil test, XCTAssert[Not]Nil asserts that the given expression value is nil XCTAssertNil (expression, format...) XCTAssertNotNil (expression, format...) / failed to assert XCT Fail (format...)