Esempio n. 1
0
    def test_ensure_unicode_string_handles_nonascii_exception_message(self):
        message = u'\u2013'
        encoded_message = message.encode('utf-8')
        ex = Exception(encoded_message)

        string = ensure_unicode_string(ex)
        if sys.version_info >= (3, ):
            message = unicode_type(encoded_message)
        self.assertEqual(string, message)
Esempio n. 2
0
    def test_ensure_unicode_string_handles_nonascii_exception_message(self):
        message = u'\u2013'
        encoded_message = message.encode('utf-8')
        ex = Exception(encoded_message)

        string = ensure_unicode_string(ex)
        if sys.version_info >= (3,):
            message = unicode_type(encoded_message)
        self.assertEqual(string, message)
Esempio n. 3
0
 def test_ensure_unicode_string_handles_various_strings(
     self,
     string,
     expected_unicode_string,
 ):
     unicode_string = ensure_unicode_string(string)
     if sys.version_info >= (3, ):
         expected_unicode_string = unicode_type(string)
     self.assertTrue(isinstance(unicode_string, unicode_type))
     self.assertTrue(expected_unicode_string in unicode_string)
Esempio n. 4
0
 def test_ensure_unicode_string_handles_various_strings(
         self,
         string,
         expected_unicode_string,
 ):
     unicode_string = ensure_unicode_string(string)
     if sys.version_info >= (3,):
         expected_unicode_string = unicode_type(string)
     self.assertTrue(isinstance(unicode_string, unicode_type))
     self.assertTrue(expected_unicode_string in unicode_string)
Esempio n. 5
0
 def test_ensure_unicode_string_handles_various_strings(
         self,
         string,
         expected_unicode_string,
 ):
     unicode_string = ensure_unicode_string(string)
     if sys.version_info.major >= 3:
         expected_unicode_string = unicode_type(string)
     self.assertIsInstance(unicode_string, unicode_type)
     self.assertIn(expected_unicode_string, unicode_string)
Esempio n. 6
0
 def test_ensure_unicode_string_handles_various_strings(
     self,
     string,
     expected_unicode_string,
 ):
     unicode_string = ensure_unicode_string(string)
     if sys.version_info.major >= 3:
         expected_unicode_string = unicode_type(string)
     self.assertIsInstance(unicode_string, unicode_type)
     self.assertIn(expected_unicode_string, unicode_string)
    def test_flaky_plugin_exits_after_false_rerun_filter(
        self,
        flaky_test,
        flaky_plugin,
        call_info,
        string_io,
        mock_io,
        mock_error,
        mock_plugin_rerun,
    ):
        err_tuple = (mock_error.type, mock_error.value, mock_error.traceback)

        def rerun_filter(err, name, test, plugin):
            assert err == err_tuple
            assert name == flaky_test.name
            assert test is flaky_test
            assert plugin is flaky_plugin
            return False

        flaky(rerun_filter=rerun_filter)(flaky_test)
        call_info.when = 'call'

        actual_plugin_handles_failure = flaky_plugin.add_failure(
            call_info,
            flaky_test,
            mock_error,
        )
        assert actual_plugin_handles_failure is False
        assert not mock_plugin_rerun()

        string_io.writelines([
            self._test_method_name,
            ' failed and was not selected for rerun.',
            '\n\t',
            unicode_type(mock_error.type),
            '\n\t',
            unicode_type(mock_error.value),
            '\n\t',
            unicode_type(mock_error.traceback),
            '\n',
        ])
        assert string_io.getvalue() == mock_io.getvalue()
    def test_flaky_plugin_exits_after_false_rerun_filter(
            self,
            flaky_test,
            flaky_plugin,
            call_info,
            string_io,
            mock_io,
            mock_error,
            mock_plugin_rerun,
    ):
        err_tuple = (mock_error.type, mock_error.value, mock_error.traceback)

        def rerun_filter(err, name, test, plugin):
            assert err == err_tuple
            assert name == flaky_test.name
            assert test is flaky_test
            assert plugin is flaky_plugin
            return False

        flaky(rerun_filter=rerun_filter)(flaky_test)
        call_info.when = 'call'

        actual_plugin_handles_failure = flaky_plugin.add_failure(
            call_info,
            flaky_test,
            mock_error,
        )
        assert actual_plugin_handles_failure is False
        assert not mock_plugin_rerun()

        string_io.writelines([
            self._test_method_name,
            ' failed and was not selected for rerun.',
            '\n\t',
            unicode_type(mock_error.type),
            '\n\t',
            unicode_type(mock_error.value),
            '\n\t',
            unicode_type(mock_error.traceback),
            '\n',
        ])
        assert string_io.getvalue() == mock_io.getvalue()
