XCTest
一般都会默认创建一个XCTest的target
如果没有创建,那就按照下面的步骤创建一个XCTest的target
如果出现.h 头文件找不到,需要在XCTest target中配置framework_search_path , library_search_path, header_search_path等值
可以在测试文件中加入私有变量
@interface testCalculationVC : XCTestCase{@private UIApplication *app; UIView *testView;}
测试控制器的视图是否存在
- (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.
性能测试
- (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-SW35
需要整理一下
无条件的断言,任何情况下都会触发
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 TestsXCTAssertTrue. 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 TestsXCTAssertNil. 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 TestsXCTAssertThrows. 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