searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

Golang BDD框架Godog使⽤系列(2)

2023-07-25 10:02:58
81
0

接上篇 ,这次介绍一些Godog的进阶用法

关键字

Background

通过Background关键字可以在一个Feature的范围内定义一些公共数据,以能在Feature的多个step中复用这些数据。示例(来自官网):

Feature: Multiple site support
  Only blog owners can post to a blog, except administrators,
  who can post to all blogs.

  Background:
    Given a global administrator named "Greg"
    And a blog named "Greg's anti-tax rants"
    And a customer named "Dr. Bill"
    And a blog named "Expensive Therapy" owned by "Dr. Bill"

  Scenario: Dr. Bill posts to his own blog
    Given I am logged in as Dr. Bill
    When I try to post to "Expensive Therapy"
    Then I should see "Your article was published."

  Scenario: Dr. Bill tries to post to somebody else's blog, and fails
    Given I am logged in as Dr. Bill
    When I try to post to "Greg's anti-tax rants"
    Then I should see "Hey! That's not your blog!"

Scenario Outline

这个关键字是用来用一些用例或者数据跑一个Scenario多次。以下实例来自官网,例如有下面的的场景

Scenario: eat 5 out of 12
  Given there are 12 cucumbers
  When I eat 5 cucumbers
  Then I should have 7 cucumbers

Scenario: eat 5 out of 20
  Given there are 20 cucumbers
  When I eat 5 cucumbers
  Then I should have 15 cucumbers

可以通过Scenario Outline进行简化

Scenario Outline: eating
  Given there are <start> cucumbers
  When I eat <eat> cucumbers
  Then I should have <left> cucumbers

  Examples:
    | start | eat | left |
    |    12 |   5 |    7 |
    |    20 |   5 |   15 |

Step参数

Doc String

这个用于指定一大块的文本描述,来定义一个step。需要被 """包裹。这里的内容可以根据自己的需要定义和解析:
例如下面的格式:
Markdown:

Given a blog post named "Random" with Markdown body
  """
  Some Title, Eh?
  ===============
  Here is the first paragraph of my blog post. Lorem ipsum dolor sit amet,
  consectetur adipiscing elit.
  """

Json

Scenario: Json Post   
	When I send "POST" request to "/api-test" with json:  
	"""  
	{  
		"msg": "hello"
	}  
	"""  
	Then the response code should be 200  
	And the response body should equal to json:  
	"""  
	{  
		"msg": "got hello"
	}

