示例#1
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)
def run_client(job_id, port, verbosity, coverage_output_file, coverage_include):
    job_id = int(job_id)

    from _pydev_bundle import pydev_localhost
    server = xmlrpclib.Server('http://%s:%s' % (pydev_localhost.get_localhost(), port))
    server.lock = threading.Lock()


    server_comm = ServerComm(job_id, server)
    server_comm.start()

    try:
        server_facade = ServerFacade(server_comm.notifications_queue)
        from _pydev_runfiles import pydev_runfiles
        from _pydev_runfiles import pydev_runfiles_xml_rpc
        pydev_runfiles_xml_rpc.set_server(server_facade)

        #Starts None and when the 1st test is gotten, it's started (because a server may be initiated and terminated
        #before receiving any test -- which would mean a different process got all the tests to run).
        coverage = None

        try:
            tests_to_run = [1]
            while tests_to_run:
                #Investigate: is it dangerous to use the same xmlrpclib server from different threads?
                #It seems it should be, as it creates a new connection for each request...
                server.lock.acquire()
                try:
                    tests_to_run = server.GetTestsToRun(job_id)
                finally:
                    server.lock.release()

                if not tests_to_run:
                    break

                if coverage is None:
                    _coverage_files, coverage = start_coverage_support_from_params(
                        None, coverage_output_file, 1, coverage_include)


                files_to_tests = {}
                for test in tests_to_run:
                    filename_and_test = test.split('|')
                    if len(filename_and_test) == 2:
                        files_to_tests.setdefault(filename_and_test[0], []).append(filename_and_test[1])

                configuration = pydev_runfiles.Configuration(
                    '',
                    verbosity,
                    None,
                    None,
                    None,
                    files_to_tests,
                    1, #Always single job here
                    None,

                    #The coverage is handled in this loop.
                    coverage_output_file=None,
                    coverage_include=None,
                )
                test_runner = pydev_runfiles.PydevTestRunner(configuration)
                sys.stdout.flush()
                test_runner.run_tests(handle_coverage=False)
        finally:
            if coverage is not None:
                coverage.stop()
                coverage.save()


    except:
        traceback.print_exc()
    server_comm.notifications_queue.put_nowait(KillServer())
    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)
def run_client(job_id, port, verbosity, coverage_output_file,
               coverage_include):
    job_id = int(job_id)

    from _pydev_bundle import pydev_localhost
    server = xmlrpclib.Server('http://%s:%s' %
                              (pydev_localhost.get_localhost(), port))
    server.lock = threading.Lock()

    server_comm = ServerComm(job_id, server)
    server_comm.start()

    try:
        server_facade = ServerFacade(server_comm.notifications_queue)
        from _pydev_runfiles import pydev_runfiles
        from _pydev_runfiles import pydev_runfiles_xml_rpc
        pydev_runfiles_xml_rpc.set_server(server_facade)

        #Starts None and when the 1st test is gotten, it's started (because a server may be initiated and terminated
        #before receiving any test -- which would mean a different process got all the tests to run).
        coverage = None

        try:
            tests_to_run = [1]
            while tests_to_run:
                #Investigate: is it dangerous to use the same xmlrpclib server from different threads?
                #It seems it should be, as it creates a new connection for each request...
                server.lock.acquire()
                try:
                    tests_to_run = server.GetTestsToRun(job_id)
                finally:
                    server.lock.release()

                if not tests_to_run:
                    break

                if coverage is None:
                    _coverage_files, coverage = start_coverage_support_from_params(
                        None, coverage_output_file, 1, coverage_include)

                files_to_tests = {}
                for test in tests_to_run:
                    filename_and_test = test.split('|')
                    if len(filename_and_test) == 2:
                        files_to_tests.setdefault(filename_and_test[0],
                                                  []).append(
                                                      filename_and_test[1])

                configuration = pydev_runfiles.Configuration(
                    '',
                    verbosity,
                    None,
                    None,
                    None,
                    files_to_tests,
                    1,  #Always single job here
                    None,

                    #The coverage is handled in this loop.
                    coverage_output_file=None,
                    coverage_include=None,
                )
                test_runner = pydev_runfiles.PydevTestRunner(configuration)
                sys.stdout.flush()
                test_runner.run_tests(handle_coverage=False)
        finally:
            if coverage is not None:
                coverage.stop()
                coverage.save()

    except:
        traceback.print_exc()
    server_comm.notifications_queue.put_nowait(KillServer())