【八】behave之结果表Result Table实例七
目标:使用结果表简化预期数据集的比较
结果表的用法各不相同,通常决定取决于你想比较什么?测试自动化的这些变化是:
- 有序数据集比较
- 无序数据集比较
- 有序子集比较(结果表包含子集)
- 无序子集比较(结果表包含子集)
- FIT测试框架通过fixture提供了类似的概念。FIT的扩展FitLibrary提供了更高级的fixture类/表。
- 在本实例中,无序数据集比较和无序子集比较都用于两种不同的场景。
1.编写功能测试Feature Test
# Created by bxf at 2019/11/12 Feature: 步骤结果表 Scenario: 无序结果表比较 Given set a specific users: |name | address | |xiao | 成都 | |haha | 成都 | |weiji |广州 | |jiaming|广州 | Then we will have the following people in "成都": |name| |xiao | |xiao | And we will have the following people in "广州": |name | |weiji | |jiaming| Scenario: 子集结果表比较 Given set a specific users: |name | address| |xiao | super-成都| |haha |super-成都 | Then we will have at least the following people in "super-成都": |name| |xiao|
2.提供用户域模型,用的上篇的实例六
#encoding:utf-8 #date:2019/12/9 14:14 #@Author:sunny class Users(object): def __init__(self,name,members=None): if not members: members=[] self.name=name self.members=members def add_member(self,name): assert name not in self.members 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.addresses={} def add_user(self,name,address): assert name not in self.users if address not in self.addresses: self.addresses[address]=Users(address) self.addresses[address].add_member(name) def count_person_per_address(self): pass def get_addresscount_for(self,address): return self.addresses[address].count
3.提供自动化测试类
#encoding:utf-8 #date:2019/11/12 10:27 #@Author:sunny from behave import * from hamcrest import * from hamcrest.library.collection.issequence_containinginanyorder import \ contains_inanyorder @then('we will have the following people in "{addr}"') def step_impl(context,addr): """ 实际的和预期的相比较 注意:无序比较(排序不重要) :param context: :param addr: :return: """ addr_=context.model.addresses.get(addr,None) if not addr_: assert_that(False,"addr %s is unknown" % addr) expected_person=[row['name'] for row in context.table] actual_person=addr_.members #无序表的断言 assert_that(contains_inanyorder(*expected_person),actual_person) @then('we will have at least the following people in "{addr}"') def step_impl(context,addr): """ 将人员子集与地点中的实际人员进行比较 注意:无序子集比较 :param context: :param addr: :return: """ addr_ = context.model.addresses.get(addr, None) if not addr_: assert_that(False,"addr %s is unknown" % addr)
#预期值 expected_person=[row["name"] for row in context.table]
#实际值 actual_person=addr_.members #断言 assert_that(has_items(*expected_person),actual_person)
4.运行自动化测试用例
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 Feature: 步骤结果表 # features/tutorial07_step_result_table.feature:2 Scenario: 无序结果表比较 # features/tutorial07_step_result_table.feature:3 Given set a specific users # features/steps/step_tutorial06.py:12 | name | address | | xiao | 成都 | | haha | 成都 | | weiji | 广州 | | jiaming | 广州 | Then we will have the following people in "成都" # features/steps/step_tutorial07.py:10 | name | | xiao | | xiao | And we will have the following people in "广州" # features/steps/step_tutorial07.py:10 | name | | weiji | | jiaming | Scenario: 子集结果表比较 # features/tutorial07_step_result_table.feature:19 Given set a specific users # features/steps/step_tutorial06.py:12 | name | address | | xiao | super-成都 | | haha | super-成都 | Then we will have at least the following people in "super-成都" # features/steps/step_tutorial07.py:30 | name | | xiao | 2 features passed, 0 failed, 0 skipped 3 scenarios passed, 0 failed, 0 skipped 9 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m0.004s E:\exercise\producer2-test>
由以上打印结果可知,将实例六的数据也打印出来了。这里需要两边的feature的heading名称一致 |name | address | 不然这里运行会报错。
善于跌倒仍喜爱奔跑~

浙公网安备 33010602011771号