【四】behave步骤参数Step Parameters实例三

目标:是将参数切换到步骤功能里

1.编写Feature Test功能测试

# file:features/tutorial03_step_parameters.feature
# Created by bxf at 2019/11/7
#目标是将参数切换到步骤功能里
Feature: 步骤参数
  Scenario: 洗衣机
    Given 我把"衣服"放进洗衣机
    When 当我打开洗衣服的时候
    Then 它应该是变成"干净的衣服"
#特征描述包含许多参数,可以填写不同的值,这也使得自动化测试层更加简单,因为步骤定义的数量减少了

#特征描述包含许多参数,可以填写不同的值,这也使得自动化测试层更加简单,因为步骤定义的数量减少了。

最佳实践:将参数放在双引号文本中,使变化点可见。测试运行器输出不存在此问题,因为它经常将这些参数标记为粗体文本。

2.编写模型类文件

file:features/steps/WashingMachine.py
#encoding:utf-8
#date:2019/11/8 11:27
#@Author:sunny

class Washing_machine(object):
    Map={
        "波轮式洗衣机":"干净的衣服",
        "滚筒式洗衣机":"很干净的衣服",
        "搅拌式洗衣机":"干干的衣服",
    }
    def __init__(self):
        self.clothes=None
        self.result=None

    @classmethod
    def machine_result(cls,clothes):
        return cls.Map.get(clothes,"干净的衣服")

    def add(self,clothes):
        self.clothes=clothes

    def switch_open(self):
        self.result=self.machine_result(self.clothes)

3.编写自动化测试类

# file:features/steps/step_tutorial03.py
#encoding:utf-8
#date:2019/11/7 17:15
#@Author:sunny

from behave import given,when,then
from hamcrest import assert_that,equal_to
from features.steps.WashingMachine import Washing_machine

@given('我把"{clothes}"放进洗衣机') #clothes参数名随便定义
def step_give_put_clothes_into_WashingMachine(context,clothes):
    context.washingMachine=Washing_machine()
    context.washingMachine.add(clothes)

@when('当我打开洗衣服的时候')
def step_when_switchOpen_washingMachine(context):
    context.washingMachine.switch_open()

@then('它应该是变成"{other_clothes}"')
def step_then_should_become(context,other_clothes):
    assert_that(context.washingMachine.result,equal_to(other_clothes))

运行功能测试:

E:\exercise\producer2-test>behave
Feature: 步骤参数 # features/tutorial03_step_parameters.feature:3

  Scenario: 洗衣机        # features/tutorial03_step_parameters.feature:4
    Given 我把"衣服"放进洗衣机  # features/steps/step_tutorial03.py:9
    When 当我打开洗衣服的时候    # features/steps/step_tutorial03.py:14
    Then 它应该是变成"干净的衣服" # features/steps/step_tutorial03.py:18

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.002s

E:\exercise\producer2-test>
View Code

以上可知,运行案例成功。



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