示例#1
0
    def test_can_run_feature_with_multiple_backgrounds(self):
        @step('background step ([0-9]+)')
        def background_step_number(step, number):
            my_world.background_number = number

        @step('scenario step ([0-9]+)')
        def scenario_step_number(step, number):
            my_world.background_number | should | be_equal_to(number)
            my_world.steps_run.append(int(number))

        feature = load_feature('''
        Feature: with background
          Background: 1 present
            Given a background step 1
          Scenario: with background 1
            And a scenario step 1
          Background: 2 present
            Given a background step 2
          Scenario: with background 2
            And a scenario step 2
        ''')
        my_world = World()
        my_world.steps_run = []
        result = unittest.TestResult()

        feature.run(result)

        result.testsRun | should | be(2)
        result.wasSuccessful() | should | be(True)
        my_world.steps_run | should | be_equal_to([1, 2])
示例#2
0
    def test_can_run_feature_with_scenario_outline_with_hashes(self):
        @step('a hash')
        def a_hash(step):
            my_world.run.append(list(step.hashes))

        feature = load_feature('''
        Feature: with multiline scenarnio
          Scenario Outline: follows
            Given a hash
              | <key>   | value         |
              | the     | <placeholder> |
          Examples:
            | <key> | <placeholder> |
            | key   | first         |
            | but   | second        |
        ''')
        my_world = World()
        my_world.run = []
        result = unittest.TestResult()

        feature.run(result)

        result.testsRun | should | be(2)
        result.wasSuccessful() | should | be(True)
        my_world.run | should | each_be_equal_to([
            [dict(key='the', value='first')],
            [dict(but='the', value='second')],
        ])
示例#3
0
    def test_can_run_feature_with_scenario_outline_with_background(self):
        @step('a (.*)')
        def a_something(step, value):
            my_world.run.append(value)

        feature = load_feature('''
        Feature: with multiline scenarnio
          Background: with placeholder
            Given a <placeholder>
          Scenario Outline: follows
            And a step
          Examples:
            | <placeholder> |
            | first         |
            | second        |
        ''')
        my_world = World()
        my_world.run = []
        result = unittest.TestResult()

        feature.run(result)

        result.testsRun | should | be(2)
        result.wasSuccessful() | should | be(True)
        my_world.run | should | each_be_equal_to([
            'first',
            'step',
            'second',
            'step',
        ])
示例#4
0
    def test_can_run_feature(self):
        @step('there is a step')
        def there_is_a_step(step):
            my_world.there_is_a_step = True

        @step('another step')
        def another_step(step):
            my_world.another_step = True

        @step('steps afterwards')
        def steps_afterwards(step):
            my_world.steps_afterwards = True

        feature = load_feature('''
        Feature: run a feature
          Scenario: some steps
            Given there is a step
            And another step
            When I add something undefined
            Then steps afterwards are not run
        ''')
        my_world = World()
        my_world.there_is_a_step = False
        my_world.another_step = False
        my_world.steps_afterwards = False
        result = unittest.TestResult()

        feature.run(result)

        len(result.skipped) | should | be(1)
        result.skipped[0][1] | should | start_with('pending 1 step(s):')
        run = my_world.there_is_a_step, my_world.another_step, my_world.steps_afterwards
        run | should | be_equal_to((True, True, False))
示例#5
0
    def test_can_run_feature_with_scenario_outline_with_multiline(self):
        @step('a multiline')
        def a_multiline(step):
            my_world.run.append(step.multiline)

        feature = load_feature('''
        Feature: with multiline scenarnio
          Scenario Outline: follows
            Given a multiline
              """
              with <placeholder>
              """
          Examples:
            | <placeholder> |
            | first         |
            | second        |
        ''')
        my_world = World()
        my_world.run = []
        result = unittest.TestResult()

        feature.run(result)

        result.testsRun | should | be(2)
        result.wasSuccessful() | should | be(True)
        my_world.run | should | each_be_equal_to([
            'with first\n',
            'with second\n',
        ])
示例#6
0
    def test_can_run_feature_with_scenario_outline_and_examples(self):
        @step('a (.*) with (.*)')
        def a_key_with_value(step, key, value):
            my_world.run.append((key, value))

        feature = load_feature('''
        Feature: with multiline scenarnio
          Scenario Outline: follows
            Given a <key> with <value>
          Examples:
            | key   | value     |
            | key 1 | value 1   |
            | key 2 | value 2   |
        ''')
        my_world = World()
        my_world.run = []
        result = unittest.TestResult()

        feature.run(result)

        result.testsRun | should | be(2)
        result.wasSuccessful() | should | be(True)
        my_world.run | should | each_be_equal_to([
            ('key 1', 'value 1'),
            ('key 2', 'value 2'),
        ])
