XCTest

XCTesthtml

通常都會默認建立一個XCTest的targetexpress

若是沒有建立,那就按照下面的步驟建立一個XCTest的targetxcode

 

若是出現.h 頭文件找不到,須要在XCTest target中配置framework_search_path , library_search_path, header_search_path等值cookie

 

能夠在測試文件中加入私有變量app

@interface testCalculationVC : XCTestCase
{
@private
    UIApplication *app;
    UIView *testView;

}

 

測試控制器的視圖是否存在less

- (void) testCalcView {
   // setup
   app = [NSApplication sharedApplication];
   calcViewController = (CalcViewController*)[NSApplication sharedApplication] delegate];
   calcView             = calcViewController.view;
 
   XCTAssertNotNil(calcView, @"Cannot find CalcView instance");
   // no teardown needed
}

測試異步操做異步

// Test that the document is opened. Because opening is asynchronous,
// use XCTestCase's asynchronous APIs to wait until the document has
// finished opening.
- (void)testDocumentOpening
{
    // Create an expectation object.
    // This test only has one, but it's possible to wait on multiple expectations.
    XCTestExpectation *documentOpenExpectation = [self expectationWithDescription:@"document open"];
 
    NSURL *URL = [[NSBundle bundleForClass:[self class]]
                              URLForResource:@"TestDocument" withExtension:@"mydoc"];
    UIDocument *doc = [[UIDocument alloc] initWithFileURL:URL];
    [doc openWithCompletionHandler:^(BOOL success) {
        XCTAssert(success);
        // Possibly assert other things here about the document after it has opened...
 
        // Fulfill the expectation-this will cause -waitForExpectation
        // to invoke its completion handler and then return.
        [documentOpenExpectation fulfill];
    }];
 
    // The test will pause here, running the run loop, until the timeout is hit
    // or all expectations are fulfilled.
    [self waitForExpectationsWithTimeout:1 handler:^(NSError *error) {
        [doc closeWithCompletionHandler:nil];
    }];
}
For more details on writing methods for asynchronous operations, see the XCTestCase+AsynchronousTesting.h header file in XCTest.framework.

性能測試async

- (void) testAdditionPerformance {
    [self measureBlock:^{
        // set the initial state
        [calcViewController press:[calcView viewWithTag: 6]];  // 6
        // iterate for 100000 cycles of adding 2
        for (int i=0; i<100000; i++) {
           [calcViewController press:[calcView viewWithTag:13]];  // +
           [calcViewController press:[calcView viewWithTag: 2]];  // 2
           [calcViewController press:[calcView viewWithTag:12]];  // =
        }
    }];
}

 

經常使用的斷言寫法

https://developer.apple.com/library/prerelease/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/04-writing_tests.html#//apple_ref/doc/uid/TP40014132-CH4-SW35ide

須要整理一下oop

無條件的斷言,任何狀況下都會觸發

XCTFail(format...)

 

是否相等的斷言

是否相等

XCTAssertEqualObjects(expression1, expression2, format...)

是否不相等 

XCTAssertEqualObjects. Generates a failure when expression1 is not equal to expression2 (or one object is nil and the other is not).

XCTAssertEqualObjects(expression1, expression2, format...)
XCTAssertNotEqualObjects. Generates a failure when expression1 is equal to expression2.

XCTAssertNotEqualObjects(expression1, expression2, format...)
XCTAssertEqual. Generates a failure when expression1 is not equal to expression2. This test is for scalars.

XCTAssertEqual(expression1, expression2, format...)
XCTAssertNotEqual. Generates a failure when expression1 is equal to expression2. This test is for scalars.

XCTAssertNotEqual(expression1, expression2, format...)
XCTAssertEqualWithAccuracy. Generates a failure when the difference between expression1 and expression2 is greater than accuracy. This test is for scalars such as floats and doubles, where small differences could make these items not exactly equal, but works for all scalars.

XCTAssertEqualWithAccuracy(expression1, expression2, accuracy, format...)
XCTAssertNotEqualWithAccuracy. Generates a failure when the difference between expression1 and expression2 is less than or equal to accuracy. This test is for scalars such as floats and doubles, where small differences could make these items not exactly equal, but works for all scalars.

XCTAssertNotEqualWithAccuracy(expression1, expression2, accuracy, format...)
XCTAssertGreaterThan. Generates a failure when expression1 is less than or equal to expression2. This test is for scalar values.

XCTAssertGreaterThan(expression1, expression2, format...)
XCTAssertGreaterThanOrEqual. Generates a failure when expression1 is less than expression2. This test is for scalar values.