golang代码里直接可以利用markdown或json库对"""内的内容进行解析。

json还可以结合类似于 gjson类似的库以对json的字段进行断言和验证

Scenario: Json Value Assert
	When I send "POST" request to "/students" with json:  
	"""  
	{  
		"class": "2" 
	}  
	"""  
	Then the response code should be 200  
	And the json response "code" equals "0"  
	And the json response "data.health_checks.0.status" equals "1"  
	And the json response "data.health_checks.1.status" equals "2"

golang代码

func (hc *NfvHealthCheckTest) theJsonResponseEquals(jsonPath, value string) error {  
	actual := gjson.Get(hc.respBody, jsonPath).String()  
	if actual != cast.ToString(value) {  
		return fmt.Errorf("json path value not match, respbody:%s, path:%s, expect:%s, actual:%s", hc.respBody, jsonPath, value, actual)  
	}  
	return nil  
}

DataTable

顾名思义,数据表,可方便的定义一组数据来进行初始化, 实例如下。方便进行数据转换

Given the following users exist:
  | name   | email              | twitter         |
  | Aslak  | aslak@cucumber.io  | @aslak_hellesoy |
  | Julien | julien@cucumber.io | @jbpros         |
  | Matt   | matt@cucumber.io   | @mattwynne      |

对应解析代码,这里可以用到 assistdog 进行了数据转换

type User struct {
	Name string
	Email string
	Twitter string
}

func (hc *NfvHealthCheckTest) thereAreHealthChecks(data *godog.Table) error {  
	ctx := context.TODO()  
	  
	result, err := assistdog.NewDefault().CreateSlice(new(User), data)  
	if err != nil {  
		return fmt.Errorf("convert table to users error, err:%v", err)  
	}  
	users := result.([]*dbmodel.User)  
	fmt.Printf("%+v \n", users)
	return nil  
}

Tag

通过Tag可以在给Step打上指定标签,在执行代码过程中可以指定tag来执行对应的step。tag之间可以通过逻辑关系来过滤指定tag的step。具体示例如下(来自Github

Feature: tag filters
  In order to test application behavior
  As a test suite
  I need to be able to filter features and scenarios by tags

  Scenario: should filter scenarios by X tag
    Given a feature "normal.feature" file:
      """
      Feature: tagged

        @x
        Scenario: one
          Given a feature path "one"

        @x
        Scenario: two
          Given a feature path "two"

        @x @y
        Scenario: three
          Given a feature path "three"

        @y
        Scenario: four
          Given a feature path "four"
      """
    When I run feature suite with tags "@x"
    Then the suite should have passed
    And I should have 3 scenario registered
    And the following steps should be passed:
      """
      a feature path "one"
      a feature path "two"
      a feature path "three"
      """

  Scenario: should filter scenarios by X tag not having Y
    Given a feature "normal.feature" file:
      """
      Feature: tagged

        @x
        Scenario: one
          Given a feature path "one"

        @x
        Scenario: two
          Given a feature path "two"

        @x @y
        Scenario: three
          Given a feature path "three"

        @y @z
        Scenario: four
          Given a feature path "four"
      """
    When I run feature suite with tags "@x && ~@y"
    Then the suite should have passed
    And I should have 2 scenario registered
    And the following steps should be passed:
      """
      a feature path "one"
      a feature path "two"
      """

  Scenario: should filter scenarios having Y and Z tags
    Given a feature "normal.feature" file:
      """
      Feature: tagged

        @x
        Scenario: one
          Given a feature path "one"

        @x
        Scenario: two
          Given a feature path "two"

        @x @y
        Scenario: three
          Given a feature path "three"

        @y @z
        Scenario: four
          Given a feature path "four"
      """
    When I run feature suite with tags "@y && @z"
    Then the suite should have passed
    And I should have 1 scenario registered
    And the following steps should be passed:
      """
      a feature path "four"
      """

其他

还有其他例子如load feature文件等,可以参考GodogFeature支持

使用案例

可以参考官方example- Godog 示例,此处不在做过多赘述

0条评论
0 / 1000
c****m
2文章数
1粉丝数
c****m
2 文章 | 1 粉丝
c****m
2文章数
1粉丝数
c****m
2 文章 | 1 粉丝
原创

Golang BDD框架Godog使⽤系列(2)

2023-07-25 10:02:58
81
0

接上篇 ,这次介绍一些Godog的进阶用法

关键字

Background

通过Background关键字可以在一个Feature的范围内定义一些公共数据,以能在Feature的多个step中复用这些数据。示例(来自官网):

Feature: Multiple site support
  Only blog owners can post to a blog, except administrators,
  who can post to all blogs.

  Background:
    Given a global administrator named "Greg"
    And a blog named "Greg's anti-tax rants"
    And a customer named "Dr. Bill"
    And a blog named "Expensive Therapy" owned by "Dr. Bill"

  Scenario: Dr. Bill posts to his own blog
    Given I am logged in as Dr. Bill
    When I try to post to "Expensive Therapy"
    Then I should see "Your article was published."

  Scenario: Dr. Bill tries to post to somebody else's blog, and fails
    Given I am logged in as Dr. Bill
    When I try to post to "Greg's anti-tax rants"
    Then I should see "Hey! That's not your blog!"

Scenario Outline

这个关键字是用来用一些用例或者数据跑一个Scenario多次。以下实例来自官网,例如有下面的的场景

Scenario: eat 5 out of 12
  Given there are 12 cucumbers
  When I eat 5 cucumbers
  Then I should have 7 cucumbers

Scenario: eat 5 out of 20
  Given there are 20 cucumbers
  When I eat 5 cucumbers
  Then I should have 15 cucumbers

可以通过Scenario Outline进行简化

Scenario Outline: eating
  Given there are <start> cucumbers
  When I eat <eat> cucumbers
  Then I should have <left> cucumbers

  Examples:
    | start | eat | left |
    |    12 |   5 |    7 |
    |    20 |   5 |   15 |

Step参数

Doc String

这个用于指定一大块的文本描述,来定义一个step。需要被 """包裹。这里的内容可以根据自己的需要定义和解析:
例如下面的格式:
Markdown:

Given a blog post named "Random" with Markdown body
  """
  Some Title, Eh?
  ===============
  Here is the first paragraph of my blog post. Lorem ipsum dolor sit amet,
  consectetur adipiscing elit.
  """

