【九】behave之在一个步骤中执行其他操作实例八

目标:重用现有步骤的序列作为步骤宏

场景:有时候,我们希望从一个简单的宏步骤(宏功能)替换场景中的多个功能,为了避免测试自动化层中代码重复,DBB框架通常提供一个功能,可以从步骤中轻松调用这些步骤

1.编写Feature Test功能测试

# Created by bxf at 2019/11/12
#file:features/tutorial08_step_executes_step.feature
Feature: Step executes other Steps (tutorial08)

   Scenario: Step by Step
     Given 开始玩全民k歌
     When  我点击red开始按钮
      And  我唱完了
     Then  我可以发布歌曲

   Scenario: Execute multiple Steps in middle Step
     Given 开始玩全民k歌
     When  和之前做同样的操

2.编写自动化测试代码

#encoding:utf-8
#date:2019/11/12 14:00
#@Author:sunny

rom behave   import given, when, then
from hamcrest import assert_that, greater_than

@given('开始玩全民k歌')
def step_impl(context):
    context.duck_count = 0
    context.red_button_pressed = 0

@when('我点击red开始按钮')
def step_impl(context):
    context.red_button_pressed += 1

@when('我唱完了')
def step_impl(context):
    context.duck_count += 1

@when('和之前做同样的操作')
def step_impl(context):
    context.execute_steps(u"""
        when 我点击{button_color}开始按钮
        and 我唱完了
    """.format(button_color="red"))

@then('我可以发布歌曲')
def step_impl(context):
  #断言匹配对象给定值是否大于0 assert_that(context.duck_count, greater_than(0)) assert_that(context.red_button_pressed, greater_than(0))

3.运行测试用例:

E:\exercise\producer2-test>behave
Feature: Step executes other Steps (tutorial08) # features/tutorial08_step_executes_steps.feature:15

  Scenario: Step by Step  # features/tutorial08_step_executes_steps.feature:17
    Given 开始一个新游戏         # features/steps/step_tutorial08.py:44
    When 我点击red开始按钮       # features/steps/step_tutorial08.py:49
    And 我唱完了              # features/steps/step_tutorial08.py:53
    Then 我可以发布歌曲          # features/steps/step_tutorial08.py:64

  Scenario: Execute multiple Steps in middle Step  # features/tutorial08_step_executes_steps.feature:23
    Given 开始一个新游戏                                  # features/steps/step_tutorial08.py:44
    When 和之前做同样的操作                                 # features/steps/step_tutorial08.py:57
    Then 我可以发布歌曲                                   # features/steps/step_tutorial08.py:64

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
7 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.005s

E:\exercise\producer2-test>
View Code

以上,说明运行成功。

 

posted @ 2019-12-11 14:35  做一只热爱生活的小透明  阅读(545)  评论(0)    收藏  举报