ios swift 测试框架翻译之Quick例子和一组例子的有序测试
来源:互联网 发布:数据库优化面试题 编辑:程序博客网 时间:2024/06/06 19:14
Quick使用一种特殊的语法去明确这个例子和一组例子。
在使用XCTest进行高效的测试:有序,行为和断言时,我们知道一个好的测试方法名是非常重要的–当这个测试是一个失败的开始的时候,这个测试用一个最好的方式让我们决定要修改应用的代码或者更新代码。
Quick 例子(examples) 和组例子( example groups)提供两种目的 :
* 他们鼓励你去写测试的描述* 在你的测试"安排"步骤时他们极大地简化测试代码
使用它
的例子
例子,定义一个它的函数,去了解断言如何在测试用例中使用的。
在XCTest中这些测试方法是相似的。
它
接收两个参数:这个例子的名字和一个闭包函数。在下面的例子中详细说明Sea.Dolphin
类的行为,一个海豚应该是聪明的和友好的:
// Swiftimport Quickimport Nimbleimport Seaclass DolphinSpec: QuickSpec { override func spec() { it("is friendly") { expect(Dolphin().isFriendly).to(beTruthy()) } it("is smart") { expect(Dolphin().isSmart).to(beTruthy()) } }}
// Objective-C@import Quick;@import Nimble;QuickSpecBegin(DolphinSpec)it(@"is friendly", ^{ expect(@([[Dolphin new] isFriendly])).to(beTruthy());});it(@"is smart", ^{ expect(@([[Dolphin new] isSmart])).to(beTruthy());});QuickSpecEnd
使用描述去是你的测试用例更加整洁。描述可以是任何长度和使用任何字符,包括英语之外的其他字符,或者表情 符号:✌️
使用describe
and context
分组测试的例子
分组测试的例子是逻辑组的例子,例子能共享代代码setup
和 teardown
。
使用describe
描述类和类的方法
这个Dolphin
类的click
方法指定了一种特殊的行为–换句话说;去测试方法的工作–使用describe
可以将它的的几个方法放在一个组里进行测试,讲相似的测试分组放在一起将使得测试代码更容易阅读
// Swiftimport Quickimport Nimbleclass DolphinSpec: QuickSpec { override func spec() { describe("a dolphin") { describe("its click") { it("is loud") { let click = Dolphin().click() expect(click.isLoud).to(beTruthy()) } it("has a high frequency") { let click = Dolphin().click() expect(click.hasHighFrequency).to(beTruthy()) } } } }}// Objective-C@import Quick;@import Nimble;QuickSpecBegin(DolphinSpec)describe(@"a dolphin", ^{ describe(@"its click", ^{ it(@"is loud", ^{ Click *click = [[Dolphin new] click]; expect(@(click.isLoud)).to(beTruthy()); }); it(@"has a high frequency", ^{ Click *click = [[Dolphin new] click]; expect(@(click.hasHighFrequency)).to(beTruthy()); }); });});QuickSpecEnd
当着两个部分运行在Xcode,它们将会有以下的描述信息在这个describe和他的函数中:
1. DolphinSpec.a_dolphin_its_click_is_loud
2. DolphinSpec.a_dolphin_its_click_has_a_high_frequency
当再一次进行测试的时候;我们可以明确的知道这个测试用例是用来测试什么的;
模仿 Setup/Teardown
代码 使用 beforeEach
和 afterEach
分组的例子不仅只是使得代码更加清晰,在一个组测试中也可以使用setup
和teardowm
;
在下面的例子中,使用beforeEach
函数去创建dolphin的一个新的实例和在每个测它的clivk
方法之前;这样可以确保每一次的测试类都保持最新
状态:
// Swiftimport Quickimport Nimbleclass DolphinSpec: QuickSpec { override func spec() { describe("a dolphin") { var dolphin: Dolphin! beforeEach { dolphin = Dolphin() } describe("its click") { var click: Click! beforeEach { click = dolphin.click() } it("is loud") { expect(click.isLoud).to(beTruthy()) } it("has a high frequency") { expect(click.hasHighFrequency).to(beTruthy()) } } } }}// Objective-C@import Quick;@import Nimble;QuickSpecBegin(DolphinSpec)describe(@"a dolphin", ^{ __block Dolphin *dolphin = nil; beforeEach(^{ dolphin = [Dolphin new]; }); describe(@"its click", ^{ __block Click *click = nil; beforeEach(^{ click = [dolphin click]; }); it(@"is loud", ^{ expect(@(click.isLoud)).to(beTruthy()); }); it(@"has a high frequency", ^{ expect(@(click.hasHighFrequency)).to(beTruthy()); }); });});QuickSpecEnd
在dolphin
例子中setup
看起来不是那么的的必要;但是对于一些更复杂的对象来说,它省去很多的代码量!
每次执行这些代码后,都会执行afterEach
。
使用context
模拟特定的环境
当海豚发现一些他们感兴趣的事情的时候,通过回声的次数来进行定位,通过释放有一系列的声音去确定他是什么。
这个测试展示在不同的环境不同的习惯下的click
情景;通常下,海豚只会发出一次震动,但是当它们发现特别有趣的事情的时候,就会发出多次的震动;
这些情况可以使用context
函数:一个正常的context
和当海豚接近感兴趣的东西的context
:
// Swiftimport Quickimport Nimbleclass DolphinSpec: QuickSpec { override func spec() { describe("a dolphin") { var dolphin: Dolphin! beforeEach { dolphin = Dolphin() } describe("its click") { context("when the dolphin is not near anything interesting") { it("is only emitted once") { expect(dolphin!.click().count).to(equal(1)) } } context("when the dolphin is near something interesting") { beforeEach { let ship = SunkenShip() Jamaica.dolphinCove.add(ship) Jamaica.dolphinCove.add(dolphin) } it("is emitted three times") { expect(dolphin.click().count).to(equal(3)) } } } } }}
// Objective-C@import Quick;@import Nimble;QuickSpecBegin(DolphinSpec)describe(@"a dolphin", ^{ __block Dolphin *dolphin = nil; beforeEach(^{ dolphin = [Dolphin new]; }); describe(@"its click", ^{ context(@"when the dolphin is not near anything interesting", ^{ it(@"is only emitted once", ^{ expect(@([[dolphin click] count])).to(equal(@1)); }); }); context(@"when the dolphin is near something interesting", ^{ beforeEach(^{ [[Jamaica dolphinCove] add:[SunkenShip new]]; [[Jamaica dolphinCove] add:dolphin]; }); it(@"is emitted three times", ^{ expect(@([[dolphin click] count])).to(equal(@3)); }); }); });});QuickSpecEnd
严格的来说,context
和 describe
是同义词;但是严格的规范化会使你的代码更加容易理解;
易读的测试:Quick 和 XCTest
在Effective Tests Using XCTest: Arrange, Act, and Assert
中, 我们看到每一个测试都使用一种方式测试使得测试高效明确. 在XCTest, 使用一个很长的方法名来表示:
func testDolphin_click_whenTheDolphinIsNearSomethingInteresting_isEmittedThreeTimes() { // ...}
使用 Quick, 这种情况是更加容易去阅读和 我们能为每一个测试组去设置:
describe("a dolphin") { describe("its click") { context("when the dolphin is near something interesting") { it("is emitted three times") { // ... } } }}
暂时禁用Examples
或Groups
你能够暂时禁用Examples
或Groups
是测试时不通过;这个测试实例的名称和结果会打印出来,但是并不会执行该方法:
你可以在方法之前使用x
来禁用测试方法:
// Swiftxdescribe("its click") { // ...none of the code in this closure will be run.}xcontext("when the dolphin is not near anything interesting") { // ...none of the code in this closure will be run.}xit("is only emitted once") { // ...none of the code in this closure will be run.}
// Objective-Cxdescribe(@"its click", ^{ // ...none of the code in this closure will be run.});xcontext(@"when the dolphin is not near anything interesting", ^{ // ...none of the code in this closure will be run.});xit(@"is only emitted once", ^{ // ...none of the code in this closure will be run.});
临时的运行测试用例中的一个子集
有时,它可以使得我们更加专注一个或几个例子;运行一个或两个例子比全部运行更快。使用fit
函数你可以只运行一个或者两个。你也可以使用fdescribe
或fcontext
关注一组例子:
//-------swifitfit("is loud") { // ...only this focused example will be run.}it("has a high frequency") { // ...this example is not focused, and will not be run.}fcontext("when the dolphin is near something interesting") { // ...examples in this group are also focused, so they'll be run.}
//-----ocfit(@"is loud", { // ...only this focused example will be run.});it(@"has a high frequency", ^{ // ...this example is not focused, and will not be run.});fcontext(@"when the dolphin is near something interesting", ^{ // ...examples in this group are also focused, so they'll be run.});
全局Setup/Teardown
使用beforeSuite / afterSuite
有些测试需要在测试之前进行一些设置或者提前执行一些操作;基于以上的原因使用beforeSuite / afterSuite
。
在下面的例子中,数据库中所有的海洋中的生物都必须在海洋创建之后运行;在全部的测试实例完成后销毁:
// Swiftimport Quickclass DolphinSpec: QuickSpec { override func spec() { beforeSuite { OceanDatabase.createDatabase(name: "test.db") OceanDatabase.connectToDatabase(name: "test.db") } afterSuite { OceanDatabase.teardownDatabase(name: "test.db") } describe("a dolphin") { // ... } }}
// Objective-C@import Quick;QuickSpecBegin(DolphinSpec)beforeSuite(^{ [OceanDatabase createDatabase:@"test.db"]; [OceanDatabase connectToDatabase:@"test.db"];});afterSuite(^{ [OceanDatabase teardownDatabase:@"test.db"];});describe(@"a dolphin", ^{ // ...});QuickSpecEnd
如果你喜欢你也可是使用许多的beforeSuite
和afterSuite
;在测试运行之前执行所有的beforeSuite
;在测试完成之后执行所有的afterSuite
,在这里闭包是不能保证所有的按照顺序执行;
- ios swift 测试框架翻译之Quick例子和一组例子的有序测试
- ios swift 测试框架翻译之Quick例子和一ArrangeActAssert
- Android测试框架及例子
- VBA_EXECL_SQL测试的例子
- 复杂例子的测试
- 测试例子
- 用JUnit测试Spring管理Hibernate框架的一个例子
- j2ME 测试连网的例子
- RMI 测试通过的例子
- 测试分词的一些例子
- 测试framebuffer的简单例子
- 一个Testng的测试例子
- 测试矩阵乘法的例子
- 测试Theano_LSTM的Tutorial例子
- LoadRunner接口测试的例子
- Rhino| Rhino的测试例子
- NS3 ipv6例子代码测试和解析之ping6.cc
- Libevent0.1之测试Libevent自带的例子
- servlet demo
- Unresolved inclusion: <jni.h>
- linux下定时执行任务的方法
- C++静态库与动态库
- Exception in thread "http-bio-8037-exec-45" java.lang.OutOfMemoryError: PermGen space
- ios swift 测试框架翻译之Quick例子和一组例子的有序测试
- Jquery树形插件--应用举例
- Android 自定义字体样式
- 深度学习与计算机视觉 看这一篇就够了
- ArcGIS教程:创建图表的步骤
- iOS之修改项目BUG之旅--(六)
- java实现在线预览--poi实现word、excel、ppt转html
- 时间复杂度和空间复杂度详解
- B-tree/B+tree/B*tree