BDD Cucumber 实战
阅读原文时间:2023年07月11日阅读:1

cucumber

cucumber 是一个用于执行 BDD 的自动化测试工具。

用户指南

  1. 创建 Spring Boot 项目并引入依赖


    http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    org.springframework.boot spring-boot-starter-parent 2.1.1.RELEASE
    org.zxd
    behavior-driven-development-in-action
    1.0.0
    behavior-driven-development-in-action
    behavior-driven-development-in-action

    <properties>
        <java.version>1.8</java.version>
        <cucumber.version>4.2.0</cucumber.version>
    </properties>
    
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>27.0-jre</version>
        </dependency>
    &lt;dependency&gt;
        &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
        &lt;artifactId&gt;lombok&lt;/artifactId&gt;
        &lt;optional&gt;true&lt;/optional&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/commons-io/commons-io --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;commons-io&lt;/groupId&gt;
        &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
        &lt;version&gt;2.6&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
        &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
        &lt;version&gt;3.8.1&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
        &lt;artifactId&gt;commons-collections4&lt;/artifactId&gt;
        &lt;version&gt;4.2&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;io.cucumber&lt;/groupId&gt;
        &lt;artifactId&gt;cucumber-java&lt;/artifactId&gt;
        &lt;version&gt;${cucumber.version}&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;io.cucumber&lt;/groupId&gt;
        &lt;artifactId&gt;cucumber-core&lt;/artifactId&gt;
        &lt;version&gt;${cucumber.version}&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;io.cucumber&lt;/groupId&gt;
        &lt;artifactId&gt;cucumber-junit&lt;/artifactId&gt;
        &lt;version&gt;${cucumber.version}&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;io.cucumber&lt;/groupId&gt;
        &lt;artifactId&gt;cucumber-spring&lt;/artifactId&gt;
        &lt;version&gt;${cucumber.version}&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
        &lt;plugin&gt;
            &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
            &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
            &lt;configuration&gt;
                &lt;source&gt;1.8&lt;/source&gt;
                &lt;target&gt;1.8&lt;/target&gt;
            &lt;/configuration&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;phase&gt;integration-test&lt;/phase&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;java&lt;/goal&gt;
                    &lt;/goals&gt;
                    &lt;configuration&gt;
                        &lt;classpathScope&gt;test&lt;/classpathScope&gt;
                        &lt;mainClass&gt;org.zxd.bdd.cucumber.CucumberTest.java&lt;/mainClass&gt;
                        &lt;arguments&gt;
                            &lt;argument&gt;--plugin&lt;/argument&gt;
                            &lt;argument&gt;pretty&lt;/argument&gt;
                            &lt;argument&gt;--glue&lt;/argument&gt;
                            &lt;argument&gt;src/test/resources/&lt;/argument&gt;
                        &lt;/arguments&gt;
                    &lt;/configuration&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
    </build>

  • 编写特性文件 TodoList.feature

    #Author: zxd
    #Keywords 概要
    #Feature: 场景列表
    #Scenario: 通过带有参数的步骤列表验证业务规则
    #Given: 一些前置步骤
    #When: 执行核心操作
    #Then: 验证结果
    #And,But: 列举更多的步骤
    #Scenario Outline: 数据驱动的步骤列表
    #Examples: Container for s table
    #Background: 在每个场景之前运行的步骤列表
    #""" (Doc Strings)
    #| (Data Tables)
    #@ (Tags/Labels):场景分组
    #<> (placeholder)
    #""

    (Comments)

    #Sample Feature Definition Template
    @tag
    Feature: 任务特性

    @tag1
    Scenario Outline: 执行任务
    Given 任务列表有 个任务
    When 我完成了
    Then 任务列表还剩下 个任务
    Examples:
    | total | done | left |
    | 5 | 2 | 3 |

  • 实现该特性

    @Builder
    @Getter
    public class TodoList {
    private int total;
    public void doTask(int count) {
    if (count > total || count < 0) {
    throw new IllegalArgumentException("count is invalid "+count);
    }
    total = total - count;
    }
    }

  • 配置测试组件

    @RunWith(Cucumber.class)
    /**

    • features 用于指定特性文件的根目录
    • plugin 用于指定报告插件
      */
      @CucumberOptions(features = "src/test/resources", plugin = { "pretty", "html:target/cucumber-report/",
      "json:target/cucumber-report/cucumber.json" })
      public class CucumberTest {
      }

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = BehaviorDrivenDevelopmentInActionApplication.class)
    @WebAppConfiguration
    @SpringBootTest
    public class BaseDefs{}

  • 编写测试规范

    public class TodoListTest extends BaseDefs {
    TodoList todoList;

    @Given("^任务列表有 (\\d+) 个任务$")
    public void given(int total) throws Throwable {
        todoList = TodoList.builder().total(total).build();
    }
    
    @When("^我完成了 (\\d+) 个$")
    public void when(int done) throws Throwable {
        todoList.doTask(done);
    }
    
    @Then("^任务列表还剩下 (\\d+) 个任务$")
    public void then(int left) throws Throwable {
        assertEquals(left, todoList.getTotal());
    }

    }

  • 运行测试用例

    选中 CucumberTest 运行测试并查看报告

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章