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

Cucumber 我可以通过测试低抽象级别的代码来使用BDD吗?,cucumber,bdd,gherkin,Cucumber,Bdd,Gherkin,我检查了(),但我发现的都是使用selenium的e2e测试。我想知道,是否可以用BDD编写单元测试?如果是这样的话,那么这种单元测试在小黄瓜中应该是什么样的呢?我很难想象在特性和场景描述中写些什么,以及如何使用它们生成文档,例如由 编辑 我在这里找到了一个例子: 特点: Feature: Checkout Scenario Outline: Checking out individual items Given that I have not checked anything o

我检查了(),但我发现的都是使用selenium的e2e测试。我想知道,是否可以用BDD编写单元测试?如果是这样的话,那么这种单元测试在小黄瓜中应该是什么样的呢?我很难想象在特性和场景描述中写些什么,以及如何使用它们生成文档,例如由

编辑

我在这里找到了一个例子:

特点:

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):


我不知道基于此生成文档的方法。这不是没有希望的,例如,很容易将
功能:Checkout
映射到
Checkout
类。也许在方法层面上也可以做类似的事情。编写特定于此任务的帮助程序的另一种可能的解决方案。

这里的一个关键思想是理解描述行为和测试之间的区别。在这种情况下,描述行为是:

  • 更抽象
  • 更广泛的读者易于阅读
  • 更加关注你在做什么以及为什么要做
  • 较少关注“如何”做某事
  • 不太详尽,我们使用示例,我们没有涵盖所有内容
测试往往是:

  • 精确的
  • 详细的
  • 详尽的
  • 技术的
当您使用BDD工具(例如Cucumber)来编写单元测试时,您往往最终得到的测试是

  • 冗长的
  • 充满了只有少数人能欣赏的技术细节
  • 运行非常昂贵
  • 难以维护
  • 因此,我们有不同的工具和不同的技术来进行不同类型的“测试”。通过为正确的工作使用正确的工具,你可以获得最大的回报


    虽然使用一种工具进行所有测试的想法似乎非常吸引人。最后,用一种工具来修理你的汽车是明智的——试着用锤子给轮胎打气

    哪种语言?java?@maboiteaspam我认为语言在这里并不重要,我只想知道如何将小黄瓜上描述的特性映射到API在任何oo语言上拥有的类/接口和方法。我想特性和场景描述应该包含这些信息,但我认为接口、类和方法名称是实现细节,所以小黄瓜代码不应该包含它们,它们应该只在步骤定义中。也许我错了,这就是为什么我对一个真实世界的例子感兴趣。事实上,我建议你检查一下mocha框架,因为它可以轻松地提供接口和输出文档。对于链接,对不起,这里是@maboiteaspam,我不明白它与主题有什么关系,它是一个javascript单元测试框架,我知道,我更喜欢jasmine。但它们并不完全支持BDD,它们只有类似BDD的断言,仅此而已。Cucumber有特性文件和步骤文件,而这些测试框架只有一个包含混合内容的spec文件。顺便说一句,我不是在寻找一个测试框架,我是在寻找一个通过测试低抽象级别代码来使用gherking的示例。我不知道为什么你不能。我不想让cucumber的开销出现在我的单元测试上,我认为这里的关键字是抽象的。例如,通过更改密码:
    “如果我在更改密码时以客户身份登录,那么我可以使用新密码而不是旧密码登录”
    ,这是非常抽象的,但您可以通过测试移动页面、桌面页面、rest api、业务逻辑、,或者甚至可能只是通过使用不同世界的步骤来访问数据。所以我认为如果你的BDD测试充满了技术细节,那么你就做错了。(这只是我个人的看法。)
    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