XCTAssertGreaterThanOrEqual(expression1, expression2, format...)
XCTAssertLessThan. Generates a failure when expression1 is greater than or equal to expression2. This test is for scalar values.

XCTAssertLessThan(expression1, expression2, format...)
XCTAssertLessThanOrEqual. Generates a failure when expression1 is greater than expression2. This test is for scalar values.

XCTAssertLessThanOrEqual(expression1, expression2, format...)

 

Boolean Tests
XCTAssertTrue. Generates a failure when expression evaluates to false.

XCTAssertTrue(expression, format...)
XCTAssert. Generates a failure when expression evaluates to false. Synonymous with XCTAssertTrue.

XCTAssert(expression, format...)
XCTAssertFalse. Generates a failure when expression evaluates to true.

XCTAssertFalse(expression, format...)

 

Nil Tests
XCTAssertNil. Generates a failure when the expression parameter is not nil.

XCTAssertNil(expression, format...)
XCTAssertNotNil. Generates a failure when the expression parameter is nil.

XCTAssertNotNil(expression, format...)

 

Exception Tests
XCTAssertThrows. Generates a failure when expression does not throw an exception.

XCTAssertThrows(expression, format...)
XCTAssertThrowsSpecific. Generates a failure when expression does not throw an exception of a specific class.

XCTAssertThrowsSpecific(expression, exception_class, format...)
XCTAssertThrowsSpecificNamed. Generates a failure when expression does not throw an exception of a specific class with a specific name. Useful for those frameworks like AppKit or Foundation that throw generic NSException with specific names (NSInvalidArgumentException and so forth).

XCTAssertThrowsSpecificNamed(expression, exception_class, exception_name, format...)
XCTAssertNoThrow. Generates a failure when an expression does throw an exception.

XCTAssertNoThrow(expression, format...)
XCTAssertNoThrowSpecific. Generates a failure when expression does throw an exception of the specified class. Any other exception is OK; that is, it does not generate a failure.

XCTAssertNoThrowSpecific(expression, exception_class, format...)
XCTAssertNoThrowSpecificNamed. Generates a failure when expression does throw an exception of a specific class with a specific name. Useful for those frameworks like AppKit or Foundation that throw generic NSException with specific names (NSInvalidArgumentException and so forth).

XCTAssertNoThrowSpecificNamed(expression, exception_class, exception_name, format...)

 

 

UITest

https://developer.apple.com/library/prerelease/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/09-ui_testing.html#//apple_ref/doc/uid/TP40014132-CH13-SW1

https://developer.apple.com/videos/play/wwdc2015/406/

https://developer.apple.com/library/prerelease/content/samplecode/UnitTests/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011742-Intro

 

 

用系統自帶的UITest target或者本身創建一個target,選擇某個方法,而後點擊左下角的錄製鍵。app會啓動,而後會自動用代碼記錄用戶點擊的操做。

 

以下的代碼都是xcode自動生成的

- (void)testExample {
    // Use recording to get started writing UI tests.
    // Use XCTAssert and related functions to verify your tests produce the correct results.
    
   
    XCUIApplication *app = [[XCUIApplication alloc] init];
    XCUIElementQuery *tabBarsQuery = app.tabBars;
    [tabBarsQuery.buttons[@"Device"] tap];
    [[app.tables.cells containingType:XCUIElementTypeStaticText identifier:@"AirCondition"].buttons[@"add device"] tap];
    [tabBarsQuery.buttons[@"Mine"] tap];

}

而後就能夠運行測試這端代碼

 

能夠在測試代碼中下斷點,而後觀察相關的value

po cookiesButton.value

XCUIApplication

XCUIElement

XCUIElementQuery

 

let allButtons = app.descendantsMatchingType(.Button)

let allCellsInTable = table.descendantsmatchingType(.Cell)

 

convinience API

let allButtons = app.buttons

let allCellsInTable = table.cells

 

let cellQuery = cells.containingType(.StaticText,identifier:"Groceries")

let subscripting = table.staticTexts["Groceries"]

let Index = table.staticTexts.elementAtIndex(0)

let Unique = app.navigationBars.element

 

 

 

參考資料

testing with xcode

https://developer.apple.com/library/prerelease/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/01-introduction.html#//apple_ref/doc/uid/TP40014132

sample code

Unit Testing Apps and Frameworks

https://developer.apple.com/library/prerelease/content/samplecode/UnitTests/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011742-Intro

相關文章
相關標籤/搜索