KIF和Quick / Nimble

时间:2015-01-29 08:08:14

标签: ios unit-testing integration-testing kif

我试图让KIFQuick/Nimble让iOS很好地一起玩,所以我可以使用QuickSpecs进行KIF测试。

我的测试目前看起来像这样:

class HomeSceenSpec: QuickSpec {

    override func spec() {
        describe("Home screen") {
            it("should have a failing test") {
                let tester = self.tester()
                tester.waitForViewWithAccessibilityLabel("Blah")
            }
        }
    }
}

文字' Blah'不存在,测试 应该失败。正在调用failWithException:stopTest:,但它不会引发异常或导致QuickSpec测试失败。

如何整合这两种技术?

2 个答案:

答案 0 :(得分:5)

failWithException:stopTest:调用recordFailureWithDescription:inFile:atLine:expected:可能会出现问题。 (这种方法正在进行大量的调整)。

我遇到的解决方案是在QuickSpec上创建一个类别/扩展名:

import Quick
import Nimble
import KIF

extension QuickSpec {

    func tester(_ file : String = __FILE__, _ line : Int = __LINE__) -> KIFUITestActor {
        return KIFUITestActor(inFile: file, atLine: line, delegate: self)
    }

    func system(_ file : String = __FILE__, _ line : Int = __LINE__) -> KIFSystemTestActor {
        return KIFSystemTestActor(inFile: file, atLine: line, delegate: self)
    }

    override public func failWithException(exception: NSException!, stopTest stop: Bool) {
        if let userInfo = exception.userInfo {
            fail(exception.description,
                file: userInfo["SenTestFilenameKey"] as String,
                line: userInfo["SenTestLineNumberKey"] as UInt)
        } else {
            fail(exception.description)
        }
    }
}

答案 1 :(得分:2)

我们刚刚发布了应该有用的KIF-Quick cocoapod,请参阅:

http://cocoapods.org/pods/KIF-Quick

这是一个规范的例子:

import Quick
import KIF_Quick

class LoginSpec: KIFSpec {
    override func spec() {
        describe("successful login") {
            context("home view") {
                beforeEach() {
                    tester().navigateToLoginPage()
                }

                it("should open Welcome page") {
                    viewTester().usingLabel("Login User Name").enterText("user@example.com")
                    viewTester().usingLabel("Login Password").enterText("thisismypassword")
                    viewTester().usingLabel("Log In").tap()
                    viewTester().usingLabel("Welcome").waitForView()
                }

                afterEach() {
                    tester().returnToLoggedOutHomeScreen()
                }
            }
        }
    }
}