示例#1
0
    def __run_class_fixtures(self, stage, fixtures, callback_on_run_event, callback_on_complete_event):
        """Set the current _stage, run a set of fixtures, calling callbacks before and after each."""
        self._stage = stage

        for fixture_method in fixtures:
            result = TestResult(fixture_method)

            try:
                self.fire_event(callback_on_run_event, result)
                result.start()
                if self.__execute_block_recording_exceptions(fixture_method, result, is_class_level=True):
                    result.end_in_success()
                else:
                    if self.__class_level_failure:
                        result.end_in_failure(self.__class_level_failure)
                        ### Bump failure count?
                        ### Something about failure_limit?
                    elif self.__class_level_error:
                        result.end_in_error(self.__class_level_error)
                        ### Bump failure count?
                        ### Something about failure_limit?
                    else:
                        raise Exception("Couldn't find a class-level failure or error even"
                            " though we failed while executing a class-level fixture."
                            " This should not be possible. Aborting.")
            except (KeyboardInterrupt, SystemExit):
                result.end_in_interruption(sys.exc_info())
                raise
            finally:
                self.fire_event(callback_on_complete_event, result)
示例#2
0
    def __run_class_fixtures(self, stage, fixtures, callback_on_run_event,
                             callback_on_complete_event):
        """Set the current _stage, run a set of fixtures, calling callbacks before and after each."""
        self._stage = stage

        for fixture_method in fixtures:
            result = TestResult(fixture_method)

            try:
                self.fire_event(callback_on_run_event, result)
                result.start()
                if self.__execute_block_recording_exceptions(
                        fixture_method, result, is_class_level=True):
                    result.end_in_success()
                else:
                    if self.__class_level_failure:
                        result.end_in_failure(self.__class_level_failure)
                        ### Bump failure count?
                        ### Something about failure_limit?
                    elif self.__class_level_error:
                        result.end_in_error(self.__class_level_error)
                        ### Bump failure count?
                        ### Something about failure_limit?
                    else:
                        raise Exception(
                            "Couldn't find a class-level failure or error even"
                            " though we failed while executing a class-level fixture."
                            " This should not be possible. Aborting.")
            except (KeyboardInterrupt, SystemExit):
                result.end_in_interruption(sys.exc_info())
                raise
            finally:
                self.fire_event(callback_on_complete_event, result)
示例#3
0
    def __run_test_methods(self, class_fixture_failures):
        """Run this class's setup fixtures / test methods / teardown fixtures.

        These are run in the obvious order - setup and teardown go before and after,
        respectively, every test method.  If there was a failure in the class_setup
        phase, no method-level fixtures or test methods will be run, and we'll eventually
        skip all the way to the class_teardown phase.   If a given test method is marked
        as disabled, neither it nor its fixtures will be run.  If there is an exception
        during the setup phase, the test method will not be run and execution
        will continue with the teardown phase.
        """
        for test_method in self.runnable_test_methods():
            result = TestResult(test_method)

            # Sometimes, test cases want to take further action based on
            # results, e.g. further clean-up or reporting if a test method
            # fails. (Yelp's Selenium test cases do this.) If you need to
            # programatically inspect test results, you should use
            # self.results().

            # NOTE: THIS IS INCORRECT -- im_self is shared among all test
            # methods on the TestCase instance. This is preserved for backwards
            # compatibility and should be removed eventually.

            try:
                # run "on-run" callbacks. e.g. print out the test method name
                self.fire_event(self.EVENT_ON_RUN_TEST_METHOD, result)

                result.start()
                self.__all_test_results.append(result)

                # first, run setup fixtures
                self._stage = self.STAGE_SETUP
                with self.__test_fixtures.instance_context() as fixture_failures:
                    # we haven't had any problems in class/instance setup, onward!
                    if not (fixture_failures + class_fixture_failures):
                        self._stage = self.STAGE_TEST_METHOD
                        result.record(test_method)
                    self._stage = self.STAGE_TEARDOWN

                # maybe something broke during teardown -- record it
                for exc_info in fixture_failures + class_fixture_failures:
                    result.end_in_failure(exc_info)

                # if nothing's gone wrong, it's not about to start
                if not result.complete:
                    result.end_in_success()

            except (KeyboardInterrupt, SystemExit):
                result.end_in_interruption(sys.exc_info())
                raise

            finally:
                self.fire_event(self.EVENT_ON_COMPLETE_TEST_METHOD, result)

                if not result.success:
                    self.failure_count += 1
                    if self.failure_limit and self.failure_count >= self.failure_limit:
                        break
