我检查了several(real world)BDD examples,但我发现的只是使用硒的e2e测试。我想知道,是否有可能用BDD编写单元测试?如果是这样,这样的单位测试应该如何在小黄瓜中看起来相似?我很难想象要写入功能和场景描述的内容以及如何使用它们来生成文档,例如java collection framework。
修改
我在这里找到了一个例子:http://jonkruger.com/blog/2010/12/13/using-cucumber-for-unit-tests-why-not/comment-page-1/
特点:
Feature: Checkout
Scenario Outline: Checking out individual items
Given that I have not checked anything out
When I check out item
Then the total price should be the of that item
Examples:
| item | unit price |
| "A" | 50 |
| "B" | 30 |
| "C" | 20 |
| "D" | 15 |
Scenario Outline: Checking out multiple items
Given that I have not checked anything out
When I check out
Then the total price should be the of those items
Examples:
| multiple items | expected total price | notes |
| "AAA" | 130 | 3 for 130 |
| "BB" | 45 | 2 for 45 |
| "CCC" | 60 | |
| "DDD" | 45 | |
| "BBB" | 75 | (2 for 45) + 30 |
| "BABBAA" | 205 | order doesn't matter |
| "" | 0 | |
Scenario Outline: Rounding money
When rounding "" to the nearest penny
Then it should round it using midpoint rounding to ""
Examples:
| amount | rounded amount |
| 1 | 1 |
| 1.225 | 1.23 |
| 1.2251 | 1.23 |
| 1.2249 | 1.22 |
| 1.22 | 1.22 |
步骤定义(ruby):
require 'spec_helper'
describe "Given that I have not checked anything out" do
before :each do
@check_out = CheckOut.new
end
[["A", 50], ["B", 30], ["C", 20], ["D", 15]].each do |item, unit_price|
describe "When I check out an invididual item" do
it "The total price should be the unit price of that item" do
@check_out.scan(item)
@check_out.total.should == unit_price
end
end
end
[["AAA", 130], # 3 for 130
["BB", 45], # 2 for 45
["CCC", 60],
["DDD", 45],
["BBB", 75], # (2 for 45) + 30
["BABBAA", 205], # order doesn't matter
["", 0]].each do |items, expected_total_price|
describe "When I check out multiple items" do
it "The total price should be the expected total price of those items" do
individual_items = items.split(//)
individual_items.each { |item| @check_out.scan(item) }
@check_out.total.should == expected_total_price
end
end
end
end
class RoundingTester
include Rounding
end
[[1, 1],
[1.225, 1.23],
[1.2251, 1.23],
[1.2249, 1.22],
[1.22, 1.22]].each do |amount, rounded_amount|
describe "When rounding an amount of money to the nearest penny" do
it "Should round the amount using midpoint rounding" do
RoundingTester.new.round_money(amount).should == rounded_amount
end
end
end
我不知道基于此生成文档的方法。这不是没有希望的,例如将Feature: Checkout
映射到Checkout
类很容易。也许在方法层面上可以做类似的事情。编写特定于此任务的助手的另一种可能的解决方案。
答案 0 :(得分:1)
这里的一个关键想法是理解描述行为和测试之间的区别。在此上下文中描述行为是:
测试往往是:
使用BDD工具时,例如编写单元测试的黄瓜往往最终会得到
的测试因此,我们为不同类型的“测试”提供了不同的工具和不同的技术。通过使用正确的工具来获得合适的工作,您将获得最大的收益。
虽然使用一个工具进行所有测试的想法似乎非常有吸引力。最后,它与使用一个工具来修理汽车一样明智 - 尝试用锤子打开轮胎!