Gee*_*e.E 6 unit-testing ios quick-nimble
我写了一个包含beforeEach
和的示例组的测试用例afterEach
.我希望每个beforeEach
和afterEach
将每个调用一次it
.
唉,单个it
的beforeEach
,并afterEach
得到了多次调用.
我查看了一些文档(即Quick自己的文档和http://jasmine.github.io/2.1/introduction.html),但这些文档并没有帮助我.
这是一个小片段,演示了这一点:
class CheckerTests:QuickSpec {
override func spec() {
describe("something") {
beforeEach {
tLog.info("describe before")
}
afterEach {
tLog.info("describe after")
}
context("of something") {
beforeEach {
tLog.info("context before")
}
afterEach {
tLog.info("context after")
}
it("should behave like something") {
tLog.info("in the `IT`")
expect(true).to(beTrue())
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
我的控制台记录:
以上日志提出了两个问题:
beforeEach
和afterEach
现在被称为; 我也不知道为什么我会看到多个日志调用它们.为什么他们多次被召唤?- 上面的日志显示在示例通过之前context
调用了块之后的... 在示例之后不应该发生这种情况吗?
从我的代码片段中我会期望返回日志:
有人可以解释这里发生了什么吗 这是正确的行为吗?
编辑:
正如评论所建议的那样; 我还添加了日志内的it
示例(参见上述修改后的代码片段).这给了我以下日志:
Test Suite 'CheckerTests' started at 2017-05-18 13:35:29.025
Test Case '-[CheckerTests something__of_something__should_behave_like_something]' started.
13:35:29.046 INFO CheckerTests.spec():21 - describe before
13:35:29.046 INFO CheckerTests.spec():21 - describe before
13:35:29.048 INFO CheckerTests.spec():29 - context before
13:35:29.048 INFO CheckerTests.spec():29 - context before
13:35:29.048 INFO CheckerTests.spec():36 - in the `IT`
13:35:29.048 INFO CheckerTests.spec():36 - in the `IT`
13:35:29.049 INFO CheckerTests.spec():32 - context after
1Test Case '-[CheckerTests something__of_something__should_behave_like_something]' passed (0.024 seconds).
3:35:29.049 INFO CheckerTests.spec():32 - context after
13:35:29.050 INFOTest Suite 'CheckerTests' passed at 2017-05-18 13:35:29.050.
Executed 1 test, with 0 failures (0 unexpected) in 0.024 (0.025) seconds
CheckerTests.spec():24 - describe after
13:35:29.050 \360\237\222Test Suite 'CheckerTests.xctest' passed at 2017-05-18 13:35:29.051.
Executed 1 test, with 0 failures (0 unexpected) in 0.024 (0.026) seconds
\231 INFO CheckerTests.spec():24 - describe after
Test Suite 'Selected tests' passed at 2017-05-18 13:35:29.051.
Executed 1 test, with 0 failures (0 unexpected) in 0.024 (0.029) seconds
Run Code Online (Sandbox Code Playgroud)
上面的日志告诉我示例运行了两次,这让我更加困惑.
编辑:
其中一个问题得到了回答:
- 上面的日志显示在示例通过之前 context
调用了块之后的... 在示例之后不应该发生这种情况吗?
似乎测试按照正确的顺序进行跟进,以便回答上述问题.
编辑:
以供参考; 这就是我的Podfile的样子:
def pods_for_testing
pod 'Quick'
pod 'Nimble'
pod 'KIF'
end
target 'Checker' do
project 'Checker.xcodeproj', 'dev' => :debug, 'ntrl' => :debug, 'acpt' => :release, 'prod' => :release, 'prod appstore' => :release
pod 'SQLCipher'
pod 'UrbanAirship-iOS-SDK'
pod 'TBXML', :inhibit_warnings => true
pod 'SSZipArchive'
pod 'Google/Analytics'
pod 'Moya', '>= 8.0'
pod 'Unbox'
pod 'ProcedureKit'
pod 'ProcedureKit/Mobile'
pod 'SwiftyBeaver'
pod 'OHHTTPStubs'
pod 'OHHTTPStubs/Swift'
target 'CheckerTests' do
inherit! :search_paths
pods_for_testing
end
target 'CheckerUITests' do
inherit! :search_paths
pods_for_testing
end
end
Run Code Online (Sandbox Code Playgroud)
接下来我不确定其他设置可能会影响测试.
我尝试重现该问题。但就我而言,每个测试用例都只执行一个。
因此,该问题在正常设置中似乎无法重现。可能您有一些特殊设置导致了上述问题。
注意:
我不确定您还需要哪些其他库来获取“tLog.info”,但我找不到。我认为这对于这个目的来说并不重要。我用 print(_:) 语句代替。
这是我的“TryQuickTest.swift”:
import XCTest
import Quick
import Nimble
class TryQuickTest: QuickSpec {
override func spec() {
describe("blah") {
beforeEach {
print("describe before")
}
afterEach {
print("describe after")
}
context("of blah2") {
beforeEach {
print("context before")
}
afterEach {
print("context after")
}
it("first it should be like this") {
print("in the first `IT`")
XCTFail("Hey fail in first it")
}
it("second it should be like this") {
print("in the second `IT`")
XCTFail("Hey fail in second it")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的控制台用于运行此测试文件:
Test Suite 'Selected tests' started at 2017-05-28 07:35:32.345
Test Suite 'PlayQuickTests.xctest' started at 2017-05-28 07:35:32.347
Test Suite 'TryQuickTest' started at 2017-05-28 07:35:32.348
Test Case '-[PlayQuickTests.TryQuickTest blah__of_blah2__first_it_should_be_like_this]' started.
describe before
context before
in the first `IT`
/Users/shisui/Developer/General/iOS_Swift/PlayQuick/PlayQuickTests/TryQuickTest.swift:35: error: -[PlayQuickTests.TryQuickTest blah__of_blah2__first_it_should_be_like_this] : failed - Hey fail in first it
context after
describe after
Test Case '-[PlayQuickTests.TryQuickTest blah__of_blah2__first_it_should_be_like_this]' failed (0.004 seconds).
Test Case '-[PlayQuickTests.TryQuickTest blah__of_blah2__second_it_should_be_like_this]' started.
describe before
context before
in the second `IT`
/Users/shisui/Developer/General/iOS_Swift/PlayQuick/PlayQuickTests/TryQuickTest.swift:40: error: -[PlayQuickTests.TryQuickTest blah__of_blah2__second_it_should_be_like_this] : failed - Hey fail in second it
context after
describe after
Test Case '-[PlayQuickTests.TryQuickTest blah__of_blah2__second_it_should_be_like_this]' failed (0.003 seconds).
Test Suite 'TryQuickTest' failed at 2017-05-28 07:35:32.359.
Executed 2 tests, with 2 failures (0 unexpected) in 0.007 (0.010) seconds
Test Suite 'PlayQuickTests.xctest' failed at 2017-05-28 07:35:32.359.
Executed 2 tests, with 2 failures (0 unexpected) in 0.007 (0.012) seconds
Test Suite 'Selected tests' failed at 2017-05-28 07:35:32.374.
Executed 2 tests, with 2 failures (0 unexpected) in 0.007 (0.029) seconds
Run Code Online (Sandbox Code Playgroud)
从上面的输出来看。每个测试用例仅执行一次。
如果我删除第二个“it”部分。控制台输出如下所示:
Test Suite 'Selected tests' started at 2017-05-28 07:56:09.214
Test Suite 'PlayQuickTests.xctest' started at 2017-05-28 07:56:09.215
Test Suite 'TryQuickTest' started at 2017-05-28 07:56:09.215
Test Case '-[PlayQuickTests.TryQuickTest blah__of_blah2__first_it_should_be_like_this]' started.
describe before
context before
in the first `IT`
/Users/shisui/Developer/General/iOS_Swift/PlayQuick/PlayQuickTests/TryQuickTest.swift:35: error: -[PlayQuickTests.TryQuickTest blah__of_blah2__first_it_should_be_like_this] : failed - Hey fail in first it
context after
describe after
Test Case '-[PlayQuickTests.TryQuickTest blah__of_blah2__first_it_should_be_like_this]' failed (0.006 seconds).
Test Suite 'TryQuickTest' failed at 2017-05-28 07:56:09.222.
Executed 1 test, with 1 failure (0 unexpected) in 0.006 (0.007) seconds
Test Suite 'PlayQuickTests.xctest' failed at 2017-05-28 07:56:09.224.
Executed 1 test, with 1 failure (0 unexpected) in 0.006 (0.009) seconds
Test Suite 'Selected tests' failed at 2017-05-28 07:56:09.224.
Executed 1 test, with 1 failure (0 unexpected) in 0.006 (0.011) seconds
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
407 次 |
最近记录: |