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

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

问题描述

鼻子测试框架(用于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

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

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构建的原型(使用nose)但是如果可以的话我想在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 我使用应该

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天全站免登陆