代码之家  ›  专栏  ›  技术社区  ›  sheodox

实习生功能测试失败后如何清理?

  •  1
  • sheodox  · 技术社区  · 7 年前

    我有一些使用Intern(3)运行的功能测试,测试的最后一步是进行一些清理,包括清除远程浏览器上的localStorage,以及通过立即存储窗口句柄并在测试结束前切换回原始窗口来切换回原始窗口(这样,如果之前的测试在另一个窗口上结束,任何后续测试都不会失败,因为它们试图在关闭的窗口上运行)。但是,如果一些chaijs断言在中失败。then()跳过末尾的清理代码。有没有更好的方法来清理那些即使某些断言失败仍能运行的功能测试?

    this.leadfoot.remote
        .get(require.toUrl(url))
        .execute(function() { return document.querySelector('#welcome').textContent})
        .then(welcome => {
            assert.equal(welcome, 'hello world', 'properly greeted');
        })
        .execute(function() {
            localStorage.clear();
        });
    

    如果断言失败,它将永远不会在最后清除localStorage,如果运行的下一个测试预期localStorage为空,它也将失败。功能测试后有没有更好的清理方法?

    2 回复  |  直到 7 年前
        1
  •  4
  •   jason0x43    7 年前

    使用 afterEach 方法

    afterEach() {
        return this.remote
            .execute(function () {
                localStorage.clear();
            });
    },
    
    'my test'() {
        return this.remote
            .get(...)
            .execute(...)
            .then(welcome => {
                assert.equal(welcome, ...);
            });
    }
    
        2
  •  0
  •   eusebiu    7 年前

    在我们的项目中,我们是这样做的:

    afterEach: function() {
            var command = this.remote;
            if(intern.variables.currentCase && !intern.variables.currentCase.hasPassed) {
                command = command.execute(function () {
                   localStorage.clear();
                });
            }
            return command;
        },
    

    和本地存储。仅当测试失败时,才会执行clear():