美文网首页
BDD(Behavior Driven Development,

BDD(Behavior Driven Development,

作者: Feng_001 | 来源:发表于2020-01-03 15:12 被阅读0次

什么是BDD?

行为驱动开发(BDD)是从测试驱动开发(TDD)发展而来的一种软件开发方法。它的不同之处在于它是用一种共享的DSL(Domain Specified Language)语言编写的,这可以改善技术团队和非技术团队以及利益相关者之间的沟通。在这两种开发方法中,测试都是在代码之前编写的,但是在BDD中,测试更加以用户为中心,并且基于系统的行为。

BDD 特点

  1. 关注的是业务领域,而不是技术:BDD强调用领域特定语言(DSL, domain specific language)描述用户行为,定义业务需求,而不会关心系统的技术实现。
  2. 不是工具,强调的是一种协作方式:BDD要求各个角色共同参与系统行为的挖掘和定义,以实现对业务价值的一致理解。
  3. 不是关于测试的:BDD源自TDD,又不同于TDD,重点不是关于测试的,但可以指导更好的做自动化测试。
  4. 全栈敏捷方法:BDD促使团队所有角色从需求到最后的测试验证,进行高度的协作和沟通,以交付最有价值的功能。

使用BDD的一些好处

如果您计划实现BDD,这里有几点将有利于软件团队。
1、你不再定义“测试”,而是定义“行为”。
2、开发人员、测试人员和产品所有者之间更好的沟通。
3、因为BDD是用简单的语言来解释的,所以学习曲线会短得多。
4、由于它本质上是非技术性的,所以可以接触到更广泛的受众。
5、行为方法定义了开发之前的验收标准。

BDD的缺点

即使是最好的开发方法也会有陷阱,BDD也不例外。其中一些是:
1、要在BDD工作,必须有TDD的经验。
2、BDD与瀑布方法不兼容。
3、如果没有正确地指定需求,BDD可能无效。
4、使用BDD的测试人员需要具备足够的技术技能。

使用BDD

可以使用小黄瓜(Cucumber)DSL语言创建测试
given (some context)
when (something happens)
then (outcome)

Cucumber实践

Cucumber是一个支持BDD(Behavior Driven Development),即行为驱动开发的自动化测试框架。在进行单元测试或者集成测试之前,事先将测试的步骤和验证信息用通用的语言(英语)定义好,使得测试的步骤、单元测试和集成测试每一步执行的目的能被非开发人员读懂,并且写单元测试和集成测试的人员可以依据事先写好的框架进行代码的编写,达到行为驱动开发的目的。

创建项目

1、使用MVN 命令行创建项目

mvn archetype:generate                      \
   "-DarchetypeGroupId=io.cucumber"           \
   "-DarchetypeArtifactId=cucumber-archetype" \
   "-DarchetypeVersion=4.8.0.0"               \
   "-DgroupId=hellocucumber"                  \
   "-DartifactId=hellocucumber"               \
   "-Dpackage=hellocucumber"                  \
   "-Dversion=1.0.0-SNAPSHOT"                 \
   "-DinteractiveMode=false"

2、IDE创建项目,添加POM依赖

<properties>
        <cucumber.version>4.8.0</cucumber.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
Cucumber 启动文件 RunCucumberTest

该项目全部放在test-> java下面。
1、创建 hellocucumber 包
下面两个文件
RunCucumberTest.java
Stepdefs.java
2、test-> resources 下面创建文件夹hellocucumber
一个文件
is_it_friday_yet.feature

package hellocucumber;

import io.cucumber.junit.CucumberOptions;
import io.cucumber.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty"})
public class RunCucumberTest {
}

Feature 文件 resources 下对应包hellocucumber 创建 is_it_friday_yet.feature 文件
Feature: Is it Friday yet?
  Everybody wants to know when it's Friday

  Scenario: Sunday isn't Friday
    Given today is Sunday
    When I ask whether it's Friday yet
    Then I should be told "Nope"

  Scenario: Friday is Friday
    Given today is Friday
    When I ask whether it's Friday yet
    Then I should be told "TGIF"

打开Terminal 执行 mvn test

会提示


image.png
增加实现类包含测试方法和测试内容

class IsItFriday {
    static String isItFriday(String today) {
        return "Friday".equals(today) ? "TGIF" : "Nope";
    }
}

public class Stepdefs {
    private String today;
    private String actualAnswer;

    @Given("today is Sunday")
    public void today_is_Sunday() {
        today = "Sunday";
    }

    @When("I ask whether it's Friday yet")
    public void i_ask_whether_it_s_Friday_yet() {
        actualAnswer = IsItFriday.isItFriday(today);
    }

    @Then("I should be told {string}")
    public void i_should_be_told(String expectedAnswer) {
        assertEquals(expectedAnswer, actualAnswer);
    }

    @Given("today is Friday")
    public void today_is_Friday() {
        today = "Friday";
    }


}

mvn test

image.png

以上我们使用BDD 的方法对 IsItFriday.isItFriday(today); 做了两个场景测试!

参考资料

推荐

相关文章

网友评论

      本文标题:BDD(Behavior Driven Development,

      本文链接:https://www.haomeiwen.com/subject/hymzoctx.html