Commit 5fd4ec28 by Gabriel Falcão

Adding missing files from last commit and implementing Before and After…

Adding missing files from last commit and implementing Before and After callbacks for each feature. closes #16
parent e9c41f64
...@@ -289,7 +289,14 @@ class Feature(object): ...@@ -289,7 +289,14 @@ class Feature(object):
return scenarios, description return scenarios, description
def run(self, ignore_case=True): def run(self, ignore_case=True):
for callback in CALLBACK_REGISTRY['feature']['before_each']:
callback(self)
scenarios_ran = [scenario.run(ignore_case) for scenario in self.scenarios] scenarios_ran = [scenario.run(ignore_case) for scenario in self.scenarios]
for callback in CALLBACK_REGISTRY['feature']['after_each']:
callback(self)
return FeatureResult(self, *scenarios_ran) return FeatureResult(self, *scenarios_ran)
class FeatureResult(object): class FeatureResult(object):
......
# -*- coding: utf-8 -*-
# <Lettuce - Behaviour Driven Development for python>
# Copyright (C) <2010> Gabriel Falcão <gabriel@nacaolivre.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import threading
STEP_REGISTRY = {}
world = threading.local()
world._set = False
CALLBACK_REGISTRY = {
'step': {
'before_each': [],
'after_each': []
},
'scenario': {
'before_each': [],
'after_each': []
},
'feature': {
'before_each': [],
'after_each': []
}
}
...@@ -29,6 +29,11 @@ class before: ...@@ -29,6 +29,11 @@ class before:
CALLBACK_REGISTRY['scenario']['%s_each' % cls.__name__].append(function) CALLBACK_REGISTRY['scenario']['%s_each' % cls.__name__].append(function)
return function return function
@classmethod
def each_feature(cls, function):
CALLBACK_REGISTRY['feature']['%s_each' % cls.__name__].append(function)
return function
class after: class after:
@classmethod @classmethod
def each_step(cls, function): def each_step(cls, function):
...@@ -40,3 +45,8 @@ class after: ...@@ -40,3 +45,8 @@ class after:
CALLBACK_REGISTRY['scenario']['%s_each' % cls.__name__].append(function) CALLBACK_REGISTRY['scenario']['%s_each' % cls.__name__].append(function)
return function return function
@classmethod
def each_feature(cls, function):
CALLBACK_REGISTRY['feature']['%s_each' % cls.__name__].append(function)
return function
# -*- coding: utf-8 -*-
# <Lettuce - Behaviour Driven Development for python>
# Copyright (C) <2010> Gabriel Falcão <gabriel@nacaolivre.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from lettuce.terrain import world
world.works_fine = True
#!/usr/bin/env python
# -*- coding: utf-8; -*-
#
# Copyright (C) 2009 Gabriel Falcão <gabriel@nacaolivre.org>
# Copyright (C) 2009 Bernardo Heynemann <heynemann@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
from os.path import abspath, dirname, join, split
from nose.tools import assert_equals
from lettuce.fs import FileSystem
def test_abspath():
fs = FileSystem()
p = fs.abspath(".")
p2 = abspath(".")
assert p == p2
def test_current_dir_with_join():
fs = FileSystem()
got = fs.current_dir("etc")
expected = join(abspath("."), "etc")
assert_equals(got, expected)
def test_current_dir_without_join():
fs = FileSystem()
got = fs.current_dir()
expected = abspath(".")
assert_equals(got, expected)
def test_join():
fs = FileSystem()
p = fs.join(fs.abspath("."), "test")
p2 = join(abspath("."), "test")
assert p == p2, "Expected:\n%r\nGot:\n%r" % (p2, p)
def test_dirname():
fs = FileSystem()
p = fs.dirname(fs.abspath("."))
p2 = dirname(abspath("."))
assert p == p2, "Expected:\n%r\nGot:\n%r" % (p2, p)
def test_recursive_locate():
fs = FileSystem()
files = fs.locate(path=abspath(join(dirname(__file__), "files_to_locate")), match="*.txt", recursive=True)
assert files
assert isinstance(files, list)
assert len(files) == 2
assert split(files[0])[-1] == "test.txt"
assert split(files[1])[-1] == "test2.txt"
assert split(split(files[1])[0])[-1] == "sub"
def test_non_recursive_locate():
fs = FileSystem()
files = fs.locate(path=abspath(join(dirname(__file__), "files_to_locate")), match="*.txt", recursive=False)
assert files
assert isinstance(files, list)
assert len(files) == 1
assert split(files[0])[-1] == "test.txt"
def test_open_non_abspath():
fs = FileSystem()
assert fs.open('tests/functional/data/some.txt', 'r').read() == 'some text here!\n'
def test_open_abspath():
fs = FileSystem()
assert fs.open(abspath('./tests/functional/data/some.txt'), 'r').read() == 'some text here!\n'
def test_open_raw_non_abspath():
fs = FileSystem()
assert fs.open_raw('tests/functional/data/some.txt', 'r').read() == 'some text here!\n'
def test_open_raw_abspath():
fs = FileSystem()
assert fs.open_raw(abspath('./tests/functional/data/some.txt'), 'r').read() == 'some text here!\n'
# -*- coding: utf-8 -*-
# <Lettuce - Behaviour Driven Development for python>
# Copyright (C) <2010> Gabriel Falcão <gabriel@nacaolivre.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from os.path import dirname, abspath, join
from lettuce import Runner
from lettuce.terrain import world
def test_imports_terrain_under_path_that_is_run():
assert not hasattr(world, 'works_fine')
runner = Runner(join(abspath(dirname(__file__)), '1st_feature_dir'))
assert runner.terrain
assert hasattr(world, 'works_fine')
assert world.works_fine
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
from nose.tools import assert_equals from nose.tools import assert_equals
from lettuce import step from lettuce import step
from lettuce import registry
from lettuce.terrain import after from lettuce.terrain import after
from lettuce.terrain import before from lettuce.terrain import before
from lettuce.terrain import world from lettuce.terrain import world
...@@ -106,3 +107,27 @@ def test_after_each_scenario_is_executed_before_each_scenario(): ...@@ -106,3 +107,27 @@ def test_after_each_scenario_is_executed_before_each_scenario():
['before', 'during', 'after', 'before', 'during', 'after'] ['before', 'during', 'after', 'before', 'during', 'after']
) )
def test_after_each_feature_is_executed_before_each_feature():
"terrain.before.each_feature and terrain.after.each_feature decorators"
world.feature_steps = []
@before.each_feature
def set_state_to_before(feature):
world.feature_steps.append('before')
@step('append "during" to states')
def append_during_to_feature_steps():
world.feature_steps.append("during")
@after.each_feature
def set_state_to_after(feature):
world.feature_steps.append('after')
feature = Feature.from_string(FEATURE2)
feature.run()
assert_equals(
world.feature_steps,
['before', 'during', 'during', 'after']
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment