使用不同的数据多次运行相同的mocha测试 [英] Running the same mocha test multiple times with different data

查看:87
本文介绍了使用不同的数据多次运行相同的mocha测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个测试在mocha中做同样的事情。这对我而言,它是重复的,当你希望你的系统可以维持时,这是最糟糕的事情。

I have several tests that do the same thing in mocha. This for me, it's duplication, and is the worst thing to do when you want your system to be maintenable.

var exerciseIsPetitionActive = function (expected, dateNow) {
    var actual = sut.isPetitionActive(dateNow);
    chai.assert.equal(expected, actual);
};

test('test_isPetitionActive_calledWithDateUnderNumSeconds_returnTrue', function () {
    exerciseIsPetitionActive(true, new Date('2013-05-21 13:11:34'));
});

test('test_isPetitionActive_calledWithDateGreaterThanNumSeconds_returnFalse', function () {
    exerciseIsPetitionActive(false, new Date('2013-05-21 13:12:35'));
});



我需要什么



我需要一种只在一个中折叠重复的mocha测试的方法。

What do I need

I need a way of collapsing my duplicated mocha tests in only one.

例如,在PhpUnit(和其他测试框架)中,你有 dataProviders

在phpUnit中,dataProvider以这种方式工作:

For example, in PhpUnit (and other test frameworks) you have dataProviders.
In phpUnit a dataProvider works this way:

<?php class DataTest extends PHPUnit_Framework_TestCase {
    /**
     * @dataProvider provider
     */
    public function testAdd($a, $b, $c)
    {
        $this->assertEquals($c, $a + $b);
    }

    public function provider()
    {
        return array(
          array(0, 0, 0),
          array(0, 1, 1),
          array(1, 0, 1),
          array(1, 1, 3)
        );
    }
}

这里的提供者为测试注入参数,并且测试执行所有情况。非常适合重复测试。

The provider in here injects parameters to the test, and the test executes all the cases. Is perfect for duplicated test.

我想知道在mocha中是否有类似的东西,例如,类似这样的东西:

I want to know if in mocha is there something similar, for example, something like this:

var exerciseIsPetitionActive = function (expected, dateNow) {
    var actual = sut.isPetitionActive(dateNow);
    chai.assert.equal(expected, actual);
};

@usesDataProvider myDataProvider
test('test_isPetitionActive_calledWithParams_returnCorrectAnswer', function (expected, date) {
    exerciseIsPetitionActive(expected, date);
});

var myDataProvider = function() {
  return {
      {true, new Date(..)},
      {false, new Date(...)}
  };
};



我已查看的内容



有一些名为共享行为的tecnique。但它并没有直接用测试套件解决问题,只是解决了具有重复测试的不同组件的问题。

What I have already looked at

There is some tecnique that is called Shared Behaviours . But it does not solve the problem directly with a test suite, it just solve the problem with different components that have duplicated tests.

你知道在mocha中实现dataProviders的任何方法吗?

推荐答案

Mocha没有为此提供工具,但很容易自己做。您只需要在循环内运行测试并使用闭包将数据提供给测试函数:

Mocha doesn't provide a tool for that, but it is easy to do it yourself. You only need to run the tests inside a loop and give the data to the test function using a closure:

suite("my test suite", function () {
    var data = ["foo", "bar", "buzz"];
    var testWithData = function (dataItem) {
        return function () {
            console.log(dataItem);
            //Here do your test.
        };
    };

    data.forEach(function (dataItem) {
        test("data_provider test", testWithData(dataItem));
    });
});

这篇关于使用不同的数据多次运行相同的mocha测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