【发布时间】:2022-03-17 20:19:04
【问题描述】:
我检查了several (real world) BDD examples,但我发现的只是使用 selenium 的 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 |
步骤定义(红宝石):
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 类很容易。也许可以在方法级别上做类似的事情。编写特定于此任务的帮助器的另一种可能的解决方案。
【问题讨论】:
-
哪种语言?爪哇?
-
@maboiteaspam 我认为语言在这里并不重要,我只想知道如何将 gherkin 上描述的特性映射到 API 在任何 oo 语言上的类/接口和方法。我猜功能和场景描述应该包含这些信息,但我认为接口、类和方法名称是实现细节,所以小黄瓜代码不应该包含它们,它们应该只在步骤定义中。也许我错了,这就是我对现实世界的例子感兴趣的原因。
-
事实上我想建议你检查一下 mocha 框架,因为它提供了界面和输出文档很容易。忘记链接对不起,它在这里mochajs.org
-
@maboiteaspam 不明白跟题目有什么关系,是个javascript单元测试框架,我知道,我更喜欢jasmine。但是它们并不完全支持 BDD,它们只有 BDD 之类的断言,仅此而已。 Cucumber 有功能和步骤文件,而这些测试框架只有一个包含混合内容的规范文件。顺便提一句。我不是在寻找测试框架,而是通过测试低抽象级别的代码来寻找 gherking 的示例用法。
-
我不明白你为什么不能。我不希望在我的单元测试中增加黄瓜的开销。