51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1495|回复: 1
打印 上一主题 下一主题

cucumber框架

[复制链接]
  • TA的每日心情
    无聊
    7 小时前
  • 签到天数: 938 天

    连续签到: 5 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2021-9-18 10:57:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    .背景介绍
    随着测试的发展,测试自动化越来越成为人们的关注点。现在我们公司也在进行接口自动化的推广,在我看来接口自动化的价值就在于整体项目的回归,完成一些没法通过人力进行的测试,比如压力测试。为了解决测试开发人员和功能测试人员的同步问题,选择了Cucumber框架。Cucumber 是一个能够理解用普通语言描述的测试用例的行为驱动开发(BDD)的自动化测试工具,换句话说就是学习成本比较低。并且可以方便测试开发人员和功能测试人员协同合作,开发人员进行公共方法的封装,功能测试人员进行测试用例的编写。
    二.Cucumber组成
    FeaturesStep_definitionsCucumber command组成
    1.Features
    -基于Gherkin。支持语言:# language: en zh-CN)等
    - Features文件必须以.features命名。
    - 包含title,多个scenarios,每个scenario包含多个step
    示例如下:多组参数传参
    ```
    1. Features: test                           //Features关键字,测试用例集
    2. Scenario Outline: eating                  //Scenario Outline关键字,测数用例
    3.   Given there are <start> cucumbers       //Given关键字,进行接口请求
    4.   When I eat <eat> cucumbers            //When关键字,数据准备
    5.   Then I should have <left> cucumbers      //Then关键字
    6.   Examples:
    7.     | start | eat | left |
    8.     |    12 |   5 |    7 |
    9.     |    20 |   5 |   15 |
    10. ```
    复制代码
    关键字详解:
    - Feature (功能)-------------test suite (测试用例集)
    - Scenario(情景)-----------test case (测试用例)
    - Scenario Outline (or Scenario Template):如下,和examples更配哦,多用于逻辑相同时,
    - Given(给定)-------------setup(创建测试所需环境)
    - When(当)----------------test(触发被测事件)
    - Then(则)-----------------assert(断言,验证结果)
    - Background(背景):您会发现自己在一个功能的所有场景中重复相同的给定步骤,因为它在每个场景中都是重复的,这表明这些步骤对于描述场景不是必需的;它们是附带的细节。您可以通过将这些给定的步骤分组到background部分,将它们移动到后台。
    - And(or But):如果你有连续的给定何时然后
    - """(定义多行字符串):方便地将较大的文本段传递给步骤定义
    - |(用来定义表格):数据表便于将值列表传递给步骤定义
    2.Step_definitions
    - Step定义必须以关键字GivenWhenThenAnd开始,根据正则匹配对应的关键字
    - 根据feature文件中定义的step编写对应的测试代码。
    示例如下:
    1. ```java
    2. public class StepDefinition {
    3.     private String today;
    4.     private String actualAnswer;
    5.     @Given("^today is Sunday$")              //和features中的Given对应
    6.     public void today_is_Sunday() {
    7.         today = "Sunday";
    8.     }
    9.     @When("^I ask whether it's Friday yet$")     //和features中的When对应
    10.     public void i_ask_whether_is_s_Friday_yet() {
    11.         actualAnswer = IsItFriday.isItFriday(today);
    12.     }
    13.     @Then("^I should be told \"([^\"]*)\"$")        //和features中的Then对应
    14.     public void i_should_be_told(String expectedAnswer) {
    15.         assertEquals(expectedAnswer, actualAnswer);
    16.     }
    17. }
    18. ```
    复制代码
    3.Cucumber command
    - 运行*.feature文件。cucumber会分析feature文件中定义的step,然后去step -definitions寻找相匹配的step,执行step中的代码
    - 运行结果以html的形式保存,fail的情况查看对应log日志
    .cucumber开发过程
    1.首先使用cucumber原型Maven插件创建一个新的项目目录
    1. ```powershell
    2. mvn archetype:generate -DarchetypeGroupId=io.cucumber -DarchetypeArtifactId=cucumber-archetype -DarchetypeVersion=6.10.4 -DgroupId=hellocucumber -DartifactId=hellocucumber -Dpackage=hellocucumber -Dversion=1.0.0-SNAPSHOT -DinteractiveMode=false
    3. ```
    复制代码
    项目目录如下:
    2reources文件夹下,创建feature文件,包括feature,scenariosstep
    1. ```
    2. Feature: Is it Friday yet?                      //Features关键字,测试用例集
    3.   Scenario: Sunday isn't Friday                  //Scenario Outline关键字,测试用例
    4.     Given today is Sunday                     //Given关键字,进行接口请求
    5.     When I ask whether it's Friday yet             //When关键字,数据准备
    6.     Then I should be told "Nope"                //Then关键字
    7. ```
    复制代码
    3.hellocucumber文件下创建step_definitions
    1. ```java
    2. package hellocucumber;

    3. import io.cucumber.java.en.Given;
    4. import io.cucumber.java.en.Then;
    5. import io.cucumber.java.en.When;
    6. import static org.junit.Assert.*;

    7. class IsItFriday {
    8.     static String isItFriday(String today) {
    9.         return "Nope";
    10.     }
    11. }
    12. public class StepDefinition {
    13.     private String today;
    14.     private String actualAnswer;
    15.     @Given("^today is Sunday$")              //和features中的Given对应
    16.     public void today_is_Sunday() {
    17.         today = "Sunday";
    18.     }
    19.     @When("^I ask whether it's Friday yet$")     //和features中的When对应
    20.     public void i_ask_whether_is_s_Friday_yet() {
    21.         actualAnswer = IsItFriday.isItFriday(today);
    22.     }
    23.     @Then("^I should be told \"([^\"]*)\"$")        //和features中的Then对应
    24.     public void i_should_be_told(String expectedAnswer) {
    25.         assertEquals(expectedAnswer, actualAnswer);
    26.     }
    27. }
    28. ```
    复制代码
    4. 项目运行
    idea中直接运行hellocucumber文件夹下的Runcucumber.java文件即可
    1. ```java
    2. import io.cucumber.junit.Cucumber;
    3. import io.cucumber.junit.CucumberOptions;
    4. import org.junit.runner.RunWith;

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

    8. }
    9. ```
    复制代码
    四.测试用例设计
    测试用例设计时按接口文档给的标准生成数据,然后填充到如下图的examples中即可,框架会循环进行执行测试用例,生成测试结果
    1. Features: test                           //Features关键字,测试用例集
    2. Scenario Outline: eating                  //Scenario Outline关键字,测试用例
    3.   Given there are <start> cucumbers       //Given关键字,进行接口请求
    4.   When I eat <eat> cucumbers            //When关键字,数据准备
    5.   Then I should have <left> cucumbers      //Then关键字
    6.   Examples:                           //Examples关键字
    7.     | start | eat | left |
    8.     |    12 |   5 |    7 |
    9.     |    20 |   5 |   15 |
    10. ```
    复制代码
    .后期维护
    后续迭代版本功能测试人员和测试开发人员分工进行,功能测试人员维护Features,也就是测试用例;测试开发人员进行step_definitions的维护,就是一些代码逻辑和公共方法,最重要的也就是断言方法的改动比较大,接口请求就几乎是格式化的东西
    六.项目框架定制思路
    1.测试前数据准备:类似于登录后获取请求头这种在里面进行实现
    2.测试用例数据:Features文件中存放
    3.逻辑处理,接口请求:封装到Step_definitions
    4.公共工具封装:一些数据库连接,yaml文件读取或者一些其他工具的存放地点
    5.框架配置信息:环境相关信息放置位置,不同城市,不同数据库,不同账号的切换在里面进行设置
    6.测试报告存放位置:用于测试报告的存放,接口文档的存放


    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-26 17:12 , Processed in 0.068893 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表