Exemplo n.º 1
0
    def __get_module_from_str(self, modname, print_exception, pyfile):
        """ Import the module in the given import path.
            * Returns the "final" module, so importing "coilib40.subject.visu"
            returns the "visu" module, not the "coilib40" as returned by __import__ """
        try:
            mod = __import__(modname)
            for part in modname.split('.')[1:]:
                mod = getattr(mod, part)
            return mod
        except:
            if print_exception:
                from _pydev_runfiles import pydev_runfiles_xml_rpc
                from _pydevd_bundle import pydevd_io
                buf_err = pydevd_io.start_redirect(keep_original_redirection=True, std='stderr')
                buf_out = pydevd_io.start_redirect(keep_original_redirection=True, std='stdout')
                try:
                    import traceback;traceback.print_exc()
                    sys.stderr.write('ERROR: Module: %s could not be imported (file: %s).\n' % (modname, pyfile))
                finally:
                    pydevd_io.end_redirect('stderr')
                    pydevd_io.end_redirect('stdout')

                pydev_runfiles_xml_rpc.notifyTest(
                    'error', buf_out.getvalue(), buf_err.getvalue(), pyfile, modname, 0)

            return None
Exemplo n.º 2
0
    def stopTest(self, test):
        end_time = time.time()
        pydevd_io.end_redirect(std='both')

        _PythonTextTestResult.stopTest(self, test)

        captured_output = self.buf.getvalue()
        del self.buf
        error_contents = ''
        test_name = self.get_test_name(test)

        diff_time = '%.2f' % (end_time - self.start_time)

        skipped = False
        outcome = getattr(test, '_outcome', None)
        if outcome is not None:
            skipped = bool(getattr(outcome, 'skipped', None))

        if skipped:
            pydev_runfiles_xml_rpc.notifyTest('skip', captured_output,
                                              error_contents,
                                              test.__pydev_pyfile__, test_name,
                                              diff_time)
        elif not self._current_errors_stack and not self._current_failures_stack:
            pydev_runfiles_xml_rpc.notifyTest('ok', captured_output,
                                              error_contents,
                                              test.__pydev_pyfile__, test_name,
                                              diff_time)
        else:
            self._reportErrors(self._current_errors_stack,
                               self._current_failures_stack, captured_output,
                               test_name)
Exemplo n.º 3
0
    def __get_module_from_str(self, modname, print_exception, pyfile):
        """ Import the module in the given import path.
            * Returns the "final" module, so importing "coilib40.subject.visu"
            returns the "visu" module, not the "coilib40" as returned by __import__ """
        try:
            mod = __import__(modname)
            for part in modname.split('.')[1:]:
                mod = getattr(mod, part)
            return mod
        except:
            if print_exception:
                from _pydev_runfiles import pydev_runfiles_xml_rpc
                from _pydevd_bundle import pydevd_io
                buf_err = pydevd_io.start_redirect(keep_original_redirection=True, std='stderr')
                buf_out = pydevd_io.start_redirect(keep_original_redirection=True, std='stdout')
                try:
                    import traceback;traceback.print_exc()
                    sys.stderr.write('ERROR: Module: %s could not be imported (file: %s).\n' % (modname, pyfile))
                finally:
                    pydevd_io.end_redirect('stderr')
                    pydevd_io.end_redirect('stdout')

                pydev_runfiles_xml_rpc.notifyTest(
                    'error', buf_out.getvalue(), buf_err.getvalue(), pyfile, modname, 0)

            return None
Exemplo n.º 4
0
    def stopTest(self, test):
        end_time = time.time()
        pydevd_io.end_redirect(std='both')

        _PythonTextTestResult.stopTest(self, test)

        captured_output = self.buf.getvalue()
        del self.buf
        error_contents = ''
        test_name = self.get_test_name(test)


        diff_time = '%.2f' % (end_time - self.start_time)
        if not self._current_errors_stack and not self._current_failures_stack:
            pydev_runfiles_xml_rpc.notifyTest(
                'ok', captured_output, error_contents, test.__pydev_pyfile__, test_name, diff_time)
        else:
            self._reportErrors(self._current_errors_stack, self._current_failures_stack, captured_output, test_name)