Esempio n. 9
0
    def _test_flaky_plugin_handles_failure_or_error(
        self,
        current_errors=None,
        current_passes=0,
        current_runs=0,
        is_failure=False,
        is_test_method=True,
        max_runs=2,
        min_passes=1,
    ):
        self._assert_flaky_plugin_configured()
        self._expect_test_flaky(is_test_method, max_runs, min_passes)
        if current_errors is None:
            current_errors = [self._mock_error]
        else:
            current_errors.append(self._mock_error)
        self._set_flaky_attribute(FlakyNames.CURRENT_ERRORS, current_errors)
        self._set_flaky_attribute(FlakyNames.CURRENT_PASSES, current_passes)
        self._set_flaky_attribute(FlakyNames.CURRENT_RUNS, current_runs)

        retries_remaining = current_runs + 1 < max_runs
        too_few_passes = current_passes < min_passes
        expected_plugin_handles_failure = too_few_passes and retries_remaining
        did_plugin_retry_test = max_runs > 1

        self._flaky_plugin.prepareTestCase(self._mock_test_case)
        if is_failure:
            actual_plugin_handles_failure = self._flaky_plugin.handleFailure(self._mock_test_case, self._mock_error)
        else:
            actual_plugin_handles_failure = self._flaky_plugin.handleError(self._mock_test_case, self._mock_error)

        self.assertEqual(
            expected_plugin_handles_failure,
            actual_plugin_handles_failure,
            "Expected plugin{0} to handle the test run, but it did{1}.".format(
                " to" if expected_plugin_handles_failure else "", "" if actual_plugin_handles_failure else " not"
            ),
        )
        self._assert_flaky_attributes_contains(
            {FlakyNames.CURRENT_RUNS: current_runs + 1, FlakyNames.CURRENT_ERRORS: tuple(current_errors)}
        )
        expected_test_case_calls = [mock.call.address(), mock.call.address()]
        expected_result_calls = []
        if expected_plugin_handles_failure:
            expected_test_case_calls.append(("__hash__",))
            expected_stream_calls = [
                mock.call.writelines(
                    [
                        self._mock_test_method_name,
                        " failed ({0} runs remaining out of {1}).".format(max_runs - current_runs - 1, max_runs),
                        "\n\t",
                        unicode_type(self._mock_error[0]),
                        "\n\t",
                        unicode_type(self._mock_error[1]),
                        "\n\t",
                        unicode_type(self._mock_error[2]),
                        "\n",
                    ]
                )
            ]
        else:
            if did_plugin_retry_test:
                if is_failure:
                    expected_result_calls.append(mock.call.addFailure(self._mock_test_case, self._mock_error))
                else:
                    expected_result_calls.append(mock.call.addError(self._mock_test_case, self._mock_error))
            expected_stream_calls = [
                mock.call.writelines(
                    [
                        self._mock_test_method_name,
                        " failed; it passed {0} out of the required {1} times.".format(current_passes, min_passes),
                        "\n\t",
                        unicode_type(self._mock_error[0]),
                        "\n\t",
                        unicode_type(self._mock_error[1]),
                        "\n\t",
                        unicode_type(self._mock_error[2]),
                        "\n",
                    ]
                )
            ]
        self.assertEqual(self._mock_nose_result.mock_calls, expected_result_calls)
        self.assertEqual(
            self._mock_test_case.mock_calls,
            expected_test_case_calls,
            "Unexpected TestCase calls: {0} vs {1}".format(self._mock_test_case.mock_calls, expected_test_case_calls),
        )
        self.assertEqual(self._mock_stream.mock_calls, expected_stream_calls)
