【问题标题】:BDD framework for the frontend?前端的 BDD 框架?
【发布时间】:2021-07-14 21:10:10
【问题描述】:

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

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

【问题讨论】:

  • 你也可以看看Yadda。它不是像 CucumberJS 这样的独立测试框架,而是在 Mocha、CasperJS、Qunit 等其他框架中启用 BDD。
  • 将此作为答案而不是评论,我会投赞成票。其他答案要么太面向 TDD(即不支持用于验收测试的小黄瓜语法),要么无法在浏览器中运行(cucumber-js)。

标签: javascript tdd yui bdd qunit


【解决方案1】:

看看茉莉花

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

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

https://github.com/pivotal/jasmine

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

【讨论】:

  • Jasmine 和 Mocha 都不支持纯英语验收测试。对我来说,这就是 BDD(与 TDD 相对)的全部意义所在。我想与利益相关者就行为达成一致,然后(持续)对此进行测试,以便所有相关方都承担责任。
  • @JohnSyrinek 有更好的选择吗?我可以看到你喜欢那里的 Yadda,由 Steve == cressie176 提出,但它仍然建立在其他诸如茉莉花之类的之上(我对这一切都很陌生)。该操作确实提到了黄瓜,这看起来已经是他对 Yadda 所做的选择,所以我认为茉莉花在这里更有意义。此外,黄瓜似乎比 Yadda 或 Kyuri 更稳定(如茉莉花)。
  • @Cawas Yadda 是 IMO 的所在地。使用 Yadda,您可以在步骤定义中使用 TDD 库(Jasmine、Mocha 等)。 Yadda 在这些 TDD 库之上添加了纯英语语言支持。 Cucumber 是一种用于验收测试(Given、When、Then)的特定语言。 Yadda 使用这种开箱即用的方法,但也允许您完全自定义测试解析器以满足您的需求。至于稳定性,Yadda 非常稳定——我从来没有遇到过问题。我在以前的项目中构建了类似 Moonraker 的东西,建议从那里开始。
【解决方案2】:

您也可以查看Yadda。它不是像 CucumberJS 这样的独立测试框架,而是能够使用来自其他框架(如 Mocha、Jasmine、CasperJS、Zombie、Qunit 等)的 Gherkin 类语法。

【讨论】:

  • cressie176 == 史蒂夫。我在这里复制并粘贴了我自己的评论,以便人们可以对其进行投票(如 John Syrinek 所建议的那样)。猜猜结果适得其反!
  • 不错!我会尝试一下。我也喜欢黄瓜,使用断言库或 TDD 框架编写步骤定义会很棒。
  • 总有一天我们会敲掉错误的答案。
【解决方案3】:

我第二个Jasmine,也看看Jasmine-species

另外值得注意的是Kyuri——它是一个用于 javascript 的 Gherkin(Cucumber DSL)解析器,最初它针对 Vows.js,但它也可以生成普通的旧 javascript 存根(但是,它现在仍然有很多错误) .

【讨论】:

  • 最近有人遇到这个问题,请注意 jasmine-species 自 2011 年以来就没有维护过。但不确定它是否仍然有效。
【解决方案4】:

这是节点 wiki 上列出的 testing frameworks 列表。

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();
});

【讨论】:

  • 我一直在研究使用 Karma 和 Mocha 在真实浏览器中运行验收测试。据我所知,Cucumber 无法在浏览器中运行。它对 require() 和其他节点库的使用使这充其量是困难的,甚至可能是不可能的。
【解决方案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 年 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

这就是我目前喜欢的方式。我使用让步骤定义来设置固定装置和断言的值,但是 ofc。如果你愿意,你可以给Examplesgherkin

  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”字符串并解析值以获取数字数组。我认为您可以决定哪种方式更适合您。

高抽象级别的功能描述真正有趣的是,您可以编写多个不同的步骤定义。因此,例如,您可以测试 2 个不同的 api,它们做同样的事情,或者坚持使用计算器示例,您可以为多个用户界面(cli、web 应用程序等)编写 e2e 测试,或者您可以编写一个简单的测试,它仅测试域。无论如何,功能描述或多或少是可重复使用的。

2016 年 4 月 15 日更新:

我决定使用Yaddamocha 而不是Cucumberjasmine。我也喜欢 Cucumber 和 jasmine,但我认为 Yadda 和 mocha 更灵活。

【讨论】:

    【解决方案6】:

    现在有 karma-cucumberjs 可以在真实浏览器和 PhantomJS 中进行 Cucumber 测试。

    https://github.com/s9tpepper/karma-cucumberjs

    【讨论】:

      【解决方案7】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多