在服务器端,我们有用于 BDD 开发的 Rspec/Cucumber (ruby) vowsjs (node.js)
是否有可在 Web 浏览器上使用的 BDD 框架(不是 qUnit 或 YUI 测试,因为它们仅用于 TDD)?
在服务器端,我们有用于 BDD 开发的 Rspec/Cucumber (ruby) vowsjs (node.js)
是否有可在 Web 浏览器上使用的 BDD 框架(不是 qUnit 或 YUI 测试,因为它们仅用于 TDD)?
看看茉莉花
describe("Jasmine", function() {
it("makes testing JavaScript awesome!", function() {
expect(yourCode).toBeLotsBetter();
});
});
http://pivotal.github.com/jasmine/
https://github.com/pivotal/jasmine
应该是(原文如此)对红宝石人来说非常熟悉
你也可以看看雅达。它不是像 CucumberJS 这样的独立测试框架,而是能够使用来自其他框架(如 Mocha、Jasmine、CasperJS、Zombie、Qunit 等)的 Gherkin 类语法。
这是节点 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
步骤定义
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();
});
我认为 jasmine 只是一个 TDD 框架,而不是 BDD,因为它没有 BDD 框架所具有的两层抽象:
但没关系,这是一个很好的起点。我也不喜欢重新发明轮子(使用基于 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。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”字符串并解析值以获取数字数组。我认为您可以决定哪种方式更适合您。
高抽象级别的特征描述真正有趣的是,您可以编写多个不同的步骤定义。因此,例如,您可以测试 2 个不同的 api,它们做同样的事情,或者坚持使用计算器示例,您可以为多个用户界面(cli、web 应用程序等)编写 e2e 测试,或者您可以编写一个简单的测试,这仅测试域。无论如何,功能描述或多或少是可重用的。
2016 年 4 月 15 日更新:
我决定用Yadda和mocha而不是Cucumber和jasmine。我也喜欢 Cucumber 和 jasmine,但我认为 Yadda 和 mocha 更灵活。
现在有 karma-cucumberjs 可以在真实浏览器和 PhantomJS 中进行 Cucumber 测试。
我可以推荐使用 WebdriverIO + CucumberJS 的 BDD 方法,如果您使用 Jira,您可以通过 Jira Xray 管理您的测试,也可以尝试使用 Sauce Labs 进行云测试。