Esempio n. 10
0
    def _test_flaky_plugin_handles_failure(
        self,
        test,
        plugin,
        info,
        stream,
        mock_stream,
        mock_error,
        current_errors=None,
        current_passes=0,
        current_runs=0,
        max_runs=2,
        min_passes=1,
        rerun_filter=None,
    ):
        flaky(max_runs, min_passes, rerun_filter)(test)
        if current_errors is None:
            current_errors = [None]
        else:
            current_errors.append(None)
        setattr(
            test,
            FlakyNames.CURRENT_ERRORS,
            current_errors,
        )
        setattr(
            test,
            FlakyNames.CURRENT_PASSES,
            current_passes,
        )
        setattr(
            test,
            FlakyNames.CURRENT_RUNS,
            current_runs,
        )

        too_few_passes = current_passes < min_passes
        retries_remaining = current_runs + 1 < max_runs
        expected_plugin_handles_failure = too_few_passes and retries_remaining

        info.when = 'call'
        actual_plugin_handles_failure = plugin.add_failure(
            info,
            test,
            mock_error,
        )

        assert expected_plugin_handles_failure == actual_plugin_handles_failure
        self._assert_flaky_attributes_contains(
            {
                FlakyNames.CURRENT_RUNS: current_runs + 1,
                FlakyNames.CURRENT_ERRORS: current_errors
            },
            test,
        )
        if expected_plugin_handles_failure:
            stream.writelines([
                self._test_method_name,
                ' failed ({0} runs remaining out of {1}).'.format(
                    max_runs - current_runs - 1, max_runs),
                '\n\t',
                unicode_type(mock_error.type),
                '\n\t',
                unicode_type(mock_error.value),
                '\n\t',
                unicode_type(mock_error.traceback),
                '\n',
            ])
        else:
            message = ' failed; it passed {0} out of the required {1} times.'
            stream.writelines([
                self._test_method_name,
                message.format(current_passes, min_passes),
                '\n\t',
                unicode_type(mock_error.type),
                '\n\t',
                unicode_type(mock_error.value),
                '\n\t',
                unicode_type(mock_error.traceback),
                '\n',
            ])
        assert stream.getvalue() == mock_stream.getvalue()
