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: import pydev_runfiles_xml_rpc import pydevd_io buf_err = pydevd_io.StartRedirect( keep_original_redirection=True, std='stderr') buf_out = pydevd_io.StartRedirect( 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.EndRedirect('stderr') pydevd_io.EndRedirect('stdout') pydev_runfiles_xml_rpc.notifyTest('error', buf_out.getvalue(), buf_err.getvalue(), pyfile, modname, 0) return None
def startTest(self, test): _PythonTextTestResult.startTest(self, test) self.buf = pydevd_io.StartRedirect(keep_original_redirection=True, std='both') self.start_time = time.time() self._current_errors_stack = [] self._current_failures_stack = [] try: test_name = test.__class__.__name__+"."+test._testMethodName except AttributeError: #Support for jython 2.1 (__testMethodName is pseudo-private in the test case) test_name = test.__class__.__name__+"."+test._TestCase__testMethodName pydev_runfiles_xml_rpc.notifyStartTest( test.__pydev_pyfile__, test_name)
def test_xml_rpc_communication(self): 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): 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.SetServer(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') 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') self._setup_scenario(None, files_to_tests=files_to_tests) self.MyTestRunner.verbosity = 2 buf = pydevd_io.StartRedirect(keep_original_redirection=False) try: self.MyTestRunner.run_tests() self.assertEqual(6, len(notifications)) expected = [ ('notifyTestsCollected', 4), ('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'), ('notifyTestRunFinished', ), ] expected.sort() notifications.sort() self.assertEqual(expected, notifications) finally: pydevd_io.EndRedirect() b = buf.getvalue() self.assert_(b.find('Ran 4 tests in ') != -1, 'Found: ' + b)
def test_xml_rpc_communication(self): 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.SetServer(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.StartRedirect(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.EndRedirect() 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)