【问题标题】:Mocha run after all tests in all classes runMocha 在所有类中的所有测试运行后运行
【发布时间】:2015-01-01 21:27:40
【问题描述】:

所以我正在使用 mocha 并且我有超过 10 个 JS 类的测试。我已经构建了一个日志变量,它在这 10 个类的整个测试运行中都存在。现在我想做的是在最后一次测试运行之后,我想将此日志写入文件。

所以我做的第一件事就是在每个班级都写这样的东西

test.afterEach(function() {
    currentTest = this.currentTest;

    if(currentTest.state === 'failed') {
        common.takeScreenshot(driver, common.getTestName() + " - Failed.jpg");
    }

    common.writerLoggerToFile(this.currentTest.title+ ".json");

    driver.quit();
});

这将为每个测试创建一个文件。我知道我可以做 test.after 将它减少到每个班级一个文件。我在最后一个创建的文件中注意到的东西包含来自所有测试的所有数据。所以我希望它是一个文件。

注意:这些文件将数据存储在 json 中,所以我不能简单地附加文件。如果 mocha 有解决方案或者我需要构建一个解决方案,有什么想法吗?谢谢

【问题讨论】:

  • 您能否添加一个示例来说明问题中记录的内容?
  • 这里是一些相关的官方文档:mochajs.org/#root-hook-plugins - 在串行运行测试时可以使用根钩子来完成

标签: node.js mocha.js


【解决方案1】:

我最近遇到了类似的问题。我已经通过嵌套套件解决了这个问题。

这是我的根套件:

import utils from './utils'

describe('ROOT SUITE', function () {

  before(async function(){    
    await utils.before()    
    console.log('ROOT BEFORE')
  })

  // Require specs to consume
  require('require-dir')('./specs')

  after(async function(){
    await utils.after()
    console.log('ROOT AFTER')
  })  
})

这是我的套房之一:

import utils from '../utils'

describe('General tests', function() {
  
  let app = null
  
  before(function(){
    app = utils.app
    console.log('local before')
  })
  after(function(){
    console.log('local after')
  })

  beforeEach(function(){
    console.log('local beforeEach')
  })
  afterEach(function(){
    console.log('local afterEach')
  })

  it('shows the proper application title', function () {
    return app.client.getTitle()
      .then(title => {
        expect(title).to.equal('electron-vue-testing-project')
      })
  }),
  it('is true', function () {
    return expect(true).to.be.true
  })
})

【讨论】:

    【解决方案2】:

    考虑使用jsonjson-stream 报告器。

    mocha -R json >> your-file.json
    

    【讨论】:

    • 没有问题是没有日志的变量定义。这将产生我 console.log 的一切
    【解决方案3】:

    您还可以将测试包装到一个套件中,在根套件中您可以初始化数据库连接等必要的东西。然后,您可以使用global variable 在套件之间传递必要的信息。

    测试套件 1 (suite1.test.js):

    describe("Root of Suite 1", function () {
    
        describe("Function of Suite 1", function () {
            it("should do ..", async function () {
                ...
            });
        });
    });
    

    测试套件 2 (suite2.test.js):

    describe("Root of Suite 2", function () {
    
        // This will inform root suite about the completion of tests
        after(function () {
            global.isTestsCompleted = true;
        });
    
        describe("Function of Suite 2", function () {
            it("should do ..", async function () {
                ...
            });
        });
    });
    

    根套件(root.test.js):

    const { isReady } = require("../db");
    
    describe("Root Suite", function () {
        this.timeout(60000);
    
        // Wait until the DB connection is ready
        before(function (done) {
            isReady().then(() => done());
            global.isTestsCompleted = false;
        });
    
        // Check the variable every 500ms to see if all test suites executed
        after(function () {
           setInterval(() => {
               if (global.isTestsCompleted) {
                   process.exit(1);
               }
           }, 500); 
        });
    
        require("./suite1.test.js");
        require("./suite2.test.js");
    });
    

    在上面,套件的顺序很重要。它们将按照我们导入它们的顺序执行。因此,我们将控制机制置于最后一个套件 suite2.test.js

    更新 还有一个内部选项可以在所有测试运行后终止 mocha 进程:mocha --exit。 因此,在这种情况下,我们不需要使用全局变量,根套件如下所示:

    const { isReady } = require("../db");
    
    describe("Root Suite", function () {
        this.timeout(60000);
    
        // Wait until the DB connection is ready
        before(function (done) {
            isReady().then(() => done());
        });
    
        require("./suite1.test.js");
        require("./suite2.test.js");
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-26
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      相关资源
      最近更新 更多