Spring-boot + Cucumber測試配置介紹

目前Spring-boot成爲了java開發的主流框架,Cucumber做爲一款支持dsl的自動化測試工具,很適合用戶編寫DSL優化過的單元測試等測試用例。本文將講解如何在SpringBoot中配置Cucumber進行自動化測試。
目錄結構以下:java

gradle相關配置以下所示:git

<properties>
        <!-- 集成測試依賴 -->
        <cucumber.java.version>1.2.5</cucumber.java.version>
    </properties>
<!--start 集成測試 -->
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <version>2.22.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>${cucumber.java.version}</version>
            <scope>test</scope>
        </dependency>

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

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

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>${cucumber.java.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.javafaker</groupId>
            <artifactId>javafaker</artifactId>
            <version>0.14</version>
            <scope>test</scope>
        </dependency>
        <!--end 集成測試 -->

src中爲常見的spring代碼結構,咱們再也不具體講解。test文件夾中上邊爲自帶的SpringBootTest測試框架,而demo中的內容是Cucumber框架的具體配置。github

1.首先咱們須要配置基本的Cucumber框架CucumberTest.java:spring

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources")
public class CucumberTest {
}

2.接下來,咱們要配置有關Spring-boot的配置,由於咱們的測試框架也須要在springboot的運行下完成AbstractDefs.java:api

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestSpringMongoApplication.class)
@WebAppConfiguration
@SpringBootTest
public class AbstractDefs
{
}

3.而後,咱們便開始寫屬於咱們的DSL解析語法,可具體的測試流程TestSteps.java:springboot

import com.tiandi.service.FaultInjectionInfoService;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;

public class TestSteps extends AbstractDefs {
    @Autowired
    private FaultInjectionInfoService faultInjectionInfoService;

    private String faultNodeId;

    @Given("^Generate testCase for \"([^\"]*)\"$")
    public void generateTestCase(String id) {
        faultNodeId = id;
    }

    @Then("^finish$")
    public void finish() {
        faultInjectionInfoService.generateTestCase(faultNodeId);
    }
}
//在這裏,咱們經過Autowired注入了spring中的service,並編寫用戶自定義的DSL來調用service中的方法執行,得到測試結果。

4.最後,咱們在resources中填寫具體的黃瓜測試用例,並執行。框架

Feature: test cucumber
  Scenario: system out print something
    Given Generate testCase for "COM-COM-F"
    Then finish

5.測試結果如圖所示工具

綠色表明測試經過單元測試

相關文章
相關標籤/搜索