MTK 1.1.0

MTK 1.1.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
Released最后发布2018年3月
SwiftSwift 版本3.0
SPM支持 SPM

Nick GriffithLogan Gauthier 维护。



MTK 1.1.0

  • Nick Griffith 和 Logan Gauthier

MetovaTestKit

Build Status CocoaPods Compatible Documentation Coverage Status Platform Twitter

Metova 测试套件是一个有用的测试辅助工具集,旨在减轻编写 iOS 应用程序测试的负担。


要求

  • Swift 3.0
  • iOS 8.0

安装

Metova 测试套件可以通过 CocoaPods 获取。

Metova 测试套件旨在与单元测试目标一起使用。要安装它,请在项目的 Podfile 中添加 MTK

target 'YourApp' do
  # Your app's pods:
  pod 'DataManager'
  pod 'ThunderCats'
  pod 'MetovaBase'

  target 'YourAppTests' do
    inherit! :search_paths
    pod 'MTK'
  end
end

然后运行 pod install

如果您想测试 Metova 测试套件的测试版,您可以安装最新的 develop 版本

pod 'MTK', :git => 'https://github.com/metova/MetovaTestKit.git', :branch => 'develop'

使用方法

MTK可测试

Metova测试套件定义了MTK可测试协议。正确实现此协议允许进行功能单元测试。它将设置和取消设置的代码抽象成您想要测试的类型扩展,并允许进行功能单元测试。

func testOutlets() {
    HomeViewControllerClass.test { testVC in
        XCTAssertNotNil(testVC.userameTextField)
        XCTAssertNotNil(testVC.passwordTextField)
        XCTAssertNotNil(testVC.loginButton)
    }
}

测试UIKit组件

UIControl

通过单个断言,您可以验证您的控件动作是否正确连接,以及目标是否对将要发送给它的选择器做出响应。

MTKAssertControl(testVC.loginButton, sends: #selector(LoginViewController.didTapLoginButton(_:)), to: testVC, for: .touchUpInside, "The login button should be hooked up to the login action.") 

UIAlertController

验证显示的视图控制器是否有一个特定的样式、标题、消息和操作。

MTKAssertAlertIsPresented(
    by: testVC,
    style: .alert,
    title: "Warning",
    message: "Are you sure you want to delete this user?",
    actions: [
        ExpectedAlertAction(title: "Delete", style: .destructive),
        ExpectedAlertAction(title: "Cancel", style: .cancel)
    ]
)

UISegmentedControl

验证UISegmentedControl是否包含您期望的段标题。

MTKAssertSegmentedControl(segmentedControl, hasSegmentTitles: ["Followers", "Following"])

UIBarButtonItem

验证一个条形按钮项是否有预期的目标/操作对,并且目标确实对将要发送给它的选择器做出响应。

MTKAssertBarButtonItem(testVC.editBarButtonItem, sends: #selector(MyViewController.didTapEditButton(_:)), to: testVC) 

测试自动布局约束

您可以使用Metova Test Kit断言您没有出现破坏性的自动布局约束。

MTKAssertNoBrokenConstraints {
    // code that does anything that impacts the user interface
    // including simply loading a view for the first time
}

对于破坏的约束,此断言将失败并报告测试过程中破坏的约束数量。您还可以传递自定义信息。

MTKAssertNoBrokenConstraints(message: "Constraints were broken.") {
    // code to test
}

该测试还会返回一个包含破坏的约束数量的值。

let brokenConstraintCount = MTKAssertNoBrokenConstraints {
    // code to test
}

测试异常

您可以使用Metova Test Kit断言不应该抛出异常的代码没有抛出。如果没有MTK,这会导致整个测试套件崩溃。有MTK,这只是失败的测试,您仍然可以运行测试套件的其余部分。

MTKAssertNoException {
    // code that should not throw exceptions
    // results in passing test if no exceptions are thrown
    // results in failing test if exceptions are thrown
}

您还可以传递在失败时打印的消息。

MTKAssertNoException(message: "Exception was thrown.") {
    // code that should not throw exceptions
    // results in passing test if no exceptions are thrown
    // results in failing test if exceptions are thrown
}

您还可以测试代码以验证抛出了异常,而不会使您的测试套件崩溃。如果不关心具体的异常,只想验证代码块是否抛出了异常,您可以使用MTKAssertException

MTKAssertException {
    // code that should throw exceptions
    // results in passing test if an exception is thrown
    // results in a failing test if this closure returns without throwing
}

MTKAssertNoException类似,这个函数也接受一个消息。

MTKAssertException(message: "No exception was thrown.") {
    // code that should throw exceptions
    // results in passing test if an exception is thrown
    // results in a failing test if this closure returns without throwing
}

这些方法也返回抛出的异常,以便您获取更多关于异常的信息。

guard let exception = MTKAssertException(testBlock: throwingBlock) else {
    XCTFail("Block failed to throw an exception")
    return
}

// More assertion about the given exception that was returned
if let exception = MTKAssertNoException(testBlock: blockThatShouldntThrow) {
    XCTFail("Block should not have thrown but instead threw \(exception)")
    return
}

如果闭包没有抛出异常,则函数返回nil。否则,返回一个NSException实例,您可以验证它是否是您期望块抛出的异常。

异步测试

XCTest提供了使用expectation(description:)waitForExpectations(timeout:handler:)进行异步测试的能力。然而,当测试简单的延迟异步操作时,这种方法可能很繁琐,意图可能不是很明显。使用MTK的MTKWaitThenContinueTest(after:testAction:)实用方法,这类测试变得更简单,且更容易理解。

mockUserSearchNetworkRequest(withResponseTime: 0.5)
testViewController.didTapSearchButton()
 
XCTAssertFalse(testViewController.searchButton.isEnabled, "The search button should be disabled while a search request is taking place.")
 
MTKWaitThenContinueTest(after: 1) {
    XCTAssertTrue(testViewController.searchButton.isEnabled, "Once the request is complete, the search button should be re-enabled.") 
}

鸣谢

Metova Test Kit由Metova Inc.所有和维护。

贡献者

如果您想为Metova Test Kit做出贡献,请查看我们的CONTRIBUTING指南

Metova 测试套件横幅图像和其他资产由 Christi Johnson 提供。


许可证

Metova 测试套件遵循 MIT 许可协议。有关更多信息,请参阅 LICENSE 文件。