【问题标题】:Dynamically create Mocha Test Suites动态创建 Mocha 测试套件
【发布时间】:2020-07-29 18:23:48
【问题描述】:

我正在尝试实现一个应用程序,它将测试配置/案例作为输入 (yaml/json) 文件并执行一堆断言。

case:
 - operation: fetch_user
  args:
  - name: id
    value: 1
 - checks:
   - attribute: username
     expected_value: user1
   - attribute: email
     expected_value: user1@test.com    

我想编写通用测试块 (it()) 来动态执行断言。

我试过Does mocha/supertest create express server for each test suite?

我在 index.js 中获取用户信息,并且在 assertion.js 文件中有断言。

index.js

//read yaml file

const body = userService.get(userId);

body.then(userResponse => {
    const suite = createTestSuite("Test", checks, userResponse);
    suite.run();
});

assertion.js


function createTestSuite(name, checks, userResponse) {
    let suite = new Suite("Moch test");
    suite.addTest(new Test("Check Username", () => {
        expect(userResponse.username).to.equal(checks.expected_username);
    }));

    return suite;
}

【问题讨论】:

    标签: javascript testing automated-tests mocha.js


    【解决方案1】:

    --delay 选项应该会有所帮助 (docs)。

    尝试类似(未经测试):

    // test.js
    // run with `mocha --delay test.js`
    
    // getData will return a Promise which fulfills with an array of test data
    getData().then(testData => {
      describe('mocha tests', function() {
        testData.forEach(data => {
          it('should do something with data', function() {
            // assertion here
          });
        });
      });
    
      run(); // this is a global which starts the test run
    });
    
    

    --delay 选项会导致测试运行等待,直到您调用 run(),这允许您执行异步操作(例如,收集一些数据)之前定义测试。

    Mocha 目前不支持异步套件,这使得从异步数据源动态生成测试变得很尴尬。

    【讨论】:

      猜你喜欢
      • 2016-10-09
      • 2013-06-30
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2020-06-25
      • 2017-06-08
      相关资源
      最近更新 更多