BDD自动化测试框架cucumber(1): 最基本的demo

BDD(Behavior Driven Development),行为驱动开发, 对应自动化测试框架,python有behave,java有cucumber, 这次记录cucumber+springboot+maven的自动化测试框架。

基本结构如下:

 

 

 1)POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>cucumberExample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--can set the same version for the same module-->
    <properties>
        <cucumber.version>2.3.1</cucumber.version>
        <cucumber-reporting.version>3.14.0</cucumber-reporting.version>
        <maven.compiler.version>3.7.0</maven.compiler.version>
        <java.compiler.version>1.8</java.compiler.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java8 -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java8</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-spring -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting -->
        <dependency>
            <groupId>net.masterthought</groupId>
            <artifactId>cucumber-reporting</artifactId>
            <version>${cucumber-reporting.version}</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.version}</version>
                <configuration>
                    <source>${java.compiler.version}</source>
                    <target>${java.compiler.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <inherited>true</inherited>
                <dependencies>
                    <!-- https://mvnrepository.com/artifact/org.apache.maven.surefire/surefire-junit4 -->
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit4</artifactId>
                        <version>3.0.0-M5</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <argLine>-Xmx1024m</argLine>
                    <argLine>-Xms1024m</argLine>
                    <forkCount>3.0c</forkCount>
                    <reuseForks>true</reuseForks>
                    <testFailureIgnore>true</testFailureIgnore>
                    <includes>
                        <include>**/demoRunner.class</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

 

2)features: 最重要的环节,用户场景

@login
Feature: login function

  Scenario Outline: password checking
    Given I have open the login page
    And input account=<account>
    And input password=<password>
    When I click the LOGIN button
    Then it return login success to me

    Examples:
    |account|password|
    |user1  |password1|
    |user2  |password2|

3)steps: 实现feature 里面每一个步骤的逻辑,一句话对应一个step一个方法

备注:这里我还没有加上方法体,以后再加

package steps;
/*
 * @author Helen Lee
 * @create 2020/9/11
 * @description
 */
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.junit.Assert;

public class login_steps {
    @Given("I have open the login page")
    public void iHaveOpenTheLoginPage() {
    }

    @Given("input account=(.*)")
    public void inputAccountAccount(String account) {

    }

    @Given("input password=(.*)")
    public void inputPasswordPassword(String password) {

    }

    @When("I click the LOGIN button")
    public void iClickTheLOGINButton() {

    }

    @Then("it return login success to me")
    public void itReturnLoginSuccessToMe() {
    }
}

4)runner: 执行testcases

/*
 * @author Helen Lee
 * @create 2020/9/11
 * @description
 */

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;

@RunWith(Cucumber.class)
@SpringBootTest
@CucumberOptions(
        tags = {"@login"},
        features = {"src/main/resources/features"},
        glue = {"steps"},
        plugin = {
                "pretty",
                "html:target/cucumber",
                "json:target/cucumberReportJsonFiles/cucumber-report.json"
        }
)
public class demoRunner {
    public void test() {
    }
}

 

完成这四个之后,就可以run demoRunner了,结果如下:跑了user1/password1 和user2/passwords两个test cases

 

 

posted @ 2020-09-11 22:14  helentester  阅读(1784)  评论(0编辑  收藏  举报