【七】behave之设置表Setup Table实例六

教程6:设置表

目标:使用设置表简化测试设置,这里以人和地区为实例展开。

1.编写功能测试Feature Test

#file:features/tutorial06_setup_table.feature

Feature: 步骤设置
  Scenario: 设置表
    #如下图在Given中加入表格
    Given set a specific users:
     |name | address |
     | lijun | 成都     |
     |haha | 广州      |
     |gangge | 成都     |
    When count different address num
    Then we will find two people in "成都"
    But  we will find one person in "广州"

2.编写用户域模型

#file:features/steps/UserModel.py

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

class Users(object):
    def __init__(self,names,members=None):
        if not members:
            #members数组用来装所有的name
            members=[]
        self.name=names
        self.members=members

    def add_member(self,name):
        #断言,如果name不在members里面就追加
        assert name not in self.members
        # print("userModel:name----",name)
        self.members.append(name)

    @property
    def count(self):
        return len(self.members)

    def __len__(self):
        return self.count


class UserModel(object):
    def __init__(self):
        self.users=[]
        self.addrees={}


    def add_user(self,user,addre):
        assert user not in self.users
        # print('userModel:user',user)
        if addre not in self.addrees:
            self.addrees[addre]=Users(addre)
        self.addrees[addre].add_member(user)

    def count_person_per_addre(self):
        pass

    def get_addrecount_for(self,addre):
        return self.addrees[addre].count

3.编写testutils类

#file:features/steps/testutils.py

#encoding:utf-8
#date:2019/12/9 14:25
#@Author:sunny


class NameNumber(object):
    #将命名数字映射成数字
    MAP={
        "one":1,
        "two":2,
        "three":3,
        "four":4,
        "five":5,
        "six":6
    }

    @classmethod
    def from_string(self,name_number):
        name=name_number.strip().lower()
        return  self.MAP[name]

4.编写自动化测试类

#encoding:utf-8
#date:2019/12/9 14:23
#@Author:sunny

from hamcrest import *
from behave import *
from features.steps.testutil import NameNumber
from features.steps.UserModel import UserModel



@given('set a specific users')
def step_impl(context):
    '''
    getattr(object, name[, default])
    object -- 对象。
    name -- 字符串,对象属性。
    default -- 默认返回值,如果不提供该参数,在没有对应属性时,将触发 AttributeError。
    如:getattr(a, 'bar2', 3)    # 属性 bar2 不存在,但设置了默认值3
    '''
    model=getattr(context,"model",None)
    if not model:
        context.model=UserModel()
    #这个表格的数据可以再测试代码中使用context.table来调用,然后加到model里面。
    for row in context.table:
        #利用add_user方法添加row["name"]=左列  row["address"]=右列
        context.model.add_user(row["name"],row["address"])

@when('count different address num')
def step_impl(context):
    context.model.count_person_per_addre()

@then('we will find {count} people in "{addr}"')
def step_impl(context,count,addr):
    count_=NameNumber.from_string(count)
    # print("=========",count_)  #2
    assert_that(count_,equal_to(context.model.get_addrecount_for(addr)))

@then('we will find one person in "{addr}"')
def step_impl(context,addr):
    assert_that(1,equal_to(context.model.get_addrecount_for(addr)))

5.运行测试用例

E:\exercise\producer2-test>behave
Feature: 步骤设置 # features/tutorial06_setup_table.feature:1

  Scenario: 设置表                          # features/tutorial06_setup_table.feature:2
    Given set a specific users           # features/steps/step_tutorial06.py:12
      | name  | address |
      | xiao  | 成都      |
      | haha  | 广州      |
      | zhang | 成都      |
    When count different address num     # features/steps/step_tutorial06.py:29
    Then we will find two people in "成都" # features/steps/step_tutorial06.py:33
    But we will find one person in "广州"  # features/steps/step_tutorial06.py:39

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

E:\exercise\producer2-test>behave
Feature: 步骤设置 # features/tutorial06_setup_table.feature:1

  Scenario: 设置表                          # features/tutorial06_setup_table.feature:2
    Given set a specific users           # features/steps/step_tutorial06.py:12
      | name   | address |
      | lijun  | 成都      |
      | haha   | 广州      |
      | gangge | 成都      |
    When count different address num     # features/steps/step_tutorial06.py:29
    Then we will find two people in "成都" # features/steps/step_tutorial06.py:33
    But we will find one person in "广州"  # features/steps/step_tutorial06.py:39

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
4 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.004s

E:\exercise\producer2-test>
View Code

 

错误示范,这里修改testutils代码

"two":10,
#encoding:utf-8
#date:2019/12/9 14:25
#@Author:sunny


class NameNumber(object):
    #将命名数字映射成数字
    MAP={
        "one":1,
        "two":10,
        "three":3,
        "four":4,
        "five":5,
        "six":6
    }

    @classmethod
    def from_string(self,name_number):
        name=name_number.strip().lower()
        return  self.MAP[name]
View Code

运行结果:

Assertion Failed:
Expected: <2>
but: was <10>

E:\exercise\producer2-test>behave
Feature: 步骤设置 # features/tutorial06_setup_table.feature:1

  Scenario: 设置表                          # features/tutorial06_setup_table.feature:2
    Given set a specific users           # features/steps/step_tutorial06.py:10
      | name  | address |
      | xiao  | 成都      |
      | haha  | 广州      |
      | zhang | 成都      |
    When count different address num     # features/steps/step_tutorial06.py:18
    Then we will find two people in "成都" # features/steps/step_tutorial06.py:22
      Assertion Failed:
      Expected: <2>
           but: was <10>

    But we will find one person in "广州"  # None


Failing scenarios:
  features/tutorial06_setup_table.feature:2  设置表

0 features passed, 1 failed, 0 skipped
0 scenarios passed, 1 failed, 0 skipped
2 steps passed, 1 failed, 1 skipped, 0 undefined
Took 0m0.001s

E:\exercise\producer2-test>
View Code

将"two":10,改成2即可正常。

 

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