Exemplo n.º 5
0
    def test_xml_rpc_communication(self):
        import sys
        sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'samples'))
        notifications = []
        class Server:

            def __init__(self, notifications):
                self.notifications = notifications

            def notifyConnected(self):
                #This method is called at the very start (in runfiles.py), and we do not check this here
                raise AssertionError('Should not be called from the run tests.')


            def notifyTestsCollected(self, number_of_tests):
                self.notifications.append(('notifyTestsCollected', number_of_tests))


            def notifyStartTest(self, file, test):
                pass

            def notifyTest(self, cond, captured_output, error_contents, file, test, time):
                try:
                    #I.e.: when marked as Binary in xml-rpc
                    captured_output = captured_output.data
                except:
                    pass
                try:
                    #I.e.: when marked as Binary in xml-rpc
                    error_contents = error_contents.data
                except:
                    pass
                if error_contents:
                    error_contents = error_contents.splitlines()[-1].strip()
                self.notifications.append(('notifyTest', cond, captured_output.strip(), error_contents, file, test))

            def notifyTestRunFinished(self, total_time):
                self.notifications.append(('notifyTestRunFinished',))

        server = Server(notifications)
        pydev_runfiles_xml_rpc.set_server(server)
        simple_test = os.path.join(self.file_dir[0], 'simple_test.py')
        simple_test2 = os.path.join(self.file_dir[0], 'simple2_test.py')
        simpleClass_test = os.path.join(self.file_dir[0], 'simpleClass_test.py')
        simpleModule_test = os.path.join(self.file_dir[0], 'simpleModule_test.py')

        files_to_tests = {}
        files_to_tests.setdefault(simple_test , []).append('SampleTest.test_xxxxxx1')
        files_to_tests.setdefault(simple_test , []).append('SampleTest.test_xxxxxx2')
        files_to_tests.setdefault(simple_test , []).append('SampleTest.test_non_unique_name')
        files_to_tests.setdefault(simple_test2, []).append('YetAnotherSampleTest.test_abc')
        files_to_tests.setdefault(simpleClass_test, []).append('SetUpClassTest.test_blank')
        files_to_tests.setdefault(simpleModule_test, []).append('SetUpModuleTest.test_blank')

        self._setup_scenario(None, files_to_tests=files_to_tests)
        self.MyTestRunner.verbosity = 2

        buf = pydevd_io.start_redirect(keep_original_redirection=False)
        try:
            self.MyTestRunner.run_tests()
            self.assertEqual(8, len(notifications))
            expected = [
                    ('notifyTestsCollected', 6),
                    ('notifyTest', 'ok', 'non unique name ran', '', simple_test, 'SampleTest.test_non_unique_name'),
                    ('notifyTest', 'fail', '', 'AssertionError: Fail test 2', simple_test, 'SampleTest.test_xxxxxx1'),
                    ('notifyTest', 'ok', '', '', simple_test, 'SampleTest.test_xxxxxx2'),
                    ('notifyTest', 'ok', '', '', simple_test2, 'YetAnotherSampleTest.test_abc'),
                ]

            if not IS_JYTHON:
                if 'samples.simpleClass_test' in str(notifications):
                    expected.append(('notifyTest', 'error', '', 'ValueError: This is an INTENTIONAL value error in setUpClass.',
                            simpleClass_test.replace('/', os.path.sep), 'samples.simpleClass_test.SetUpClassTest <setUpClass>'))
                    expected.append(('notifyTest', 'error', '', 'ValueError: This is an INTENTIONAL value error in setUpModule.',
                                simpleModule_test.replace('/', os.path.sep), 'samples.simpleModule_test <setUpModule>'))
                else:
                    expected.append(('notifyTest', 'error', '', 'ValueError: This is an INTENTIONAL value error in setUpClass.',
                            simpleClass_test.replace('/', os.path.sep), 'simpleClass_test.SetUpClassTest <setUpClass>'))
                    expected.append(('notifyTest', 'error', '', 'ValueError: This is an INTENTIONAL value error in setUpModule.',
                                simpleModule_test.replace('/', os.path.sep), 'simpleModule_test <setUpModule>'))
            else:
                expected.append(('notifyTest', 'ok', '', '', simpleClass_test, 'SetUpClassTest.test_blank'))
                expected.append(('notifyTest', 'ok', '', '', simpleModule_test, 'SetUpModuleTest.test_blank'))

            expected.append(('notifyTestRunFinished',))
            expected.sort()
            new_notifications = []
            for notification in expected:
                try:
                    if len(notification) == 6:
                        # Some are binary on Py3.
                        new_notifications.append((
                            notification[0],
                            notification[1],
                            notification[2].encode('latin1'),
                            notification[3].encode('latin1'),
                            notification[4],
                            notification[5],
                        ))
                    else:
                        new_notifications.append(notification)
                except:
                    raise
            expected = new_notifications

            notifications.sort()
            self.assertEqual(
                expected,
                notifications
            )
        finally:
            pydevd_io.end_redirect()
        b = buf.getvalue()
        if not IS_JYTHON:
            self.assert_(b.find('Ran 4 tests in ') != -1, 'Found: ' + b)
        else:
            self.assert_(b.find('Ran 6 tests in ') != -1, 'Found: ' + b)
