前端的BDD框架?

ajs*_*sie 16 javascript tdd bdd yui qunit

在服务器端,我们有用于BDD开发的Rspec/Cucumber(ruby)vowsjs(node.js)

是否有在Web浏览器上使用的BDD框架(不是qUnit或YUI测试,因为这些只适用于TDD)?

mac*_*thy 13

看看茉莉花

describe("Jasmine", function() {
  it("makes testing JavaScript awesome!", function() {
    expect(yourCode).toBeLotsBetter();
  });
});
Run Code Online (Sandbox Code Playgroud)

http://pivotal.github.com/jasmine/

https://github.com/pivotal/jasmine

应该对红宝石人非常熟悉(原文如此)

  • Jasmine和Mocha都不支持普通英语验收测试.对我来说,这是BDD的重点(而不是TDD).我想就与利益相关者的行为达成一致,然后(不断)对此进行测试,以便所有相关方都对此负责. (10认同)

cre*_*176 10

你也可以看看Yadda.它不是像CucumberJS那样独立的测试框架,而是可以使用其他框架中的Gherkin语法,如Mocha,Jasmine,CasperJS,Zombie,Qunit等.


Omn*_*nce 7

我是第二个茉莉花,也看看茉莉花种

另外值得注意的是Kyuri - 这是一个用于javascript的Gherkin(黄瓜DSL)解析器,最初它针对的是Vows.js,但它也可以生成普通的旧javascript存根(但是,它现在仍然很漂亮).


Kyl*_*ley 6

这是节点wiki上列出的测试框架列表.

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
Run Code Online (Sandbox Code Playgroud)

步骤定义

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();
});
Run Code Online (Sandbox Code Playgroud)


inf*_*rno 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"')
Run Code Online (Sandbox Code Playgroud)

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);
    })
Run Code Online (Sandbox Code Playgroud)

2016年2月16日更新:

经过几个月的 BDD 练习后,我最终得到了基于 txt 的功能描述和 ofc。与小黄瓜。我认为最好在功能描述中写入一些非常高的抽象级别,而不是我之前在 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
Run Code Online (Sandbox Code Playgroud)

这就是我目前喜欢的方式。我使用步骤定义来设置装置和断言的值,但是ofc。如果你愿意的话,你可以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    |
Run Code Online (Sandbox Code Playgroud)

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

    Given I have 1, 2, 3, 6
    When I add these numbers together
    Then I should get 12 as result
Run Code Online (Sandbox Code Playgroud)

也许调试起来更容易,但是您必须为这些值编写解析器,例如拆分“1,2,3,6”字符串并解析这些值以获取数字数组。我认为您可以决定哪种方式更适合您。

高抽象级别的功能描述真正有趣的是,您可以编写多个不同的步骤定义。例如,您可以测试 2 个不同的 api,它们执行相同的操作,或者继续使用计算器示例,您可以为多个用户界面(cli、webapplication 等)编写 e2e 测试,或者您可以编写一个简单的测试,其中仅测试域。无论如何,功能描述或多或少是可重用的。

2016年4月15日更新:

我决定使用Yaddamocha代替Cucumberjasmine。我也喜欢黄瓜和茉莉花,但我认为雅达和摩卡更灵活。