Lettuce:Terrian

地形是一个Lettuce“双关语”,是它的“生活场所”,其安装和拆卸,基本上嵌入在你的Lettuce测试。

terrain.py

按照惯例,Lettuce尝试加载一个位于当前目录下叫terrain.py的文件。
这个文件作为一个全局设置的地方,在那里你可以设置全局Hook,把事情放到Lettuce的“世界”。


你也可以将terrain.py文件设置在你的Django项目根目录下,运行python manage.py命令时,Lettuce会加载它。更多内容参考Django的命令。

实践

尝试下面的文件布局:

/home/user/projects/some-project
       | features
            - the-name-of-my-test.feature
            - the-file-which-holds-step-definitions.py
            - terrain.py

接着在terrian.py文件中加一些配置,运行Lettuce

user@machine:~/projects/some-project$ lettuce

注意terrian.py将首先加载

world

为了更容易和更有趣的编写测试,Lettuce”违背了“python中好的设计的一些原则,如避免隐含的利用全局概念。
Lettuce的“world”概念主要是“全局的概念”。

实践

想象一个位于某地的文件在Lettuce开始运行试验之前,被你的应用程序导入:

from lettuce import world

world.some_variable = "yay!"

因此,在一些步骤你可以使用world的预先设定文件:

from lettuce import *

@step(r'exemplify "world" by seeing that some variable contains "(.*)"')
def exemplify_world(step, value):
    assert world.some_variable == value

这个功能可能有点像:

Feature: test lettuce's world
  Scenario: check variable
    When I exemplify "world" by seeing that some variable contains "yay!"

world.absorb

将功能或类放入lettuce.world非常有用
例如:

from lettuce import world

def my_project_wide_function():
    # do something

world.my_project_wide_function = my_project_wide_function

world.my_project_wide_function()

但是,正如你所注意到的,随着项目的发展,可能会有很多重复的行,而起到避免代码重复的:
为了避免这种情况,Lettuce提供给"world"一个“absorb”的修饰。
让我们看下它:

from lettuce import world

@world.absorb
def my_project_wide_function():
    # do something

world.my_project_wide_function()

你也可以用类的形式使用它:

from lettuce import world

@world.absorb
class MyClass:
    pass

assert isinstance(world.MyClass(), MyClass)

甚至可以用Lambda表达式,但在这种情况下,你需要命名它

from lettuce import world

world.absorb(lambda: "yeah", "optimist_function")

assert world.optimist_function() == 'yeah'

world.spew

好吧,如果你读了上面的内容,你可能会想:“如果我要把一些事情放入lettuce.world,有时候它可能膨胀,或迷惑与我的步骤,或hook的成员名。
这种时候,world“吸收”东西后,也可以“吐”。

from lettuce import world

@world.absorb
def generic_function():
    # do something

assert hasattr(world, 'generic_function')

world.spew('generic_function')

assert not hasattr(world, 'generic_function')

Hooks

Lettuce有hook,称为顺序之前和之后的每一个动作
作为Python的修饰,它可以采取任何对你有用的动作。
例如,你可以设置一个浏览器驱动的world,以及最后关闭连接,使用测试数据操作数据库,或者其他任何你想做的,例如
让我们由表及里的了解它

@before.all

这个hook将在Lettuce加载feature文件之前执行
修饰函数不需要参数

from lettuce import *

@before.all
def say_hello():
    print "Hello there!"
    print "Lettuce will start to run tests right now..."

@after.all

这个hook将在Lettuce运行所有功能,方案和步骤之后运行
修饰函数需要totalresult作为参数,这样你可以使用统计结果

from lettuce import *

@after.all
def say_goodbye(total):
    print "Congratulations, %d of %d scenarios passed!" % (
        total.scenarios_ran,
        total.scenarios_passed
    )
    print "Goodbye!"

@before.each_feature

这个hook是Lettuce运行每个功能前运行
修饰函数将Feature作为参数,这样你可以使用它来获取场景和步骤。

from lettuce import *

@before.each_feature
def setup_some_feature(feature):
    print "Running the feature %r, at file %s" % (
        feature.name,
        feature.described_at.file
    )

@after.each_feature

这个hook和@before.each_feature一样,除了:在Lettuce运行每个功能之后运行。

from lettuce import *

@after.each_feature
def teardown_some_feature(feature):
    print "The feature %r just has just ran" % feature.name

@before.each_scenario

这个hook是Lettuce运行每个场景之前运行
修饰函数将Senario作为参数,这样你可以使用它来获取内部的步骤。

from lettuce import *
from fixtures import populate_test_database

