【问题标题】:mocha conditional data driven testingmocha 条件数据驱动测试
【发布时间】:2021-04-08 10:54:19
【问题描述】:

我想在 mocha 中多次执行相同的测试用例,执行条件的数量将在测试代码块之前设置。我该如何实现。我的示例代码是

describe('describe block ',function() {
    var a;
    
    before('before hook', function () {
        a =[1,2,3,4];
        console.log('outside before '+allorders);
    })

    a.forEach(()=>{
       it("Check it", function () {
          console.log('HI');
       });
    });

});

【问题讨论】:

  • 为什么要多次运行相同的东西?测试逻辑是否取决于数组值?另外请添加您的代码当前存在的问题。
  • 要求运行测试用例作为网页中存在的行数。
  • 这不是我所有 2 个问题和 1 个请求的答案

标签: javascript mocha.js webdriver-io


【解决方案1】:

现在我要做的是将我的测试包装到一个函数中,然后使用输入运行该测试。一个非常简单的示例如下所示

const { expect } = require("chai")

const myTest = (input) => {
  it('will do something with the input', () => {
    expect(input).to.be.true;
  });
}


describe('all my tests', () => {
  [true,false,true].forEach(val => {
    myTest(val)
  })
});

如果您想在 before 块中设置数组,则如下所示

const { expect } = require("chai");

let testCases;

const myTest = (input) => {
  it('will do something with the input', () => {
    expect(input).to.be.true;
  });
}


describe('all my tests', () => {
  before(() => {
    testCases = [true, false, true];
  });

  testCases.forEach(val => {
    myTest(val)
  })
});

在这种情况下,测试将运行 3 次并检查输入是否为真。所以它会在第一次和最后一次通过并在第二次失败。这是一个非常微不足道的例子,但我认为它阐明了如何完成你想做的事情

【讨论】:

  • 我的场景是运行时间设置的数组值。这对我不起作用
  • 您也可以在 before 块中轻松执行此操作。我将编辑答案以使其更易于阅读。希望这就是您的意思,否则您可能需要详细说明您的问题
  • 出现以下错误:TypeError: Cannot read property 'forEach' of undefined for testCases。你能从你这边试试吗
  • 啊,是的。我看到了我的错误。当我回到电脑时会更新答案
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多