[英]How to create a data driven test in Node.js
In Node.js
unit tests, what is the way to create data driven unit tests
? 在Node.js
单元测试中,创建数据驱动unit tests
的方法是什么?
For Example, I've a common function / method
, which I want to re-use in multiple unit tests with different sets of data. 例如,我有一个常见的function / method
,我希望在具有不同数据集的多个单元测试中重用它。 I tried looking into nodeunit, vows, whiskey, qunit, expresso
; 我试着查看nodeunit, vows, whiskey, qunit, expresso
; But I wasn't able to figure out a way to achieve this functionality. 但我无法找到实现此功能的方法。
I was not looking at calling the function literally in multiple tests, but rather use the common method
in a loop to pick up the data in each iteration and execute it, as a unittest
我没有看在多个测试字面上调用函数,而是使用通用的method
在一个循环拿起数据在每次迭代中并执行它,作为一个unittest
The reason for this is, I've atleast 1000 rows of parameterized data, for which I want to write unittest
. 原因是,我至少有1000行参数化数据,我想编写unittest
。 Obviously I cannot go on writing 1000 unittests
physically. 很显然,我不能写去千个unittests
物理。
Anyone could you please point me a way to achieve the above. 有人可以请你指点一下实现上述目标。
There is qunit addon which allows to run parameterized quint tests 有qunit插件允许运行参数化的quint测试
https://github.com/AStepaniuk/qunit-parameterize https://github.com/AStepaniuk/qunit-parameterize
So you can separate test data and test method and run the same test method against different data sets. 因此,您可以分离测试数据和测试方法,并针对不同的数据集运行相同的测试方法。
This is a pretty old post, but I just hit this problem myself and wasn't able to find a clean solution for QUnit without using the plugin referenced by the other comment (qunit-parameterize). 这是一篇非常古老的帖子,但我自己就遇到了这个问题,并且在没有使用其他评论(qunit-parameterize)引用的插件的情况下无法为QUnit找到一个干净的解决方案。 Honestly, I couldn't figure out how to integrate the plugin with my company's project and gave up after about an hour. 老实说,我无法弄清楚如何将插件与我公司的项目集成,并在大约一个小时后就放弃了。
This is how I ended up solving it: 这就是我最终解决它的方法:
Just define an array with your inputs (and expected outputs, if needed), iterate over your array, and define the QUnit test in the callback! 只需使用您的输入(以及预期的输出,如果需要)定义一个数组,迭代您的数组,并在回调中定义QUnit测试! Super simple, really, but worked quite well. 超级简单,真的,但工作得很好。
const testCases = [
{ input: "01/01/2015", expected: "2015-01-01" },
{ input: "09/25/2015", expected: "2015-09-01" },
{ input: "12/31/2015", expected: "2015-12-01" }
];
testCases.forEach(testCase => {
QUnit.test("gets first of month",
() => {
const actual = new classUnderTest().getFirstOfMonth(testCase.input);
strictEqual(actual, testCase.expected);
});
});
I wasn't sure that QUnit would discover the test if it were nested as such, but it does just fine. 我不确定QUnit是否会发现测试是否嵌套,但它确实很好。
Enjoy! 请享用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.