示例#4
0
    def __run_test_methods(self):
        """Run this class's setup fixtures / test methods / teardown fixtures.

        These are run in the obvious order - setup and teardown go before and after,
        respectively, every test method.  If there was a failure in the class_setup
        phase, no method-level fixtures or test methods will be run, and we'll eventually
        skip all the way to the class_teardown phase.   If a given test method is marked
        as disabled, neither it nor its fixtures will be run.  If there is an exception
        during during the setup phase, the test method will not be run and execution
        will continue with the teardown phase.
        """
        for test_method in self.runnable_test_methods():

            result = TestResult(test_method)
            test_method.im_self.test_result = result

            try:
                # run "on-run" callbacks. eg/ print out the test method name
                for callback in self.__on_run_test_method_callbacks:
                    callback(self, test_method)
                result.start()

                if self.__class_level_failure:
                    result.end_in_failure(self.__class_level_failure)
                elif self.__class_level_error:
                    result.end_in_error(self.__class_level_error)
                else:
                    # first, run setup fixtures
                    self._stage = self.STAGE_SETUP
                    def _setup_block():
                        for fixture_method in self.setup_fixtures:
                            fixture_method()
                        self.__run_deprecated_fixture_method('setUp')
                    self.__execute_block_recording_exceptions(_setup_block, result)

                    # then run the test method itself, assuming setup was successful
                    self._stage = self.STAGE_TEST_METHOD
                    if not result.complete:
                        self.__execute_block_recording_exceptions(test_method, result)

                    # finally, run the teardown phase
                    self._stage = self.STAGE_TEARDOWN
                    def _teardown_block():
                        self.__run_deprecated_fixture_method('tearDown')
                        for fixture_method in self.teardown_fixtures:
                            fixture_method()
                    self.__execute_block_recording_exceptions(_teardown_block, result)

                # if nothing's gone wrong, it's not about to start
                if not result.complete:
                    result.end_in_success()
            except (KeyboardInterrupt, SystemExit):
                result.end_in_incomplete(sys.exc_info())
                for callback in self.__on_complete_test_method_callbacks:
                    callback(self, result)
                raise
            else:
                for callback in self.__on_complete_test_method_callbacks:
                    callback(self, result)