Exemplo n.º 6
0
    def test_xml_rpc_communication(self):
        import sys
        sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'samples'))
        notifications = []

        class Server:
            def __init__(self, notifications):
                self.notifications = notifications

            def notifyConnected(self):
                #This method is called at the very start (in runfiles.py), and we do not check this here
                raise AssertionError(
                    'Should not be called from the run tests.')

            def notifyTestsCollected(self, number_of_tests):
                self.notifications.append(
                    ('notifyTestsCollected', number_of_tests))

            def notifyStartTest(self, file, test):
                pass

            def notifyTest(self, cond, captured_output, error_contents, file,
                           test, time):
                try:
                    #I.e.: when marked as Binary in xml-rpc
                    captured_output = captured_output.data
                except:
                    pass
                try:
                    #I.e.: when marked as Binary in xml-rpc
                    error_contents = error_contents.data
                except:
                    pass
                if error_contents:
                    error_contents = error_contents.splitlines()[-1].strip()
                self.notifications.append(
                    ('notifyTest', cond, captured_output.strip(),
                     error_contents, file, test))

            def notifyTestRunFinished(self, total_time):
                self.notifications.append(('notifyTestRunFinished', ))

        server = Server(notifications)
        pydev_runfiles_xml_rpc.set_server(server)
        simple_test = os.path.join(self.file_dir[0], 'simple_test.py')
        simple_test2 = os.path.join(self.file_dir[0], 'simple2_test.py')
        simpleClass_test = os.path.join(self.file_dir[0],
                                        'simpleClass_test.py')
        simpleModule_test = os.path.join(self.file_dir[0],
                                         'simpleModule_test.py')

        files_to_tests = {}
        files_to_tests.setdefault(simple_test,
                                  []).append('SampleTest.test_xxxxxx1')
        files_to_tests.setdefault(simple_test,
                                  []).append('SampleTest.test_xxxxxx2')
        files_to_tests.setdefault(simple_test,
                                  []).append('SampleTest.test_non_unique_name')
        files_to_tests.setdefault(simple_test2,
                                  []).append('YetAnotherSampleTest.test_abc')
        files_to_tests.setdefault(simpleClass_test,
                                  []).append('SetUpClassTest.test_blank')
        files_to_tests.setdefault(simpleModule_test,
                                  []).append('SetUpModuleTest.test_blank')

        self._setup_scenario(None, files_to_tests=files_to_tests)
        self.MyTestRunner.verbosity = 2

        buf = pydevd_io.start_redirect(keep_original_redirection=False)
        try:
            self.MyTestRunner.run_tests()
            self.assertEqual(8, len(notifications))
            if sys.version_info[:2] <= (2, 6):
                # The setUpClass is not supported in Python 2.6 (thus we have no collection error).
                expected = [
                    ('notifyTest', 'fail', '', 'AssertionError: Fail test 2',
                     simple_test, 'SampleTest.test_xxxxxx1'),
                    ('notifyTest', 'ok', '', '', simple_test2,
                     'YetAnotherSampleTest.test_abc'),
                    ('notifyTest', 'ok', '', '', simpleClass_test,
                     'SetUpClassTest.test_blank'),
                    ('notifyTest', 'ok', '', '', simpleModule_test,
                     'SetUpModuleTest.test_blank'),
                    ('notifyTest', 'ok', '', '', simple_test,
                     'SampleTest.test_xxxxxx2'),
                    ('notifyTest', 'ok', 'non unique name ran', '',
                     simple_test, 'SampleTest.test_non_unique_name'),
                    ('notifyTestRunFinished', ), ('notifyTestsCollected', 6)
                ]
            else:
                expected = [
                    ('notifyTestsCollected', 6),
                    ('notifyTest', 'ok', 'non unique name ran', '',
                     simple_test, 'SampleTest.test_non_unique_name'),
                    ('notifyTest', 'fail', '', 'AssertionError: Fail test 2',
                     simple_test, 'SampleTest.test_xxxxxx1'),
                    ('notifyTest', 'ok', '', '', simple_test,
                     'SampleTest.test_xxxxxx2'),
                    ('notifyTest', 'ok', '', '', simple_test2,
                     'YetAnotherSampleTest.test_abc'),
                ]

                if not IS_JYTHON:
                    if 'samples.simpleClass_test' in str(notifications):
                        expected.append((
                            'notifyTest', 'error', '',
                            'ValueError: This is an INTENTIONAL value error in setUpClass.',
                            simpleClass_test.replace('/', os.path.sep),
                            'samples.simpleClass_test.SetUpClassTest <setUpClass>'
                        ))
                        expected.append((
                            'notifyTest', 'error', '',
                            'ValueError: This is an INTENTIONAL value error in setUpModule.',
                            simpleModule_test.replace('/', os.path.sep),
                            'samples.simpleModule_test <setUpModule>'))
                    else:
                        expected.append((
                            'notifyTest', 'error', '',
                            'ValueError: This is an INTENTIONAL value error in setUpClass.',
                            simpleClass_test.replace('/', os.path.sep),
                            'simpleClass_test.SetUpClassTest <setUpClass>'))
                        expected.append((
                            'notifyTest', 'error', '',
                            'ValueError: This is an INTENTIONAL value error in setUpModule.',
                            simpleModule_test.replace('/', os.path.sep),
                            'simpleModule_test <setUpModule>'))
                else:
                    expected.append(
                        ('notifyTest', 'ok', '', '', simpleClass_test,
                         'SetUpClassTest.test_blank'))
                    expected.append(
                        ('notifyTest', 'ok', '', '', simpleModule_test,
                         'SetUpModuleTest.test_blank'))

                expected.append(('notifyTestRunFinished', ))

            expected.sort()
            new_notifications = []
            for notification in expected:
                try:
                    if len(notification) == 6:
                        # Some are binary on Py3.
                        new_notifications.append((
                            notification[0],
                            notification[1],
                            notification[2].encode('latin1'),
                            notification[3].encode('latin1'),
                            notification[4],
                            notification[5],
                        ))
                    else:
                        new_notifications.append(notification)
                except:
                    raise
            expected = new_notifications

            notifications.sort()
            if not IS_JYTHON:
                self.assertEqual(expected, notifications)
        finally:
            pydevd_io.end_redirect()
        b = buf.getvalue()
        if sys.version_info[:2] > (2, 6):
            self.assertTrue(b.find('Ran 4 tests in ') != -1, 'Found: ' + b)
        else:
            self.assertTrue(b.find('Ran 6 tests in ') != -1, 'Found: ' + b)