Behave 1.0.21

Behave 1.0.21

Derek Bronston维护。



Behave 1.0.21

  • 作者
  • Derek Bronston和Denis Efimov

BEHAVE

N|Solid

什么是Behave

Behave是一个非常轻量级且灵活的Swift库,旨在帮助iOS开发人员编写简单的BDD(UI测试),且执行速度快。

注意:在ExampleApp中包含了使用XCTestUI和Behave编写的测试的左右对比

安装

CocoaPods

CocoaPods是Cocoa项目的依赖管理器。有关使用和安装说明,请访问他们的网站。要使用CocoaPods将Behave集成到Xcode项目中,请在您的Podfile中指定它

pod 'Behave', '~> 1.0'

Behave测试的解剖结构 声明在您的XCTestCase文件中声明一个 Behaviour的实例

let api = Behaviour()

期望将测试用例包裹在期望中。使用testTimeInterval值作为超时值

func testMyBehavior() {
  let expectations = expectation(description: "Fulfill")
  // TEST CODE WILL LIVE HERE
  let api = Behaviour()
  waitForExpectations(timeout: api.testTimeInterval){ error in }
}

listen: listen方法将事件添加到您的测试队列,然后监听它被触发。

api.listen(for: "my-view") {
    // This completion handler gets called once the object has been detected
}
  1. 类似于XCUITest,Behave依赖于accessibility identifiers。您需要在代码中添加它们,以便访问您的代码中的元素。它们可以直接在代码中添加,也可以通过Interface Builder添加。
  2. Behave监听事件完成。当事件完成时,您可以触发一个动作或验证一些状态变化。
  3. 当事件被触发时,完成处理程序将被调用。对测试中添加的每个事件都按其添加顺序进行测试,先进先出。事件是同步触发的。

执行:要让您的测试运行,请使用run方法。如果没有显式调用run,Behave测试不会运行。Run有一个fail完成处理程序,这将在您的任何事件未触发时被触发。Behave会返回标识符,以帮助您识别问题。

  api.run(fail: { error in
    XCTFail(error)
    expectations.fullfill()
  })

示例测试下面的测试包含在我们的示例应用中。它测试了一个简单的登录流程。

func testGivenTheUsersEntersCredsWhenTheUserTapsSubmitAndTheRequestSucceedsThenDisplayTheHomeScreen() {
        let expectations = expectation(description: "\(#function)")
        let api = Behaviour()
        api.listen(for: "login-view") {
            api.stubNetworkRequest(stub: Stub(httpMethod: HTTPMethods.post, httpResponse: 200, jsonReturn: "{\"success\":\"true\"}"))
            api.typeIntoTextField(identifier: "email", text: "email")
            api.typeIntoTextField(identifier: "password", text: "password")
            api.tapButton(identifier: "submit")
        }
        api.listen(for: "home-view") {
            expectations.fulfill()
        }
        api.run(fail: { error in
           XCTFail(error)
           expectations.fulfill()
        })
        waitForExpectations(timeout: api.testTimeInterval)
    }

API

query(identifier: String) -> UIView?
stubNetworkRequest(stub: Stub, httpResponse: Int32, jsonReturn: String)
typeIntoTextField(identifier: String, text: String)
typeIntoSecureTextField(identifier: String, text: String)
tapRightNavigationItem(with object: Any? = nil, with additionalObject: Any? = nil)
tapBackButton()
tapButton(identifier: String)
selectTabOnTabBar(index: Int)
selectTableRow(identfier: String, indexPath: IndexPath)
scrollTableTo(indexPath: IndexPath, identfier: String)
selectCollectionItem(identfier: String, indexPath: IndexPath)
selectEmebeddedCollectionItem(parentView: UIView, identfier: String, indexPath:IndexPath)
waitForAlert(complete: @escaping () -> Void)
Behave由 Freshly 研发,并由 Derek Bronston、Denis Efimov 以及 Freshly iOS 团队维护。