行为驱动(1)
1、简介
BDD:Behavior Driven Development
本质:用中文、英文或其他语言编写测试用例,然后去执行。每一个语言通过装饰器对应到一个测试用例步骤的执行。
关键字:
(1)Feature:特性,将多个测试用例集合到一起,对应于unittest中的test suite(测试用例集)
(2)Scenario:情景,用于描述一个用例,对应于unittest中的test case(测试用例)
(3)Given:如果,用例开始执行前的一个前置条件,类似于unittest中setup方法中的一些步骤
(4)When:当,用例开始执行时的一好些关键操作步骤,类似于unittest中的以test开头的方法,比如执行一个单击元素的操作
(5)Then:那么,验证结果,就是平时用例中中验证步骤,比如assert方法
(6)And:和,一个步骤中如果存在多个Given操作,后面的Given可以用And代替
(7)But:一个步骤中如果存在多个Then操作,第二个开始后面的Then可以用But代替
2、环境准备:
(1)安装lettuce 命令行使命令:pip install lettuce 验证是否安装成功---进入python交互模式,导入lettuce:import lettuce
(2)导入时出现警报:安装python-Levenshtein pip install python-Levenshtein
(3)安装python-Levenshtein时报错,需要安装VCForPython27.msi(Visual C++环境)
3、第一个英文语言行为驱动测试
测试逻辑:
(1)从lettuce全局变量命名空间world中取得一个整数
(2)计算该整数的阶乘
(3)断言计算结果的正确性
BDD实施步骤:
(1)Pycharm工具创建如下所示的目录结构及文件:
lettuce\MyFirstBDD\features\-zero.feature -steps.py
前两层目录lettuce和MyFirstBDD不是必需的,而且名字可以自定义;但features目录是必须存在的,并且目录名不能更改,执行行为驱动脚本时,lettuce首先寻找的就是这个名字的目录;features目录下放的是执行场景文件(扩展名为.feature的文件)和描述行为的脚本文件(扩展名为.py的文件)
(2)zero.feature用于完成lettuce工作流程的第一步,描述测试场景的行为,具体内容如下:
Feature: Compute factorial In order to play with Lettuce As beginners We‘ll implement factorial Scenario: Factorial of 0 Given I have the number 0 When I compute its factorial Then I see the number 1 Scenario: Factorial of 1 Given I have the number 1 When I compute its factorial Then I see the number 1 Scenario: Factorial of 2 Given I have the number 2 When I compute its factorial Then I see the number 2 Scenario: Factorial of 3 Given I have the number 3 When I compute its factorial Then I see the number 6
(3)steps.py里面使用Python语言编写行为步骤,并且提供检测执行结果代码,具体内容如下:
#encoding=utf-8 from lettuce import * #用于计算整数的阶乘函数 def factorial(number): number = int(number) if number == 0 or number == 1: return 1 else: return reduce(lambda x,y: x * y,range(1,number+1)) @step("I have the number (\d+)") def have_the_number(step,number): #将通过正则表达式匹配的数字在于全局变量world中 world.number = int(number) @step("I compute its factorial") def compute_its_factorial(step): #从全局变量world中取出匹配的数字 #计算其阶乘,并将结果再存回world中 world.number = factorial(world.number) @step("I see the number (\d+)") def check_number(step,expected): #通过正则匹配到预期数字 expected = int(expected) #断言计算阶乘结果是否等于期望 assert world.number == expected,"Got %d" % world.number
(4)在PyCharm工具的Terminal(终端)中,将当前工作目录切换到features目录所在的目录(操作方法如同Windows的CMD),然后执行命令“lettuce”启动行为驱动测试,也可以直接在cmd中执行“lettuce”
4、通过类模式实现英文行为驱动
测试逻辑:
(1)将测试步骤封装到类中,并从全局变量中获取需要的计算阶乘的整数
(2)计算该整数的阶乘
(3)断言计算结果的正确性
BDD实施步骤:
(1)Pycharm工具创建如下所示的目录结构及文件:
lettuce\ClassBDD\features\-zero.feature -steps.py
(2)zero.feature文件具体内容如下:
Feature: Compute factorial In order to play with Lettuce As beginners We‘ll implement factorial Scenario: Factorial of 0 Given I have the number 0 When I compute its factorial Then I see the number 1 Scenario: Factorial of 1 Given I have the number 1 When I compute its factorial Then I see the number 1 Scenario: Factorial of 2 Given I have the number 2 When I compute its factorial Then I see the number 2 Scenario: Factorial of 3 Given I have the number 3 When I compute its factorial Then I see the number 6
(3)steps.py文件具体内容如下:
#encoding=utf-8 from lettuce import * def factorial(number): number = int(number) if number == 0 or number == 1: return 1 else: return reduce(lambda x,y: x * y,range(1,number+1)) @steps class FactorialSteps(object): """Methods in exclude or starting with _ will not be considered as step""" exclude = ["set_number",‘get_number‘] def __init__(self,environs): #初始全局变量 self.environs = environs def set_number(self,value): #设置全局变量中的number变量的值 self.environs.number = int(value) def get_number(self): #从全局变量中取出number的值 return self.environs.number def _assert_number_is(self,expected,msg = "Got %d"): number = self.get_number() #断言 assert number == expected,msg % number def have_the_number(self,step,number): """I have the number (\d+)""" #上面的三引号引起的代码必须写,并且必须是三引号引起 #表示从场景步骤中获取需要的数据 #并将获取的数据存到全局变量number中 self.set_number(number) def i_compute_its_factorial(self,step): number = self.get_number() #调用factorial方法进行阶乘计算 #并将结果在于全局变量中的number中 self.set_number(factorial(number)) def check_number(self,step,expected): """I see the number (\d+)""" #上面的三引号引起的代码必须写,并且必须是三引号引起 #表示从场景步骤中获取需要的数据以便断言测试结果 self._assert_number_is(int(expected)) FactorialSteps(world)
原文地址:https://www.cnblogs.com/test-chen/p/10975308.html