美文网首页
Cucumber + IDEA 环境搭建

Cucumber + IDEA 环境搭建

作者: 不步步不 | 来源:发表于2020-05-22 11:39 被阅读0次

    前提:已安装 idea 、 jdk 1.8
    官方安装文档: https://www.jetbrains.com/help/idea/2016.1/cucumber.html?origin=old_help
    1 IDEA > File > Setting > Plugins
    安装Cucumber for java ; Cucumber for Groovy
    2 使用maven新建工程
    3 POM文件:

        <dependencies>
            <!-- test support library (page objects, elements, test base, ...) -->
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-java</artifactId>
                <scope>test</scope>
                <version>1.2.5</version>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-junit</artifactId>
                <scope>test</scope>
                <version>1.2.5</version>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-jvm</artifactId>
                <version>1.2.5</version>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
        </dependencies>
    

    4 目录结构如下:


    image.png

    准备工作结束,开始写第一个Cucumber测试
    1 test/ersources下新建feature文件

    # Created by Jane.Yao at 2020/5/22
    Feature: Add Test
      Scenario: Add Test
        Given x is 5 and y is 6
        When add x and y
        Then Result is 11
    

    2 test/java下创建Runner文件,用于执行Cucumber测试

    package com.cucumber;
    
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;
    import org.junit.runner.RunWith;
    
    @RunWith(Cucumber.class)
    @CucumberOptions(plugin = {"pretty","json:target/cucumbr-report.json"})
    public class CalculatorTestRunner {
    }
    

    3 feature文件,alt+enter, 创建step,指定step文件存放路径


    image.png
    image.png

    4 自动生成的step文件内容

    package com.cucumber;
    
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    
    public class MyStepdefs {
        @Given("^xP is (\\d+) and y is (\\d+)$")
        public void xpIsAndYIs(int arg0, int arg1) {
        }
    
        @When("^add x and y$")
        public void addXAndY() {
        }
    
        @Then("^Result is (\\d+)$")
        public void resultIs(int arg0) {
        }
    }
    

    5 填充实际操作code

    package com.cucumber;
    
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    
    import static org.hamcrest.core.IsEqual.equalTo;
    import static org.junit.Assert.assertThat;
    
    public class MyStepdefs {
    
        private Calculator calculator;
        private int result;
    
        @Given("^xP is (\\d+) and y is (\\d+)$")
        public void xpIsAndYIs(int arg0, int arg1) {
            this.calculator = new Calculator(arg0, arg1);
        }
    
        @When("^add x and y$")
        public void addXAndY() {
            result = this.calculator.add();
        }
    
        @Then("^Result is (\\d+)$")
        public void resultIs(int arg0) {
            assertThat(result, equalTo(arg0));
        }
    }
    

    6 运行runner文件,看与期望结果是否一致


    image.png

    7 附:Calculator文件内容

    package com.cucumber;
    
    public class Calculator {
        private int x;
        private int y;
        public Calculator(int x , int y){
            this.x = x;
            this.y = y;
        }
        public int add(){
            return this.x + this.y;
        }
    }
    

    相关文章

      网友评论

          本文标题:Cucumber + IDEA 环境搭建

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