@before.each_scenario
def setup_some_scenario(scenario):
    populate_test_database()

@after.each_scenario

这个hook和@before.each_scenario一样,除了:在每个Scenario之后运行。

from lettuce import *
from database import models
@after.each_scenario
def teardown_some_scenario(scenario):
    models.reset_all_data()

@before.each_outline

这个hook在Lettuce执行每个场景大纲迭代前执行
修饰函数将Scenario参数和轮廓,每一列的名称是key

from lettuce import *

@before.each_outline
def setup_some_scenario(scenario, outline):
    print "We are now going to run scenario {0} with size={1}".format(scenario.name, outline["size"])

@after.each_outline

这个hook和@before.each_outline一样,除了:在场景大纲迭代之后执行。

from lettuce import *

@after.each_outline
def complete_some_scenario(scenario, outline):
    print "We are now finished scenario {0} with size={1}".format(scenario.name, outline["size"])

@before.each_step

这个hook在Lettuce执行每个步骤之前运行
修饰函数将Step作为参数,这样你可以用它获取表

from lettuce import *

@before.each_step
def setup_some_step(step):
    print "running step %r, defined at %s" % (
        step.sentence,
        step.defined_at.file
    )

@after.each_step

这个hook和@before.each_step一样,除了:在步骤执行之后运行

from lettuce import *

@after.each_step
def teardown_some_step(step):
    if not step.hashes:
       print "no tables in the step"

django-specific hooks

自从Lettuce正式支持Django,有一些特定的hooks,帮助建立你的测试套件。

@before.harvest

这个hook是在Lettuce执行Django试验之前运行。它可以设置浏览器的驱动程序,这是非常有用的(像selenium),在所有Django测试之前 。
修饰函数使用字典作为参数,内嵌harvest命令行的局部变量。

from lettuce import *
from lettuce.django import django_url
from selenium import selenium

@before.harvest
def prepare_browser_driver(variables):
    if variables.get('run_server', False) is True:
        world.browser = selenium('localhost', 4444, '*firefox', django_url('/'))
        world.browser.start()

@after.harvest

这个hook是Lettuce执行Django测试后运行。这非常有用,在关闭浏览器之前开始执行(见上面的例子)。
修饰函数用Totalresult对象列表作为参数。

from lettuce import *

@after.harvest
def shutdown_browser_driver(results):
   world.browser.stop()

@before.each_app

这个hook是在Lettuce运行每个Django程序之前运行。
修饰函数获取与当前应用程序相对应的Python模块。

from lettuce import *

@before.each_app
def populate_blog_database(app):
    if app.__name__ == 'blog':
        from blog.models import Post
        Post.objects.create(title='Nice example', body='I like writting!')

@after.each_app

这个hook是在Lettuce运行每个Django程序之后运行。
修饰函数接受两个参数:
当前应用相对应的Python模块。
Totalresult作为参数,这样你可以使用统计结果

from lettuce import *

@after.each_app
def clear_blog_database_if_successful(app, result):
    if app.__name__ == 'blog':
        if result.scenarios_ran is result.scenarios_passed:
            from blog.models import Post, Comment
            Comment.objects.all()
            Post.objects.all()

@before.runserver and @after.runserver

这些hooks是在Lettuce启动内置的HTTP服务器前后执行。
修饰函数lettuce.django.server.threadedserver对象作为参数。

from lettuce import *
from django.core.servers.basehttp import WSGIServer

@before.runserver
def prepare_database(server):
    assert isinstance(server, WSGIServer)
    import mydatabase
    mydatabase.prepare()

@after.runserver
def say_goodbye(server):
    assert isinstance(server, WSGIServer)
    print "goodbye, see you soon"

@before.handle_request and @after.handle_request

这些hooks是在Lettuce运行内置HTTP请求前后运行。
修饰函数功能将下面两个作为参数:
django.core.servers.basehttp.wsgiserver对象。
lettuce.django.server.threadedserver对象

from lettuce import *
from django.core.servers.basehttp import WSGIServer

@before.handle_request
def print_request(httpd, server):
    socket_object, (client_address, size) = httpd.get_request()
    print socket_object.dup().recv(size)

@after.handle_request
def say_goodbye(httpd, server):
    socket_object, (client_address, size) = httpd.get_request()
    print "I've just finished to respond to the client %s" % client_address

警告
所有的handle_request hooks内运行Python线程。如果CalBack出错,Lettuce会卡住。


上一篇:Lettuce: Features, Scenarios and Steps reference
下一篇:Lettuce: Language Support

你可能感兴趣的:(Lettuce:Terrian)