首页 文章

我可以通过测试低抽象级代码来使用BDD吗?

提问于
浏览
1

我检查了severalreal worldBDD examples,但我发现的是使用硒的e2e测试 . 我想知道,是否有可能用BDD编写单元测试?如果是这样,这样的单位测试应该如何在小黄瓜中看起来相似?我很难想象要写入功能和场景描述的内容以及如何使用它们来生成文档,例如java collection framework .

edit

我在这里找到了一个例子: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 类很容易 . 也许在方法层面上可以做类似的事情 . 编写特定于此任务的助手的另一种可能的解决方案 .

1 回答

  • 1

    这里的一个关键想法是理解描述行为和测试之间的区别 . 在此上下文中描述行为是:

    • 更抽象

    • 易于被更广泛的读者阅读

    • 更专注于你在做什么以及你为什么这样做

    • 少关注'how'你正在做点什么

    • 不那么详尽,我们使用的例子,我们不涵盖一切

    测试往往是:

    • 精确

    • 详细

    • 详尽无遗

    • 技术

    使用BDD工具时,例如编写单元测试的黄瓜往往最终会得到测试

    • 详细

    • 充满技术细节,只有少数人可以欣赏

    • 非常昂贵的运行

    • 难以维持

    因此,我们为不同类型的“测试”提供了不同的工具和不同的技术 . 通过使用正确的工具来获得合适的工作,您将获得最大的收益 .

    虽然使用一个工具进行所有测试的想法似乎非常有吸引力 . 最后,它与使用一个工具来修理你的汽车一样明智 - 试着用锤子给你的轮胎加油!

相关问题