Example #1
0
    def test_num_scenarios_passed_property(self):
        feature = Feature(
            'foobar',
            scenarios=[self.scenario_mock_one, self.scenario_mock_two])
        feature.successes = feature.scenarios

        self.assertEqual(feature.num_scenarios_passed, 2)
Example #2
0
    def test_failure_run(self, mock_context):
        mock_scenario = MagicMock(spec=Scenario)

        class ScenarioFailureMock(mock_scenario.__class__):
            def __init__(self):
                self.run_called = False
                self.run_called_with = None
                self.run_call_count = 0
                self.passed = False

            async def run(self, context):
                self.run_called = True
                self.run_called_with = context
                self.run_call_count += 1
                self.passed = False

        failure_scenario = ScenarioFailureMock()

        feature = Feature('foobar',
                          scenarios=[self.scenario_mock_one, failure_scenario],
                          before_all=self.before_all_mock,
                          after_all=self.after_all_mock,
                          before_each=self.before_each_mock,
                          after_each=self.after_each_mock)

        try:
            self.loop.run_until_complete(feature.run())
        except Exception:
            raise AssertionError
        else:

            def assert_mock_called_correctly(mock, call_count):
                self.assertTrue(mock.run_called)
                self.assertTrue(isinstance(mock.run_called_with, MagicMock))
                self.assertEqual(mock.run_call_count, call_count)

            assert_mock_called_correctly(self.before_all_mock, 1)
            assert_mock_called_correctly(self.before_each_mock, 2)
            assert_mock_called_correctly(self.after_each_mock, 2)
            assert_mock_called_correctly(self.after_all_mock, 1)

            assert_mock_called_correctly(self.scenario_mock_one, 1)
            assert_mock_called_correctly(failure_scenario, 1)

            self.assertEqual(self.call_order, [
                self.before_all_mock, self.before_each_mock,
                self.after_each_mock, self.before_each_mock,
                self.after_each_mock, self.after_all_mock
            ])

            self.assertEqual(feature.successes, [self.scenario_mock_one])
            self.assertEqual(feature.failures, [failure_scenario])
            self.assertFalse(feature.passed)
Example #3
0
 def test_init_with_invalid_scenarios(self):
     try:
         feature = Feature('foobar', scenarios=['foo', 'bar'])
     except ValidationException as e:
         self.assertEqual(
             str(e),
             "\n\nFeature 'foobar'. Scenario: 'foo' must be an initialized Scenario object. "
             "Instead got '<class 'str'>'.")
     else:
         raise AssertionError()
Example #4
0
 def test_init_with_invalid_steps(self):
     try:
         feature = Feature('foobar',
                           scenarios=[self.scenario_mock_one],
                           after_each=['foobar'])
     except ValidationException as e:
         self.assertEqual(
             str(e),
             "\n\nFeature 'foobar'. AfterEach: 'foobar' must be an initialized Step object. "
             "Instead got '<class 'str'>'. Did you call the decorated step function?"
         )
Example #5
0
    def test_exception_run(self, mock_context):
        class FooBarException(Exception):
            pass

        async def failing_function(context):
            self.call_order.append(failing_function)
            raise FooBarException

        self.before_each_mock.run = failing_function

        feature = Feature(
            'foobar',
            scenarios=[self.scenario_mock_one, self.scenario_mock_two],
            before_all=self.before_all_mock,
            after_all=self.after_all_mock,
            before_each=self.before_each_mock,
            after_each=self.after_each_mock)

        self.loop.run_until_complete(feature.run())

        self.assertEqual(type(feature.exception), FooBarException)

        def assert_mock_called_correctly(mock, call_count):
            self.assertTrue(mock.run_called)
            self.assertTrue(isinstance(mock.run_called_with, MagicMock))
            self.assertEqual(mock.run_call_count, call_count)

        assert_mock_called_correctly(self.before_all_mock, 1)
        self.assertFalse(self.after_each_mock.run_called)
        assert_mock_called_correctly(self.after_all_mock, 1)

        self.assertFalse(self.scenario_mock_one.run_called)
        self.assertFalse(self.scenario_mock_two.run_called)

        self.assertEqual(self.call_order, [
            self.before_all_mock, failing_function, failing_function,
            self.after_all_mock
        ])

        self.assertFalse(feature.passed)
Example #6
0
    def test_successful_run(self, mock_context):
        feature = Feature(
            'foobar',
            scenarios=[self.scenario_mock_one, self.scenario_mock_two],
            before_all=self.before_all_mock,
            after_all=self.after_all_mock,
            before_each=self.before_each_mock,
            after_each=self.after_each_mock)

        try:
            self.loop.run_until_complete(feature.run())
        except Exception:
            raise AssertionError
        else:

            def assert_mock_called_correctly(mock, call_count):
                self.assertTrue(mock.run_called)
                self.assertTrue(isinstance(mock.run_called_with, MagicMock))
                self.assertEqual(mock.run_call_count, call_count)

            assert_mock_called_correctly(self.before_all_mock, 1)
            assert_mock_called_correctly(self.before_each_mock, 2)
            assert_mock_called_correctly(self.after_each_mock, 2)
            assert_mock_called_correctly(self.after_all_mock, 1)

            assert_mock_called_correctly(self.scenario_mock_one, 1)
            assert_mock_called_correctly(self.scenario_mock_two, 1)

            self.assertEqual(self.call_order, [
                self.before_all_mock, self.before_each_mock,
                self.after_each_mock, self.before_each_mock,
                self.after_each_mock, self.after_all_mock
            ])

            self.assertEqual(len(feature.successes), 2)
            self.assertTrue(self.scenario_mock_one in feature.successes)
            self.assertTrue(self.scenario_mock_two in feature.successes)
            self.assertEqual(feature.failures, [])
            self.assertTrue(feature.passed)
Example #7
0
    def test_init(self):
        feature = Feature(
            'foobar',
            scenarios=[self.scenario_mock_one, self.scenario_mock_two],
            before_all=self.before_all_mock,
            after_all=self.after_all_mock,
            before_each=self.before_each_mock,
            after_each=self.after_each_mock)

        self.assertEqual(feature.description, 'foobar')
        self.assertEqual(feature.scenarios,
                         [self.scenario_mock_one, self.scenario_mock_two])
        self.assertEqual(feature.before_each, [self.before_each_mock])
        self.assertEqual(feature.before_all, [self.before_all_mock])
        self.assertEqual(feature.after_each, [self.after_each_mock])
        self.assertEqual(feature.after_all, [self.after_all_mock])
        self.assertIsNone(feature.exception)
        self.assertTrue(feature.passed)
        self.assertEqual(feature.successes, [])
        self.assertEqual(feature.failures, [])