Json

Scenario: Json Post   
	When I send "POST" request to "/api-test" with json:  
	"""  
	{  
		"msg": "hello"
	}  
	"""  
	Then the response code should be 200  
	And the response body should equal to json:  
	"""  
	{  
		"msg": "got hello"
	}

golang代码里直接可以利用markdown或json库对"""内的内容进行解析。

json还可以结合类似于 gjson类似的库以对json的字段进行断言和验证

Scenario: Json Value Assert
	When I send "POST" request to "/students" with json:  
	"""  
	{  
		"class": "2" 
	}  
	"""  
	Then the response code should be 200  
	And the json response "code" equals "0"  
	And the json response "data.health_checks.0.status" equals "1"  
	And the json response "data.health_checks.1.status" equals "2"

golang代码

func (hc *NfvHealthCheckTest) theJsonResponseEquals(jsonPath, value string) error {  
	actual := gjson.Get(hc.respBody, jsonPath).String()  
	if actual != cast.ToString(value) {  
		return fmt.Errorf("json path value not match, respbody:%s, path:%s, expect:%s, actual:%s", hc.respBody, jsonPath, value, actual)  
	}  
	return nil  
}

DataTable

顾名思义,数据表,可方便的定义一组数据来进行初始化, 实例如下。方便进行数据转换

Given the following users exist:
  | name   | email              | twitter         |
  | Aslak  | aslak@cucumber.io  | @aslak_hellesoy |
  | Julien | julien@cucumber.io | @jbpros         |
  | Matt   | matt@cucumber.io   | @mattwynne      |

对应解析代码,这里可以用到 assistdog 进行了数据转换

type User struct {
	Name string
	Email string
	Twitter string
}

func (hc *NfvHealthCheckTest) thereAreHealthChecks(data *godog.Table) error {  
	ctx := context.TODO()  
	  
	result, err := assistdog.NewDefault().CreateSlice(new(User), data)  
	if err != nil {  
		return fmt.Errorf("convert table to users error, err:%v", err)  
	}  
	users := result.([]*dbmodel.User)  
	fmt.Printf("%+v \n", users)
	return nil  
}

Tag

通过Tag可以在给Step打上指定标签,在执行代码过程中可以指定tag来执行对应的step。tag之间可以通过逻辑关系来过滤指定tag的step。具体示例如下(来自Github

Feature: tag filters
  In order to test application behavior
  As a test suite
  I need to be able to filter features and scenarios by tags

  Scenario: should filter scenarios by X tag
    Given a feature "normal.feature" file:
      """
      Feature: tagged

        @x
        Scenario: one
          Given a feature path "one"

        @x
        Scenario: two
          Given a feature path "two"

        @x @y
        Scenario: three
          Given a feature path "three"

        @y
        Scenario: four
          Given a feature path "four"
      """
    When I run feature suite with tags "@x"
    Then the suite should have passed
    And I should have 3 scenario registered
    And the following steps should be passed:
      """
      a feature path "one"
      a feature path "two"
      a feature path "three"
      """

  Scenario: should filter scenarios by X tag not having Y
    Given a feature "normal.feature" file:
      """
      Feature: tagged

        @x
        Scenario: one
          Given a feature path "one"

        @x
        Scenario: two
          Given a feature path "two"

        @x @y
        Scenario: three
          Given a feature path "three"

        @y @z
        Scenario: four
          Given a feature path "four"
      """
    When I run feature suite with tags "@x && ~@y"
    Then the suite should have passed
    And I should have 2 scenario registered
    And the following steps should be passed:
      """
      a feature path "one"
      a feature path "two"
      """

  Scenario: should filter scenarios having Y and Z tags
    Given a feature "normal.feature" file:
      """
      Feature: tagged

        @x
        Scenario: one
          Given a feature path "one"

        @x
        Scenario: two
          Given a feature path "two"

        @x @y
        Scenario: three
          Given a feature path "three"

        @y @z
        Scenario: four
          Given a feature path "four"
      """
    When I run feature suite with tags "@y && @z"
    Then the suite should have passed
    And I should have 1 scenario registered
    And the following steps should be passed:
      """
      a feature path "four"
      """

其他

还有其他例子如load feature文件等,可以参考GodogFeature支持

使用案例

可以参考官方example- Godog 示例,此处不在做过多赘述

文章来自个人专栏
文章 | 订阅
0条评论
0 / 1000
请输入你的评论
0
0