2017-06-16 115 views
0

我正在嘗試在示例應用程序中使用KIF學習自動UI測試。我的簡單測試連續失敗。 我的代碼:iOS KIF測試失敗

#import <XCTest/XCTest.h> 
#import <KIF/KIF.h> 

@interface AutomatedUITestsSampleUITests : KIFTestCase 

@end 

@implementation AutomatedUITestsSampleUITests 

- (void)setUp { 
    [super setUp]; 

    // Put setup code here. This method is called before the invocation of each test method in the class. 

    // In UI tests it is usually best to stop immediately when a failure occurs. 
    self.continueAfterFailure = NO; 
    // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 
    [[[XCUIApplication alloc] init] launch]; 

    // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 
} 

- (void)tearDown { 
    // Put teardown code here. This method is called after the invocation of each test method in the class. 
    [super tearDown]; 
} 

- (void)testExample { 

    [tester waitForViewWithAccessibilityLabel:@"LOGIN - Button"]; 

    [tester tapViewWithAccessibilityLabel:@"LOGIN - Button" traits:UIAccessibilityTraitButton]; 
} 

我已經設置輔助標籤LOGIN - Button到一個UIButton在我的故事板也可訪問已啓用。

我在控制檯收到此:

Test Case '-[AutomatedUITestsSampleUITests testExample]' started. 
    t =  0.00s  Start Test at 2017-06-16 13:45:07.071 
    t =  0.00s  Set Up 
    t =  0.04s   Launch com.UITests.AutomatedUITestsSample 
    t =  4.63s    Waiting for accessibility to load 
    t =  8.77s    Wait for app to idle 
    t = 10.48s  Tear Down 
Test Case '-[AutomatedUITestsSampleUITests testExample]' failed (10.868 seconds). 
Test Suite 'AutomatedUITestsSampleUITests' failed at 2017-06-16 13:45:17.939. 
    Executed 1 test, with 1 failure (1 unexpected) in 10.868 (12.735) seconds 
Test Suite 'AutomatedUITestsSampleUITests.xctest' failed at 2017-06-16 13:45:17.940. 
    Executed 1 test, with 1 failure (1 unexpected) in 10.868 (12.739) seconds 
Test Suite 'All tests' failed at 2017-06-16 13:45:17.941. 
    Executed 1 test, with 1 failure (1 unexpected) in 10.868 (12.743) seconds 

這是一個非常簡單的測試。爲什麼失敗?提前致謝。

回答

2

如果您有權訪問XCUIApplication,則您的項目配置有問題。 KIF測試目標應該是「單元測試目標」,而不是「UI測試目標」。使用正確的配置,不需要啓動應用程序(也不可能啓動應用程序啓動) - 它會在單元測試開始時啓動。當您使用UI測試目標時,您的測試將在單獨的進程中運行,並且KIF無法訪問您的應用程序。

我建議用this guide

+0

是啊,我在UI測試用KIF而不是單元測試的重新配置你的測試目標 –