为什么使用Quick进行unit testing时多次调用before-和afterEach块?

我写了一个包含beforeEachafterEach示例组的afterEach 。 而且我预计每个beforeEachafterEach每个都会被调用一次。

唉,对于一个单一的it beforeEachafterEach被多次调用。

我看了一些文档(即Quick的自己的文档和http://jasmine.github.io/2.1/introduction.html ),但这些都没有帮助我的原因。

这是一个小小的片段,演示了这一点:

类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()) } } } } 

}


我的控制台日志:

控制台日志之前

上面的日志提出了两个问题:

  • 我不知道什么时候 beforeEach和之后afterEach都被称为现在; 我也不知道为什么我看到多个日志打电话给他们。 他们怎么被叫多次?

上面的日志显示,在示例通过之前调用context …应该不会发生示例之后吗?

从我的代码片段,我会期待日志返回:

控制台日志之后

有人可以解释这里发生了什么? 这是正确的行为?


编辑:

正如评论所build议的那样; 我还在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 

上面的日志告诉我这个例子运行了两次,这让我更加困惑。


编辑:
其中一个问题是回答:

上面的日志显示,在示例通过之前调用context …应该不会发生示例之后吗?

看来testing按照正确的顺序进行,以便回答上述问题。


编辑:

以供参考; 这是我的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 

接下来,我不确定其他设置可能会影响testing。

我试图重现这个问题。 但就我而言,每个testing用例都正好执行一个。

因此,这个问题在正常情况下似乎不可重现。 可能你有一些特殊的设置,导致你在上面描述的问题。

注意:
我不知道还有什么其他库需要获取'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") } } } } } 

和我的控制台运行这个testing文件:

 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 

从上面的输出。 每个testing用例只执行一次。

如果我删除了第二个“它”的一部分。 控制台输出如下所示:

 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 
Interesting Posts