Exemple #1
0
def test_after_each_all_is_executed_before_each_all():
    "terrain.before.each_all and terrain.after.each_all decorators"
    import lettuce
    from lettuce.fs import FeatureLoader, find_base_path
    world.all_steps = []

    mox = Mox()

    loader_mock = mox.CreateMock(FeatureLoader)
    mox.StubOutWithMock(lettuce.sys, 'path')
    mox.StubOutWithMock(lettuce, 'fs')
    mox.StubOutWithMock(lettuce.fs, 'FileSystem')
    mox.StubOutWithMock(lettuce, 'Feature')
    mox.StubOutWithMock(lettuce.fs, 'find_base_path')

    lettuce.fs.FeatureLoader('some_basepath').AndReturn(loader_mock)

    lettuce.sys.path.insert(0, 'some_basepath')
    lettuce.sys.path.remove('some_basepath')

    lettuce.fs.find_base_path('some_basepath').AndReturn(( 'some_basepath', None ))
    loader_mock.load_feature_files(None).AndReturn([])
    loader_mock.find_and_load_step_definitions()
    loader_mock.find_feature_files().AndReturn(['some_basepath/foo.feature'])
    lettuce.Feature.from_file('some_basepath/foo.feature'). \
        AndReturn(Feature.from_string(FEATURE2))

    mox.ReplayAll()

    runner = lettuce.Runner('some_basepath')
    CALLBACK_REGISTRY.clear()

    @before.all
    def set_state_to_before():
        world.all_steps.append('before')

    @step('append "during" to states')
    def append_during_to_all_steps(step):
        world.all_steps.append("during")

    @after.all
    def set_state_to_after(total):
        world.all_steps.append('after')
        isinstance(total, TotalResult)

    runner.run()

    mox.VerifyAll()

    assert_equals(
        world.all_steps,
        ['before', 'during', 'during', 'after'],
    )

    mox.UnsetStubs()
Exemple #2
0
def test_after_each_all_is_executed_before_each_all():
    "terrain.before.each_all and terrain.after.each_all decorators"
    import lettuce
    from lettuce.fs import FeatureLoader
    world.all_steps = []

    loader_mock = mock.Mock(spec=FeatureLoader)
    old_sys_path = lettuce.sys.path
    old_fs = lettuce.fs
    old_FileSystem = lettuce.fs.FileSystem
    old_Feature = lettuce.Feature

    lettuce.sys.path = mock.Mock(spec=old_sys_path)
    lettuce.sys.path.insert = mock.Mock()
    lettuce.sys.path.remove = mock.Mock()
    lettuce.fs = mock.Mock(spec=old_fs)
    lettuce.fs.FileSystem = mock.Mock(spec=old_FileSystem)
    lettuce.Feature = mock.Mock(spec=old_Feature)

    loader_mock.find_feature_files = mock.Mock(return_value=['some_basepath/foo.feature'])
    lettuce.fs.FeatureLoader = mock.Mock(return_value=loader_mock)    
    lettuce.Feature.from_file = mock.Mock(return_value=Feature.from_string(FEATURE2))

    runner = lettuce.Runner('some_basepath')
    CALLBACK_REGISTRY.clear()

    @before.all
    def set_state_to_before():
        world.all_steps.append('before')

    @step('append "during" to states')
    def append_during_to_all_steps(step):
        world.all_steps.append("during")

    @after.all
    def set_state_to_after(total):
        world.all_steps.append('after')
        isinstance(total, TotalResult)

    runner.run()

    lettuce.sys.path.insert.assert_called_with(0, 'some_basepath')
    lettuce.sys.path.remove.assert_called_with('some_basepath')
    loader_mock.find_and_load_step_definitions.assert_called_once
    lettuce.Feature.from_file.assert_called_once_with('some_basepath/foo.feature')

    assert_equals(
        world.all_steps,
        ['before', 'during', 'during', 'after'],
    )

    lettuce.sys.path = old_sys_path
    lettuce.fs = old_fs
    lettuce.fs.FileSystem = old_FileSystem
    lettuce.Feature = old_Feature
Exemple #3
0
def test_after_each_all_is_executed_before_each_all():
    "terrain.before.each_all and terrain.after.each_all decorators"
    import lettuce
    from lettuce.fs import FeatureLoader
    world.all_steps = []

    mox = Mox()

    loader_mock = mox.CreateMock(FeatureLoader)
    mox.StubOutWithMock(lettuce.sys, 'path')
    mox.StubOutWithMock(lettuce, 'fs')
    mox.StubOutWithMock(lettuce.fs, 'FileSystem')
    mox.StubOutWithMock(lettuce, 'Feature')

    lettuce.fs.FeatureLoader('some_basepath').AndReturn(loader_mock)

    lettuce.sys.path.insert(0, 'some_basepath')
    lettuce.sys.path.remove('some_basepath')

    loader_mock.find_and_load_step_definitions()
    loader_mock.find_feature_files().AndReturn(['some_basepath/foo.feature'])
    lettuce.Feature.from_file('some_basepath/foo.feature'). \
        AndReturn(Feature.from_string(FEATURE2))

    mox.ReplayAll()

    runner = lettuce.Runner('some_basepath')
    CALLBACK_REGISTRY.clear()

    @before.all
    def set_state_to_before():
        world.all_steps.append('before')

    @step('append "during" to states')
    def append_during_to_all_steps(step):
        world.all_steps.append("during")

    @after.all
    def set_state_to_after(total):
        world.all_steps.append('after')
        isinstance(total, TotalResult)

    runner.run()

    mox.VerifyAll()

    assert_equals(
        world.all_steps,
        ['before', 'during', 'during', 'after'],
    )

    mox.UnsetStubs()
