如何在javascript/node中动态生成测试用例?

Ben*_*enj 21 javascript testing node.js

鼻子测试框架(用于python)支持在运行时动态生成测试用例(以下,从文档中,导致五个不同的测试用例):

def test_evens():
    for i in range(0, 5):
        yield check_even, i, i*3

def check_even(n, nn):
    assert n % 2 == 0 or nn % 2 == 0
Run Code Online (Sandbox Code Playgroud)

如何使用jocha框架(如mocha或qunit)实现此结果?(此时我不依赖于任何特定的框架.)

我的用例是编写一个测试运行器来监视外部服务器上的几个项目.我会提供一个资源URL列表.每个测试都会尝试轮询该资源,并根据它找到的内容返回成功或失败.我有一个用python构建的原型(使用nose)但是如果可以的话我想在node.js中实现.最终,这将包含在CI设置中.

Chr*_*eek 33

是的,您可以使用Mocha动态创建包含案例的测试套件.我已经全局安装了mocha npm install -g mocha,我使用了should.

var should = require('should');

var foo = 'bar';

['nl', 'fr', 'de'].forEach(function(arrElement) {
  describe(arrElement + ' suite', function() {
    it('This thing should behave like this', function(done) {
      foo.should.be.a.String();
      done();
    });
    it('That thing should behave like that', function(done) {
      foo.should.have.length(3);
      done();
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

  • NB!这仅适用于同步动态创建的测试用例.一般情况下,摩卡不支持这一点. (5认同)

rob*_*b3c 24

如果要It()使用异步获取的数据动态创建测试,可以(ab)使用before()带有占位符It()测试的挂钩来确保mocha等待直到before()运行.以下是我对相关问题的回答示例,为方便起见:

before(function () {
    console.log('Let the abuse begin...');
    return promiseFn().
        then(function (testSuite) {
            describe('here are some dynamic It() tests', function () {
                testSuite.specs.forEach(function (spec) {
                    it(spec.description, function () {
                        var actualResult = runMyTest(spec);
                        assert.equal(actualResult, spec.expectedResult);
                    });
                });
            });
        });
});

it('This is a required placeholder to allow before() to work', function () {
    console.log('Mocha should not require this hack IMHO');
});
Run Code Online (Sandbox Code Playgroud)


Tom*_*cer 10

值得注意的是,除了上面接受的答案之外,mocha的文档现在还包含了如何实现这一目标的示例.我为了后代而在下面复制了它.

var assert = require('assert');

function add() {
  return Array.prototype.slice.call(arguments).reduce(function(prev, curr) {
    return prev + curr;
  }, 0);
}

describe('add()', function() {
  var tests = [
    {args: [1, 2],       expected: 3},
    {args: [1, 2, 3],    expected: 6},
    {args: [1, 2, 3, 4], expected: 10}
  ];

  tests.forEach(function(test) {
    it('correctly adds ' + test.args.length + ' args', function() {
      var res = add.apply(null, test.args);
      assert.equal(res, test.expected);
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

  • 这仅适用于同步数据。 (5认同)

Qua*_*ong 9

使用Mocha 1.21.4,您可以通过以下方式在运行时创建套件/测试.

require('chai').should()

Mocha = require 'mocha'
Test = Mocha.Test
Suite = Mocha.Suite


mocha = new Mocha
suite = Suite.create mocha.suite, 'I am a dynamic suite'
suite.addTest new Test 'I am a dynamic test', ->
  true.should.equal true

mocha.run () ->
  console.log("done")
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅https://gist.github.com/cybertk/fff8992e12a7655157ed


Mic*_*rin 5

是的 来自泉龙的绝妙建议!

这是我使用Node的readline模块动态测试生成的示例:

const Mocha = require('mocha');
var Test = Mocha.Test;
var Suite = Mocha.Suite;

var mocha = new Mocha();
var suite = Suite.create(mocha.suite, 'My test suite with dynamic test cases');

lineReader
    .on('line', function (line) {
        suite.addTest(new Test(line, function () {
            return true;
        }));
    })
    .on('close', function () {
        mocha.run();
    });
Run Code Online (Sandbox Code Playgroud)