Esempio n. 11
0
    def _test_flaky_plugin_handles_failure_or_error(
        self,
        current_errors=None,
        current_passes=0,
        current_runs=0,
        is_failure=False,
        is_test_method=True,
        max_runs=2,
        min_passes=1,
    ):
        self._expect_test_flaky(is_test_method, max_runs, min_passes)
        if current_errors is None:
            current_errors = [self._mock_error]
        else:
            current_errors.append(self._mock_error)
        self._set_flaky_attribute(
            is_test_method,
            FlakyNames.CURRENT_ERRORS,
            current_errors
        )
        self._set_flaky_attribute(
            is_test_method,
            FlakyNames.CURRENT_PASSES,
            current_passes
        )
        self._set_flaky_attribute(
            is_test_method,
            FlakyNames.CURRENT_RUNS,
            current_runs
        )

        too_few_passes = current_passes < min_passes
        retries_remaining = current_runs + 1 < max_runs
        expected_plugin_handles_failure = too_few_passes and retries_remaining

        self._flaky_plugin.prepareTestCase(self._mock_test_case)
        if is_failure:
            actual_plugin_handles_failure = self._flaky_plugin.handleFailure(
                self._mock_test_case,
                self._mock_error,
            )
        else:
            actual_plugin_handles_failure = self._flaky_plugin.handleError(
                self._mock_test_case,
                self._mock_error,
            )

        self.assertEqual(
            expected_plugin_handles_failure,
            actual_plugin_handles_failure,
            'Expected plugin{0} to handle the test run, but it did{1}.'.format(
                ' to' if expected_plugin_handles_failure else '',
                '' if actual_plugin_handles_failure else ' not'
            )
        )
        self._assert_flaky_attributes_contains(
            {
                FlakyNames.CURRENT_RUNS: current_runs + 1,
                FlakyNames.CURRENT_ERRORS: tuple(current_errors),
            }
        )
        expected_test_case_calls = [call.address(), call.address()]
        if expected_plugin_handles_failure:
            expected_test_case_calls.append(call.run(self._mock_test_result))
            expected_stream_calls = [call.writelines([
                self._mock_test_method_name,
                ' failed ({0} runs remaining out of {1}).'.format(
                    max_runs - current_runs - 1, max_runs
                ),
                '\n\t',
                unicode_type(self._mock_error[0]),
                '\n\t',
                unicode_type(self._mock_error[1]),
                '\n\t',
                unicode_type(self._mock_error[2]),
                '\n',
            ])]
        else:
            expected_stream_calls = [call.writelines([
                self._mock_test_method_name,
                ' failed; it passed {0} out of the required {1} times.'.format(
                    current_passes,
                    min_passes
                ),
                '\n\t',
                unicode_type(self._mock_error[0]),
                '\n\t',
                unicode_type(self._mock_error[1]),
                '\n\t',
                unicode_type(self._mock_error[2]),
                '\n'
            ])]
        self.assertEqual(
            self._mock_test_case.mock_calls,
            expected_test_case_calls,
            'Unexpected TestCase calls: {0} vs {1}'.format(
                self._mock_test_case.mock_calls,
                expected_test_case_calls
            )
        )
        self.assertEqual(self._mock_stream.mock_calls, expected_stream_calls)
Esempio n. 12
0
    def _test_flaky_plugin_handles_failure(
        self,
        test,
        plugin,
        info,
        stream,
        mock_stream,
        mock_error,
        current_errors=None,
        current_passes=0,
        current_runs=0,
        max_runs=2,
        min_passes=1,
    ):
        flaky(max_runs, min_passes)(test)
        if current_errors is None:
            current_errors = [None]
        else:
            current_errors.append(None)
        setattr(
            test,
            FlakyNames.CURRENT_ERRORS,
            current_errors,
        )
        setattr(
            test,
            FlakyNames.CURRENT_PASSES,
            current_passes,
        )
        setattr(
            test,
            FlakyNames.CURRENT_RUNS,
            current_runs,
        )

        too_few_passes = current_passes < min_passes
        retries_remaining = current_runs + 1 < max_runs
        expected_plugin_handles_failure = too_few_passes and retries_remaining

        info.when = 'call'
        actual_plugin_handles_failure = plugin.add_failure(
            info,
            test,
            mock_error,
        )

        assert expected_plugin_handles_failure == actual_plugin_handles_failure
        self._assert_flaky_attributes_contains(
            {
                FlakyNames.CURRENT_RUNS: current_runs + 1,
                FlakyNames.CURRENT_ERRORS: current_errors
            },
            test,
        )
        if expected_plugin_handles_failure:
            stream.writelines([
                self._test_method_name,
                ' failed ({0} runs remaining out of {1}).'.format(
                    max_runs - current_runs - 1, max_runs
                ),
                '\n\t',
                unicode_type(mock_error.type),
                '\n\t',
                unicode_type(mock_error.value),
                '\n\t',
                unicode_type(mock_error.traceback),
                '\n',
            ])
        else:
            message = ' failed; it passed {0} out of the required {1} times.'
            stream.writelines([
                self._test_method_name,
                message.format(
                    current_passes,
                    min_passes
                ),
                '\n\t',
                unicode_type(mock_error.type),
                '\n\t',
                unicode_type(mock_error.value),
                '\n\t',
                unicode_type(mock_error.traceback),
                '\n',
            ])
        assert stream.getvalue() == mock_stream.getvalue()