Exemple #4
0
        def inner(self, callback, name=None):
            if not callable(callback):
                name = callback
                return lambda callback: inner(self, callback, name=name)

            CALLBACK_REGISTRY.append_to(
                where,
                when.format(name=self.name),
                function=callback,
                name=name,
            )

            return callback
Exemple #5
0
 def each_feature(cls, function):
     CALLBACK_REGISTRY.append_to('feature', "%s_each" % cls.__name__, function)
     return function
Exemple #6
0
 def each_scenario(cls, function):
     CALLBACK_REGISTRY.append_to('scenario', "%s_each" % cls.__name__, function)
     return function
Exemple #7
0
 def outline(cls, function):
     CALLBACK_REGISTRY.append_to('scenario', "outline", function)
     return function
Exemple #8
0
 def all(cls, function):
     CALLBACK_REGISTRY.append_to('all', cls.__name__, function)
     return function
Exemple #9
0
 def each_step(cls, function):
     CALLBACK_REGISTRY.append_to('step', "%s_each" % cls.__name__, function)
     return function
Exemple #10
0
 def runserver(cls, function):
     CALLBACK_REGISTRY.append_to('runserver', cls.__name__, function)
     return function
Exemple #11
0
 def method(self, fn):
     CALLBACK_REGISTRY.append_to(where, when % {'0': self.name}, fn)
     return fn
 def method(self, fn):
     #print name, where, when
     #print fn.func_code
     CALLBACK_REGISTRY.append_to(where, when % {'0': self.name}, fn)
     return fn
Exemple #13
0
 def outline(cls, function):
     CALLBACK_REGISTRY.append_to('scenario', "outline", function)
     return function
Exemple #14
0
 def each_app(cls, function):
     CALLBACK_REGISTRY.append_to('app', "%s_each" % cls.__name__, function)
     return function
Exemple #15
0
 def runserver(cls, function):
     CALLBACK_REGISTRY.append_to('runserver', cls.__name__, function)
     return function
Exemple #16
0
 def each_scenario(cls, function):
     CALLBACK_REGISTRY.append_to('scenario', "%s_each" % cls.__name__,
                                 function)
     return function
Exemple #17
0
 def all(cls, function):
     CALLBACK_REGISTRY.append_to('all', cls.__name__, function)
     return function
Exemple #18
0
 def handle_request(cls, function):
     CALLBACK_REGISTRY.append_to('handle_request', cls.__name__, function)
     return function
Exemple #19
0
 def method(self, fn):
     CALLBACK_REGISTRY.append_to(where, when.format(self.name), fn)
     return fn
Exemple #20
0
 def method(self, fn):
     CALLBACK_REGISTRY.append_to(where, when % {'0': self.name}, fn)
     return fn
Exemple #21
0
 def handle_request(cls, function):
     CALLBACK_REGISTRY.append_to('handle_request', cls.__name__, function)
     return function
Exemple #22
0
    def run(self):
        """ Find and load step definitions, and them find and load
        features under `base_path` specified on constructor
        """

        CALLBACK_REGISTRY.clear(name='output')
        CALLBACK_REGISTRY.clear(name='common_output')

        if self.verbosity is 0:
            from lettuce.plugins import non_verbose as output
        elif self.verbosity is 1:
            from lettuce.plugins import dots as output
        elif self.verbosity is 2:
            from lettuce.plugins import scenario_names as output
        elif self.verbosity is 3:
            from lettuce.plugins import shell_output as output
        else:
            from lettuce.plugins import colored_shell_output as output

        reload(output)

        self.output = output

        results = []
        if self.single_feature:
            features_files = [self.single_feature]
        else:
            features_files = self.loader.find_feature_files()
            if self.random:
                random.shuffle(features_files)

        if not features_files:
            self.output.print_no_features_found(self.loader.base_dir)
            return

        # only load steps if we've located some features.
        # this prevents stupid bugs when loading django modules
        # that we don't even want to test.
        try:
            self.loader.find_and_load_step_definitions()
        except StepLoadingError as e:
            print "Error loading step definitions:\n", e
            raise SystemExit(3)

        failed = False

        try:
            call_hook('before', 'all')
            for filename in features_files:
                feature = Feature.from_file(filename)
                results.append(
                    feature.run(self.scenarios,
                                tags=self.tags,
                                random=self.random,
                                failfast=self.failfast))

        except exceptions.LettuceSyntaxError as e:
            sys.stderr.write(e.msg)
            failed = True

        except exceptions.FailFast:
            print
            print ("Lettuce aborted running any more tests "
                   "because was called with the `--failfast` option")

            failed = True

        except Exception:
            # ensure our output is undiverted
            sys.stdout = real_stdout
            print "Exception in Lettuce itself!"
            failed = True
            raise

        except BaseException:
            failed = True
            raise

        finally:
            total = TotalResult(results)
            total.output_format()

            call_hook('after', 'all', total)

            if not total.passed:
                failed = True

            if failed:
                raise SystemExit(2)

            return total