示例#5
0
    def __run_test_methods(self):
        """Run this class's setup fixtures / test methods / teardown fixtures.

        These are run in the obvious order - setup and teardown go before and after,
        respectively, every test method.  If there was a failure in the class_setup
        phase, no method-level fixtures or test methods will be run, and we'll eventually
        skip all the way to the class_teardown phase.   If a given test method is marked
        as disabled, neither it nor its fixtures will be run.  If there is an exception
        during during the setup phase, the test method will not be run and execution
        will continue with the teardown phase.
        """
        for test_method in self.runnable_test_methods():

            result = TestResult(test_method)
            test_method.im_self.test_result = result

            try:
                self._method_level = True # Flag that we're currently running method-level stuff (rather than class-level)

                # run "on-run" callbacks. eg/ print out the test method name
                for callback in self.__callbacks[self.EVENT_ON_RUN_TEST_METHOD]:
                    callback(result.to_dict())
                result.start()

                if self.__class_level_failure:
                    result.end_in_failure(self.__class_level_failure)
                elif self.__class_level_error:
                    result.end_in_error(self.__class_level_error)
                else:
                    # first, run setup fixtures
                    self._stage = self.STAGE_SETUP
                    def _setup_block():
                        for fixture_method in self.setup_fixtures + [ self.setUp ]:
                            fixture_method()
                    self.__execute_block_recording_exceptions(_setup_block, result)

                    def _run_test_block():
                        # then run the test method itself, assuming setup was successful
                        self._stage = self.STAGE_TEST_METHOD
                        if not result.complete:
                            self.__execute_block_recording_exceptions(test_method, result)

                    def _setup_teardown_block():
                        self.__enter_context_managers(self.setup_teardown_fixtures, _run_test_block)

                    # then run any setup_teardown fixtures, assuming setup was successful.
                    if not result.complete:
                        self.__execute_block_recording_exceptions(_setup_teardown_block, result)

                    # finally, run the teardown phase
                    self._stage = self.STAGE_TEARDOWN
                    def _teardown_block():
                        for fixture_method in [ self.tearDown ] + self.teardown_fixtures:
                            fixture_method()
                    self.__execute_block_recording_exceptions(_teardown_block, result)

                # if nothing's gone wrong, it's not about to start
                if not result.complete:
                    result.end_in_success()
            except (KeyboardInterrupt, SystemExit):
                result.end_in_interruption(sys.exc_info())
                raise
            finally:
                for callback in self.__callbacks[self.EVENT_ON_COMPLETE_TEST_METHOD]:
                    callback(result.to_dict())

                self._method_level = False

                if not result.success:
                    self.failure_count += 1
                    if self.failure_limit and self.failure_count >= self.failure_limit:
                        return
示例#6
0
    def __run_test_methods(self):
        """Run this class's setup fixtures / test methods / teardown fixtures.

        These are run in the obvious order - setup and teardown go before and after,
        respectively, every test method.  If there was a failure in the class_setup
        phase, no method-level fixtures or test methods will be run, and we'll eventually
        skip all the way to the class_teardown phase.   If a given test method is marked
        as disabled, neither it nor its fixtures will be run.  If there is an exception
        during the setup phase, the test method will not be run and execution
        will continue with the teardown phase.
        """
        for test_method in self.runnable_test_methods():

            result = TestResult(test_method)

            try:
                self._method_level = True  # Flag that we're currently running method-level stuff (rather than class-level)

                # run "on-run" callbacks. e.g. print out the test method name
                self.fire_event(self.EVENT_ON_RUN_TEST_METHOD, result)

                result.start()

                if self.__class_level_failure:
                    result.end_in_failure(self.__class_level_failure)
                elif self.__class_level_error:
                    result.end_in_error(self.__class_level_error)
                else:
                    # first, run setup fixtures
                    self._stage = self.STAGE_SETUP

                    def _setup_block():
                        for fixture_method in self.setup_fixtures:
                            fixture_method()

                    self.__execute_block_recording_exceptions(
                        _setup_block, result)

                    def _run_test_block():
                        # then run the test method itself, assuming setup was successful
                        self._stage = self.STAGE_TEST_METHOD
                        if not result.complete:
                            self.__execute_block_recording_exceptions(
                                test_method, result)

                    def _setup_teardown_block():
                        self.__enter_context_managers(
                            self.setup_teardown_fixtures, _run_test_block)

                    # then run any setup_teardown fixtures, assuming setup was successful.
                    if not result.complete:
                        self.__execute_block_recording_exceptions(
                            _setup_teardown_block, result)

                    # finally, run the teardown phase
                    self._stage = self.STAGE_TEARDOWN

                    def _teardown_block():
                        for fixture_method in self.teardown_fixtures:
                            fixture_method()

                    self.__execute_block_recording_exceptions(
                        _teardown_block, result)

                # if nothing's gone wrong, it's not about to start
                if not result.complete:
                    result.end_in_success()
            except (KeyboardInterrupt, SystemExit):
                result.end_in_interruption(sys.exc_info())
                raise
            finally:
                self.fire_event(self.EVENT_ON_COMPLETE_TEST_METHOD, result)
                self._method_level = False

                if not result.success:
                    self.failure_count += 1
                    if self.failure_limit and self.failure_count >= self.failure_limit:
                        return