前端的BDD框架是什么?

18

在服务器端,我们使用 Rspec/Cucumber 进行 BDD 开发 (ruby),vowsjs (node.js)

是否有可用于 Web 浏览器的 BDD 框架(不包括 qUnit 或 YUI test,因为这些只适用于 TDD)?


5
您也可以查看Yadda。与CucumberJS这样的独立测试框架不同,它使得BDD在其他框架中变得更容易,比如Mocha、CasperJS和Qunit等。 - Steve
把这个变成答案而不是评论,我会投票支持它。其他的答案要么过于TDD导向(即不支持gherkin语法进行验收测试),要么无法在浏览器中运行(cucumber-js)。 - Johntron
7个回答

13

11
Jasmine和Mocha都不支持纯英语的验收测试。对我来说,这是BDD(与TDD相对)的全部意义所在。我希望与利益相关者就行为达成一致,然后(持续地)根据此进行测试,以便所有相关方都能够承担责任。 - Johntron
1
@JohnSyrinek有更好的选择吗?我可以看到你喜欢Yadda,它是由Steve == cressie176提出的,但它仍然是建立在其他框架上如jasmine(我对所有这些都很陌生)。原贴确实提到了cucumber,它似乎已经成为他所选用的替代品了,所以我认为在这里使用jasmine更加合适。此外,cucumber似乎比Yadda或Kyuri更加稳定(就像jasmine一样)。 - cregox
@Cawas Yadda 是我认为最好的选择。使用 Yadda,您可以在步骤定义中使用 TDD 库(Jasmine、Mocha 等)。Yadda 在这些 TDD 库之上添加了对纯英语语言的支持。Cucumber 是一种特定的验收测试语言(Given、When、Then)。Yadda 可以直接使用它,但也允许您完全自定义测试解析器以满足您的需求。至于稳定性,Yadda 非常稳定 - 我从未遇到过问题。我在以前的项目中构建了类似 Moonraker 的东西,并建议从那里开始。 - Johntron

11

您还可以查看Yadda。它不像CucumberJS那样是一个独立的测试框架,而是使您能够从其他框架(如Mocha、Jasmine、CasperJS、Zombie、Qunit等)中使用类似Gherkin的语法。


1
cressie176 == Steve。我将自己的评论复制并粘贴到这里,以便人们可以对其进行投票(正如John Syrinek建议的那样)。猜想那个计划失败了! - cressie176
不错!我会试一下的。我也喜欢黄瓜,但使用断言库或TDD框架编写步骤定义会更好。 - inf3rno
总有一天我们会把错误的答案排除掉。 - Lance Kind

7

我同意Jasmine的观点,也建议看看Jasmine-species

另外值得注意的是Kyuri——它是一个用于javascript的Gherkin(类似Cucumber的DSL)解析器,最初它针对Vows.js,但它也可以生成普通的javascript桩代码(然而,它现在仍然有些bug)。


最近遇到这个问题的人请注意,jasmine-species自2011年以来就没有得到维护。不确定它是否仍然可用。 - user234932

6
这是节点维基上列出的测试框架列表。 cucumber-js看起来很有前途。以下是语法示例: 特性源
Feature: Simple maths
  In order to do maths
  As a developer
  I want to increment variables

  Scenario: Increment variable once
    Given a variable set to 1
    When I increment the variable by 1
    Then the variable should contain 2

步骤定义

var variable;

Given(/^a variable set to (\d+)$/, function(number, callback) {
  variable = parseInt(number);
  callback();
});

When(/^I increment the variable by (\d+)$/, function(number, callback) {
  variable += parseInt(number);
  callback();
});

Then(/^the variable should contain (\d+)$/, function(number, callback) {
  if (variable != parseInt(number))
    throw(new Error('Variable should contain '+number+' but it contains '+variable+'.'));
  callback();
});

1
我一直在研究如何使用Karma和Mocha在真实浏览器中运行验收测试。据我所知,Cucumber无法在浏览器中运行。它对require()和其他node库的使用使得这种操作最多也很困难,甚至可能是不可能的。 - Johntron

5
我认为Jasmine只是一个TDD框架,而不是BDD框架,因为它没有BDD框架所具备的两层抽象:

  1. 我们要做什么?(通常在txt文件中)
  2. 我们如何做到这一点?(JavaScript中可重用的实现)

但没关系,这是一个很好的起点。我也不喜欢重新发明轮子(使用基于txt的语言)。我找到了一个建立在Jasmine之上的BDD框架,对我来说这是完美的解决方案:https://github.com/DealerDotCom/karma-jasmine-cucumber

例如:

specs.js(我们要做什么)

feature('Calculator: add')
    .scenario('should be able to add 2 numbers together')
        .when('I enter "1"')
        .and('I add "2"')
        .then('I should get "3"')
    .scenario('should be able to add to a result of a previous addition')
        .given('I added "1" and "2"')
        .when('I add "3"')
        .then('I should get "6"')

steps.js(我们如何做到的)

featureSteps('Calculator:')
    .before(function(){
        this.values = [];
        this.total = null;
    })
    .given('I added "(.*)" and "(.*)"', function(first, second){
        this.when('I enter "' + first + '"');
        this.when('I add "' + second + '"');
    })
    .when('I enter "(.*)"', function(val){
        this.values.push(val * 1);
    })
    .when('I add "(.*)"', function(val){
        this.values.push(val * 1);
        this.total = this.values[0] + this.values[1];
        this.values = [this.total];
    })
    .then('I should get "(.*)"', function(val){
        expect(this.total).toBe(val * 1);
    })

更新 2016-02-16:

经过几个月的行为驱动开发实践,我最终以基于文本的特性描述和 Gherkin 为结果。我认为,将高度抽象的内容写入特性描述中要比之前在我的 karma-jasmine-cucumber 示例中写的更好。根据我的旧例子,现在我宁愿写出类似这样的内容:

  Scenario: Addition of numbers
    Given I have multiple numbers
    When I add these numbers together
    Then I should get their sum as result

这是我目前喜欢的方式。我曾经让步骤定义来设置夹具的值和断言,但当然,如果你愿意,你可以使用gherkin来提供Examples

  Scenario: Addition of numbers
    Given I have <multiple numbers>
    When I add these numbers together
    Then I should get <their sum> as result

    Examples:
        | multiple numbers | their sum |
        |    1, 2, 3, 6    |     12    |
        |    8, 5          |     13    |
        |    5, -10, 32    |     27    |

Cucumber 将这3行转换为3个场景,例如:

    Given I have 1, 2, 3, 6
    When I add these numbers together
    Then I should get 12 as result

也许调试起来更容易,但是你需要编写解析器来处理这些值,例如拆分“1, 2, 3, 6”字符串并将其解析为数字数组。我认为你可以决定哪种方式更适合你。
高抽象级别的特性描述真正有趣的地方在于,你可以编写多个不同的步骤定义。所以,例如,你可以测试两个不同的API,它们做相同的事情,或者坚持使用计算器示例,你可以为多个用户界面(cli、web应用程序等)编写端到端测试,或者你可以编写一个简单的测试,仅测试域。无论如何,特性描述都是可重用的。
更新2016-04-15:
我决定使用Yaddamocha而不是Cucumberjasmine。我也喜欢Cucumber和jasmine,但我认为Yadda和mocha更灵活。

1

1
我可以推荐使用WebdriverIO + CucumberJS的BDD方法,如果您使用Jira,您可以通过Jira Xray管理测试需求,并尝试使用Sauce Labs进行云测试。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接