定义步骤,文档中提到使用@step('I have the number (\d+)'),但自己实践的时候发现不好使,找了一些资料,可以改为@step(u'I have the number (.*)')。可以注意到里面还有一个改变, (\d+) 改为了(.*)。因为没有看源码,所以原理不太清楚。只是提供了一个思路,万一按照官方文档执行不起来,可以做一些改变。
如何使用Lettuce
Lettuce是一种非常有用和迷人的BDD(行为驱动开发)工具。它可以作为Python项目的自动化测试工具,执行纯文本功能描述,就像Cucumber之于Ruby。
Lettuce使开发和测试过程变得非常容易、可伸缩、可读性强,而且最好的是,它允许不懂编程序的人来描述系统行为,而不必考虑这些描述会在开发过程中自动测试系统。
安装Lettuce
首先确认已安装了Python,然后在终端执行下面的命令
user@machine:~$[sudo]pip install lettuce
定义一个问题
让我们来为Lettuce选择一个问题:给出一个数字, 它的阶乘是多少?
注
一个正整数n的阶乘,记为N!是所有正整数小于或等于n的乘积。0的阶乘是1。
项目结构
建立如下目录树下,文件zero.feature和steps.py是空的。
/home/user/projects/mymath
| tests
| features
- zero.feature
- steps.py
开始使用Lettuce吧!
让我们开始描述和解决我们的问题...
第一轮
[a].描述行为
开始用英语在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
注
zero.feature必须在features目录下,并且它的扩展名必须是.feature。你可以自由地选择其名称。
[b]. 用Python定义步骤
现在让我们定义场景的步骤,这样Lettuce就可以理解行为描述了。创建steps.py文件,它将包含描述步骤的Python代码。
Python:
from lettuce import *
@step('I have the number (\d+)')
def have_the_number(step, number):
world.number = int(number)
@step('I compute its factorial')
def compute_its_factorial(step):
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
def factorial(number):
return -1
注
steps.py必须在features目录,但不需要必须命名为steps.py,可以是任何以.py扩展的Python文件。Lettuce将在features目录中递归查找Python文件。
理想情况下,阶乘将在其他地方被定义。然而,作为第一个例子,我们将在steps.py内部实现它,所以你获得了如何用Lettuce的想法。
注意,到目前为止,我们还没有定义阶乘函数(它返回- 1)。
[c]. 执行并观察到失败
进入tests目录,终端执行:
user@machine:~/projects/mymath/tests$ lettuce
由于你还没有实现阶乘,所以不用对这个行为感到惊讶:
我们唯一的场景失败了:( 让我们解决它...
[d]. 编写代码使其通过
根据定义,我们知道0的阶乘是1。因为我们唯一的feature是…我们可以强制阶乘返回1。
from lettuce import *
@step('I have the number (\d+)')
def have_the_number(step, number):
world.number = int(number)
@step('I compute its factorial')
def compute_its_factorial(step):
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
def factorial(number):
return 1
[e]. 再一次执行,并观察其通过
再一次通过终端执行:
user@machine:~/projects/mymath/tests$ lettuce
你会很高兴看到你的阶乘实现通过了所有预期的行为:
非常棒:)
然而,一个测试是不足以检查我们的解决方案的质量…所以让我们一遍Lettuce!
第二轮
让我们提供更多的测试,以便更好地描述我们的问题,因此我们提供了一个更准确的阶乘实现:
[a]. 描述行为
我们提供两个新的场景,数字1和2:
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
[b]. 使用Python定义步骤
由于我们没有改变定义,所以不需要对这个step进行更改。
[c]. 执行,并观察其失败
user@machine:~/projects/mymath/tests$ lettuce
运行Lettuce时,我们认识到我们之前执行的阶乘对0和1都行得通,但对2却没有作用。:(
[d]. 编写代码使其通过
让我们提供一个解决方案,可以得到所有场景的正确阶乘,特别是对数字2:
from lettuce import *
@step('I have the number (\d+)')
def have_the_number(step, number):
world.number = int(number)
@step('I compute its factorial')
def compute_its_factorial(step):
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
def factorial(number):
number = int(number)
if (number == 0) or (number == 1):
return 1
else:
return number
[e]. 再次执行,观察期通过
user@machine:~/projects/mymath/tests$ lettuce
太棒啦!三个场景都通过了!
第三轮
让我们提供更多的测试,以便更好地描述我们的问题,并得到新的错误,这样我们将能解决它们。
[a]. 描述行为
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
Scenario: Factorial of 4
Given I have the number 4
When I compute its factorial
Then I see the number 24
[b]. 使用Python定义步骤
由于我们没有改变定义,所以不需要对这个step进行更改。
[c]. 执行,并观察其失败
user@machine:~/projects/mymath/tests$ lettuce
[d]. 编写代码使其通过
user@machine:~/projects/mymath/tests$ lettuce
第四轮
只要你能继续,所有的步骤都应该重复——你的软件的质量取决于这些。
语法糖
适用于0.2.19版本
步骤语句现在可以通过函数名或文件给定。
用函数名或文件定义一个步骤的话,就毫无争议用“@step”。
下面有两个步骤,同上面的例子一样。
from lettuce import *
@step
def have_the_number(step, number):
'I have the number (\d+)'
world.number = int(number)
@step
def i_compute_its_factorial(step):
world.number = factorial(world.number)
steps可以由一组“@step”组成
from lettuce import world, steps
@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):
self.environs.number = int(value)
def get_number(self):
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+)'''
self.set_number(number)
def i_compute_its_factorial(self, step):
number = self.get_number()
self.set_number(factorial(number))
def check_number(self, step, expected):
'''I see the number (\d+)'''
self._assert_number_is(int(expected))
# Important!
# Steps are added only when you instanciate the "@steps" decorated class
# Internally decorator "@steps" build a closure with __init__
FactorialSteps(world)
def factorial(number):
number = int(number)
if (number == 0) or (number == 1):
return 1
else:
return number*factorial(number-1)
上一篇:Lettuce Feature
下一篇:Lettuce Datatable