示例#7
0
    def test_can_run_feature_with_hashes_in_background_step(self):
        @step('step with hashes')
        def step_with_hashes(step):
            my_world.hashes = step.hashes

        @step('here it is')
        def here_it_is(step):
            pass

        feature = load_feature('''
        Feature: with multiline scenarnio
          Background: with multiline step
            Given a step with hashes
              | first   | second    | third     |
              | first 1 | second 1  | third 1   |
              | first 2 | second 2  | third 2   |
          Scenario: with defined step
            And here it is
        ''')
        my_world = World()
        my_world.hashes = None
        result = unittest.TestResult()

        feature.run(result)

        result.testsRun | should | be(1)
        result.wasSuccessful() | should | be(True)
        my_world.hashes | should | each_be_equal_to([
            dict(first='first 1', second='second 1', third='third 1'),
            dict(first='first 2', second='second 2', third='third 2'),
        ])
示例#8
0
 def test_marks_scenarios_without_steps_as_skipped(self):
     feature = loader.load_feature('''
     Feature: Load feature file
       Scenario: pending
     ''')
     result = unittest.TestResult()
     feature.run(result)
     len(result.skipped) | should | be(1)
     result.skipped[0][1] | should | be_equal_to('no steps defined')
示例#9
0
    def run_feature_with_result_step_handlers(self, feature, *handlers):
        result = unittest.TestResult()
        for handler in ['startStep', 'stopStep'] + list(handlers):
            setattr(result, handler, mock.Mock(handler))

        feature.run(result)

        result.testsRun | should | be(1)
        result.startStep.call_count | should | be(1)
        result.stopStep.call_count | should | be(1)
        return result
示例#10
0
    def test_makes_itself_accessible_through_world(self):
        @step('feature attribute is set to "(.*)"')
        def feature_attribute(step, name):
            step.world.feature | should | be_instance_of(FeatureTest)
            step.world.feature.__class__.__name__ | should | be_equal_to(name)

        feature = load_feature('''
        Feature: accessible through world
          Scenario: test
            Then the feature attribute is set to "Feature_accessible_through_world"
        ''')
        result = unittest.TestResult()

        feature.run(result)

        result.testsRun | should | be(1)
        result.wasSuccessful() | should | be(True)
示例#11
0
    def test_clears_world_between_scenarios(self):
        @step('set a world var')
        def set_world(step):
            step.world.var = 'set'

        @step('check that world var')
        def check_var(step):
            getattr(step.world, 'var', None) | should | be(None)

        feature = load_feature('''
        Feature: clears world between scenarios
          Scenario: first
            When I set a world var
          Scenario: second
            Then I check that world var
        ''')
        result = unittest.TestResult()

        feature.run(result)

        result.testsRun | should | be(2)
        result.wasSuccessful() | should | be(True)
示例#12
0
    def test_can_run_feature_with_multiline_step(self):
        @step('multiline step')
        def multiline_step(step):
            my_world.multiline = step.multiline

        feature = load_feature('''
        Feature: with multiline scenarnio
          Scenario: with multiline step
            Given a multiline step
              """
              multiline content
              """
        ''')
        my_world = World()
        my_world.multiline = None
        result = unittest.TestResult()

        feature.run(result)

        result.testsRun | should | be(1)
        result.wasSuccessful() | should | be(True)
        my_world.multiline | should | be_equal_to('multiline content\n')
示例#13
0
    def test_can_provide_custom_world_class(self):
        class MyWorld(World):
            pass

        class MyFeature(unittest.TestCase):
            World = MyWorld

        @step('world is an instance of the MyWorld class')
        def world_is_instance_of(step):
            step.world | should | be_instance_of(MyWorld)

        feature = load_feature('''
        Feature: custom world class
          Scenario: test
            Then world is an instance of the MyWorld class
        ''',
                               test_case_class=MyFeature)
        result = unittest.TestResult()

        feature.run(result)

        result.testsRun | should | be(1)
        result.wasSuccessful() | should | be(True)
示例#14
0
 def test_marks_Feature_as_skipped_when_no_scenarios_are_defined(self):
     feature = loader.load_feature('Feature:')
     result = unittest.TestResult()
     feature.run(result)
     len(result.skipped) | should | be(1)
     result.skipped[0][1] | should | be_equal_to('no scenarios defined')