Esempio n. 13
0
    def _test_flaky_plugin_handles_failure_or_error(
        self,
        current_errors=None,
        current_passes=0,
        current_runs=0,
        is_failure=False,
        is_test_method=True,
        max_runs=2,
        min_passes=1,
    ):
        self._expect_test_flaky(is_test_method, max_runs, min_passes)
        if current_errors is None:
            current_errors = [self._mock_error]
        else:
            current_errors.append(self._mock_error)
        self._set_flaky_attribute(
            FlakyNames.CURRENT_ERRORS,
            current_errors,
        )
        self._set_flaky_attribute(
            FlakyNames.CURRENT_PASSES,
            current_passes,
        )
        self._set_flaky_attribute(
            FlakyNames.CURRENT_RUNS,
            current_runs,
        )

        retries_remaining = current_runs + 1 < max_runs
        too_few_passes = current_passes < min_passes
        expected_plugin_handles_failure = too_few_passes and retries_remaining

        self._flaky_plugin.prepareTestCase(self._mock_test_case)
        if is_failure:
            actual_plugin_handles_failure = self._flaky_plugin.handleFailure(
                self._mock_test_case,
                self._mock_error,
            )
        else:
            actual_plugin_handles_failure = self._flaky_plugin.handleError(
                self._mock_test_case,
                self._mock_error,
            )

        self.assertEqual(
            expected_plugin_handles_failure,
            actual_plugin_handles_failure,
            'Expected plugin{0} to handle the test run, but it did{1}.'.format(
                ' to' if expected_plugin_handles_failure else '',
                '' if actual_plugin_handles_failure else ' not'
            ),
        )
        self._assert_flaky_attributes_contains(
            {
                FlakyNames.CURRENT_RUNS: current_runs + 1,
                FlakyNames.CURRENT_ERRORS: tuple(current_errors),
            },
        )
        expected_test_case_calls = [call.address(), call.address()]
        expected_result_calls = []
        if expected_plugin_handles_failure:
            expected_test_case_calls.append(('__hash__',))
            expected_stream_calls = [call.writelines([
                self._mock_test_method_name,
                ' failed ({0} runs remaining out of {1}).'.format(
                    max_runs - current_runs - 1, max_runs
                ),
                '\n\t',
                unicode_type(self._mock_error[0]),
                '\n\t',
                unicode_type(self._mock_error[1]),
                '\n\t',
                unicode_type(self._mock_error[2]),
                '\n',
            ])]
        else:
            if is_failure:
                expected_result_calls.append(
                    call.addFailure(self._mock_test_case, self._mock_error),
                )
            else:
                expected_result_calls.append(
                    call.addError(self._mock_test_case, self._mock_error),
                )
            expected_stream_calls = [call.writelines([
                self._mock_test_method_name,
                ' failed; it passed {0} out of the required {1} times.'.format(
                    current_passes,
                    min_passes
                ),
                '\n\t',
                unicode_type(self._mock_error[0]),
                '\n\t',
                unicode_type(self._mock_error[1]),
                '\n\t',
                unicode_type(self._mock_error[2]),
                '\n'
            ])]
        self.assertEqual(
            self._mock_nose_result.mock_calls,
            expected_result_calls,
        )
        self.assertEqual(
            self._mock_test_case.mock_calls,
            expected_test_case_calls,
            'Unexpected TestCase calls: {0} vs {1}'.format(
                self._mock_test_case.mock_calls,
                expected_test_case_calls
            )
        )
        self.assertEqual(self._mock_stream.mock_calls, expected_stream_calls)