为什么在使用Quick进行单元测试时多次调用before-andAach块?

时间:2017-05-18 09:52:54

标签: ios unit-testing quick-nimble

我用一些示例组编写了一个测试用例,包括beforeEachafterEach。 我希望每个beforeEach都会调用一次afterEachit

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

我查看过一些文档(例如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())
            }
        }
    }

}

}

我的控制台记录:

Console logs before

以上日志提出了两个问题:

  • 我不确定何时立即调用 beforeEachafterEach;我也不知道为什么我会看到多个日志调用它们。为什么他们被多次打电话?

- 上面的日志显示,在示例通过之前,context之后的it被称为 ...不应该发生之后示例?

从我的代码片段中我会期望返回日志:

Console logs after

有人能解释一下这里发生了什么吗?这是正确的行为吗?

修改

根据评论的建议;我还在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

以上日志告诉我示例运行了两次,这让我更加困惑。

修改
其中一个问题得到了回答:

- 上面的日志显示,在示例通过之前,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 之后的Dim f() as Variant ReDim f(0 To 0, 0 To 0) As Variant ReDim Preserve f(0 To UBound(f), 0 To UBound(f)) 被称为 ...不应该发生之后示例?

似乎测试按照正确的顺序进行跟进,以便回答上述问题。

修改

供参考;这就是我的Podfile的样子:

[15   34   70]

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

1 个答案:

答案 0 :(得分:2)

我试图重现这个问题。但就我而言,每个测试用例都只执行一次。

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

注意:
我不确定你有什么其他库来获取'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")
                }
            }
        }   
    }    
}


我的控制台用于运行此测试文件:

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

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

如果我删除第二个'它'部分。控制台输出如下所示:

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