【十一】behave用户自定义数据类型实例十

目标:如何在步骤参数中使用用户定义的数据类型

  用户定义的数据类型简化了步骤定义中的处理,字符串参数都将自动解析并转换成为特定的数据类型

注意:除了转换为用户定义的类型之外,此机制还可以用于在参数传递给步骤定义函数之前发生的文本转换

1.写Feature Test功能测试文件

Feature:用户定义的数据类型作为步骤参数
    作为一个测试者
    我希望将步骤参数转换为特定的数据类型
    以简化步骤定义体的编程

  Scenario Outline: 计算器
    Given 我有一个计算器
    When 我添加"<X>""<Y>"
    Then 计算器返回"<SUM>"

    Examples:
      |X | Y | SUM |
      |1 |2  | 3   |
      |2 |4  | 6   |
      |3 |2  | 5   |

2.编写类模型,这里是计算器类

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

class Caculator(object):
   #初始化
    def __init__(self,value=0):
        self.result=value

    #重置
    def reset(self):
        self.result=0

    #算法
    def add2(self,X,Y):
        self.result=(X+Y)
        return self.result

3.编写自动化测试类

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

from behave import  register_type

def parse_number(text):
    """
    将已分析的文本转换为数字
    :param text:由parse.Parser.parse()调用
    :return:Number实例(整数),从解析文本创建
    """
    return int(text)

#用户定义的类型转换器(parse_type)
register_type(Number=parse_number)


#现在你可以使用数字作为步骤定义的步骤参数类型
from behave import  *
from hamcrest import *
from features.steps.Caculator import Caculator

@given('我有一个计算器')
def step_impl(context):
    context.caculator=Caculator()

@when('我添加"{X:Number}" 和"{Y:Number}"')
def step_impl(context,X,Y):
    '''
    isinstance数据类型判断
    a=1
    isinstance(a,int)——>True
    :param context:
    :return:
    '''
    assert isinstance(X,int)
    assert isinstance(Y,int)
    context.caculator.add2(X,Y)

@then('计算器返回"{expected:Number}"')
def step_impl_result(context,expected):
    assert isinstance(expected,int)
    print('expected====',expected)
    print("actual========",context.caculator.result)
    assert_that(context.caculator.result,equal_to(expected))

4.运行测试用例

E:\exercise\producer2-test>behave
Feature: 用户定义的数据类型作为步骤参数 # features/tutorial10_step_usertype.feature:5
  作为一个测试者
  我希望将步骤参数转换为特定的数据类型
  以简化步骤定义体的编程
  Scenario Outline: 计算器 -- @1.1   # features/tutorial10_step_usertype.feature:17
    Given 我有一个计算器                 # features/steps/step_tutorial10.py:25
    When 我添加"1""2"              # features/steps/step_tutorial10.py:29
    Then 计算器返回"3"                 # features/steps/step_tutorial10.py:42

  Scenario Outline: 计算器 -- @1.2   # features/tutorial10_step_usertype.feature:18
    Given 我有一个计算器                 # features/steps/step_tutorial10.py:25
    When 我添加"2""4"              # features/steps/step_tutorial10.py:29
    Then 计算器返回"6"                 # features/steps/step_tutorial10.py:42

  Scenario Outline: 计算器 -- @1.3   # features/tutorial10_step_usertype.feature:19
    Given 我有一个计算器                 # features/steps/step_tutorial10.py:25
    When 我添加"3""2"              # features/steps/step_tutorial10.py:29
    Then 计算器返回"5"                 # features/steps/step_tutorial10.py:42

1 feature passed, 0 failed, 0 skipped
3 scenarios passed, 0 failed, 0 skipped
9 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.003s

E:\exercise\producer2-test>
View Code

根据以上结果展示,运行成功。

注意:如果要不成功,直接修改features文件里的数据即可。

 

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