Jasmine/Protractor: stop test on failure in beforeEach

前端 未结 4 606
轻奢々
轻奢々 2021-02-15 08:14

I am currently writing tests protractor and I was wondering if there is some possibility to cancel test execution as soon as something in the beforeEach fails (and return some u

4条回答
  •  花落未央
    2021-02-15 09:13

    Strictly answering your question and without external dependencies:

    beforeEach(function() {
        // 1) login user
        expect(1).toBe(1);
        // This works on Jasmine 1.3.1
        if (this.results_.failedCount > 0) {
            // Hack: Quit by filtering upcoming tests
            this.env.specFilter = function(spec) {
                return false;
            };
        } else {
            // 2) set some user properties
            expect(2).toBe(2);
        }
    });
    
    it('does your thing (always runs, even on prior failure)', function() {
        // Below conditional only necessary in this first it() block
        if (this.results_.failedCount === 0) {
            expect(3).toBe(3);
        }
    });
    
    it('does more things (does not run on prior failure)', function() {
        expect(4).toBe(4);
    });
    

    So if 1 fails, 2,3,4,N won't run as you expect.

    There is also jasmine-bail-fast but I'm not sure how it will behave in your before each scenario.

提交回复
热议问题