如何在 javascript/node 中动态生成测试用例? [英] How can I dynamically generate test cases in javascript/node?

查看:30
本文介绍了如何在 javascript/node 中动态生成测试用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

nose 测试框架(用于 Python)支持在运行时动态生成测试用例(以下来自文档,产生五个不同的测试用例):

The nose testing framework (for python) supports dynamically generating test cases at run-time (the following, from the documentation, results in five distinct test cases):

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

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

How can I achieve this result using javascript frameworks such as mocha or qunit? (I am not attached to any particular framework at this point.)

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

My use-case is writing a test runner to monitor several items on an external server. I would provide a list of resource URLs. Each test attempts to poll that resource and returns success or failure depending on what it finds. I have a prototype built in python (using nose) but would like to implement in node.js if I can. Eventually, this would be included in a CI setup.

推荐答案

是的,您可以使用 摩卡.我已经全局安装了 mocha npm install -g mocha 并且我使用了 should.

Yes you can dynamically created test suites with cases using Mocha. I have installed mocha globally npm install -g mocha and I use 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();
    });
  });
});

这篇关于如何在 javascript/node 中动态生成测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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