【问题标题】:Group together Jasmine's it specs into a single test run将 Jasmine 的 IT 规格组合到一个测试运行中
【发布时间】:2015-06-25 09:48:25
【问题描述】:

我有以下情况:

describe('Base test suite', function () {
    beforeEach(function (done) {
        // an initialization routine with done as the callback
        veryLongInitialization(done);
    });

    it('description of first spec', function (done) {
        // first expectations

        done();
    });

    describe('nested suite that builds upon the base test suite', function () {
        beforeEach(function (done) {
            secondVeryLongInitialization(done);
        });

        it('description of second spec', function (done) {
            // second expectations
        });
    });

    describe('second nested suite that builds upon the base test suite', function () {
        beforeEach(function (done) {
            thirdVeryLongInitialization(done);
        });

        it('description of third spec', function (done) {
            // third expectations
        });
    });
});

如您所见,我们有一些嵌套的测试套件。在内部调用之前调用外部beforeEach 非常重要。

  • 执行外部期望时(第一个规范),veryLongInitialization 必须已完成。
  • 对于第二个规范(第一个嵌套规范),要求veryLongInitializationsecondVeryLongInitialization 已按顺序执行并成功完成。
  • 同样,对于第三个规范(第二个嵌套规范),要求veryLongInitializationthirdVeryLongInitialization 已按顺序执行并成功完成。

到目前为止效果很好。

但是,第一个规范有很多期望,所以基本上不可能写一个简短的描述。

因此,我们考虑将第一个规范拆分为多个规范,每个规范只有一些期望。这将使编写有意义的简短规范描述变得容易。

但是,这会减慢整个测试的执行速度,因为必须为每个规范执行 veryLongInitialization

所以我们考虑将veryLongInitialization 放入beforeAll 块而不是beforeEach。这将只执行一次veryLongInitialization,然后运行所有规范,从而加快整体测试执行速度。

然而,这意味着veryLongInitialization 只会运行一次。这意味着第三个规范将在序列之后运行

veryLongInitialization()
secondVeryLongInitialization()
thirdVeryLongInitialization()

这是不正确的。 secondVeryLongInitialization 不应该在第三个规范之前运行。

我希望你能看到我们的困境。 对于这种情况有更好的方法吗?

我们希望看到的是某种将规范组合在一起的方式,例如:

it('should do this', function () {
    ...
}).and('should do that', function () {
    ...
}).and('should also behave like this', function () {
    ...
}).and('should also behave like that', function () {
    ...
});

这些规范将在一次测试运行中运行,所有这些规范只有一个 beforeEach

【问题讨论】:

    标签: jasmine


    【解决方案1】:

    我认为这可行:将第一步规范合并到一个单独的 describe 中,然后在所有这些规范之前运行 veryLongInitialization。之后在第二个describesecondVeryLongInitialization 之前运行veryLongInitialization。第三步也一样。这样您就必须运行veryLongInitialization 三次,但根据您的描述,这实际上是特定套件所需要的。如果您担心 veryLongInitialization 的代码重复,可以将其移至单独的函数并在需要时多次调用。

    describe('Base test suite', function () {
    
        // for multiple time usage
        var veryLongInitialization = function (done) {
            done();
        };
    
        describe('first', function () {
    
            beforeAll(function (done) {
                veryLongInitialization(done);
            });
    
            // ...
        });
    
        describe('second', function () {
    
            beforeAll(function (done) {
                veryLongInitialization(done);
            });
    
            beforeEach(function (done) {
                secondVeryLongInitialization(done);
            });
    
            // ...
        });
    
        describe('third', function () {
    
            beforeAll(function (done) {
                veryLongInitialization(done);
            });
    
            beforeEach(function (done) {
                thirdVeryLongInitialization(done);
            });
    
            // ...
        });
    
    });
    

    【讨论】:

    • 你说得对,这很可能是最好的解决方案。太糟糕了,这使得嵌套测试套件的可用性有所降低。
    猜你喜欢
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 2014-08-17
    • 2021-02-15
    • 1970-01-01
    • 2016-06-13
    相关资源
    最近更新 更多