def test_str(self): """ Running a ProtoTest through str() is the same as getting .dotted_name """ pt = ProtoTest() pt.module = 'aoeusnth' self.assertEqual(str(pt), pt.dotted_name)
def poolRunner(test_name, coverage_number=None, omit_patterns=[]): "I am the function that pool worker subprocesses run. I run one unit test." # Each pool worker gets his own temp directory, to avoid having tests that # are used to taking turns using the same temp file name from interfering # with eachother. So long as the test doesn't use a hard-coded temp # directory, anyway. saved_tempdir = tempfile.tempdir tempfile.tempdir = tempfile.mkdtemp() # Each pool starts its own coverage, later combined by the main process. if coverage_number and coverage: cov = coverage.coverage( data_file='.coverage.{}_{}'.format( coverage_number, random.randint(0, 10000)), omit=omit_patterns) cov.start() # Create a structure to return the results of this one test result = ProtoTestResult() test = None try: test = loadTargets(test_name) except: err = sys.exc_info() t = ProtoTest() t.module = 'green.loader' t.class_name = 'N/A' t.description = 'Green encountered an error loading the unit test.' t.method_name = 'poolRunner' result.addError(t, err) try: test.run(result) except: # Some frameworks like testtools record the error AND THEN let it # through to crash things. So we only need to manufacture another error # if the underlying framework didn't, but either way we don't want to # crash. if not result.errors: err = sys.exc_info() t = ProtoTest() t.module = 'green.runner' t.class_name = 'N/A' t.description = 'Green encountered an exception not caught by the underlying test framework.' t.method_name = 'poolRunner' result.addError(t, err) # Finish coverage if coverage_number and coverage: cov.stop() cov.save() # Restore the state of the temp directory shutil.rmtree(tempfile.tempdir) tempfile.tempdir = saved_tempdir return result
def test_ProtoTestBlank(self): """ ProtoTest can be instantiated empty """ pt = ProtoTest() for i in ['module', 'class_name', 'docstr_part', 'method_name']: self.assertEqual('', getattr(pt, i, None))
def test_ProtoTestBlank(self): """ ProtoTest can be instantiated empty """ pt = ProtoTest() for i in ["module", "class_name", "docstr_part", "method_name"]: self.assertEqual("", getattr(pt, i, None))
def test_stderrNoErrput(self): """ recordStderr ignores empty errput sent to it """ btr = BaseTestResult(None, None) pt = ProtoTest() btr.recordStderr(pt, '') self.assertEqual(btr.stderr_errput, {})
def test_stdoutNoOutput(self): """ recordStdout ignores empty output sent to it """ btr = BaseTestResult(None, None) pt = ProtoTest() btr.recordStdout(pt, '') self.assertEqual(btr.stdout_output, {})
def test_stderrErrput(self): """ recordStderr records errput. """ btr = BaseTestResult(None, None) pt = ProtoTest() o = "some errput" btr.recordStderr(pt, o) self.assertEqual(btr.stderr_errput[pt], o)
def test_stdoutOutput(self): """ recordStdout records output. """ btr = BaseTestResult(None, None) pt = ProtoTest() o = "some output" btr.recordStdout(pt, o) self.assertEqual(btr.stdout_output[pt], o)
def setUp(self): self._destination = StringIO() self._test_results = GreenTestResult(default_args, GreenStream(StringIO())) self._adapter = JUnitXML() self._test = ProtoTest() self._test.module = "my_module" self._test.class_name = "MyClass" self._test.method_name = "my_method"
def test_displayStderr(self): """ displayStderr displays captured stderr """ stream = StringIO() noise = "blah blah blah" btr = BaseTestResult(stream, Colors(False)) pt = ProtoTest() btr.stderr_errput[pt] = noise btr.displayStderr(pt) self.assertIn(noise, stream.getvalue())
def poolRunner(test_name, coverage_number=None, omit=[]): "I am the function that pool worker subprocesses run. I run one unit test." # Each pool worker gets his own temp directory, to avoid having tests that # are used to taking turns using the same temp file name from interfering # with eachother. So long as the test doesn't use a hard-coded temp # directory, anyway. saved_tempdir = tempfile.tempdir tempfile.tempdir = tempfile.mkdtemp() # Each pool starts its own coverage, later combined by the main process. if coverage_number and coverage: cov = coverage.coverage( data_file='.coverage.{}_{}'.format( coverage_number, random.randint(0, 10000)), omit=omit) cov.start() # Create a structure to return the results of this one test result = ProtoTestResult() test = None try: test = getTests(test_name) test.run(result) except: err = sys.exc_info() t = ProtoTest() t.module = 'green.runner' t.class_name = 'N/A' t.description = 'Green encountered an error loading the unit test itself.' t.method_name = 'poolRunner' result.addError(t, err) # Finish coverage if coverage_number and coverage: cov.stop() cov.save() # Restore the state of the temp directory shutil.rmtree(tempfile.tempdir) tempfile.tempdir = saved_tempdir return result
def test_class_or_module_failure(self): """ If we parse an error from a class or module failure, we get the correct result. """ p = ProtoTest() p.is_class_or_module_teardown_error = True p.name = "the thing" self.assertEqual(p.getDescription(1), "the thing") self.assertEqual(p.getDescription(2), "the thing") self.assertEqual(p.getDescription(3), "the thing")
def test_ProtoTestFromTest(self): """ Passing a test into ProtoTest copies out the relevant info. """ module = 'green.test.test_result' class_name = 'Small' docstr_part = 'stuff' method_name = 'test_method' class Small(unittest.TestCase): def test_method(self): "stuff" pt = ProtoTest(Small('test_method')) for i in ['module', 'class_name', 'docstr_part', 'method_name']: self.assertEqual(locals()[i], getattr(pt, i, None))
def test_ProtoTestFromTest(self): """ Passing a test into ProtoTest copies out the relevant info. """ module = "green.test.test_result" class_name = "Small" docstr_part = "stuff" method_name = "test_method" class Small(unittest.TestCase): def test_method(self): "stuff" pt = ProtoTest(Small("test_method")) for i in ["module", "class_name", "docstr_part", "method_name"]: self.assertEqual(locals()[i], getattr(pt, i, None))
def raise_internal_failure(msg): err = sys.exc_info() t = ProtoTest() t.module = 'green.loader' t.class_name = 'N/A' t.description = msg t.method_name = 'poolRunner' result.startTest(t) result.addError(t, err) result.stopTest(t) queue.put(result) cleanup()
def test(module, class_name, method_name): test = ProtoTest() test.module = module test.class_name = class_name test.method_name = method_name return test
def poolRunner(target, queue, coverage_number=None, omit_patterns=[]): # pragma: no cover """ I am the function that pool worker processes run. I run one unit test. """ # Each pool worker gets his own temp directory, to avoid having tests that # are used to taking turns using the same temp file name from interfering # with eachother. So long as the test doesn't use a hard-coded temp # directory, anyway. saved_tempdir = tempfile.tempdir tempfile.tempdir = tempfile.mkdtemp() def cleanup(): # Restore the state of the temp directory shutil.rmtree(tempfile.tempdir, ignore_errors=True) tempfile.tempdir = saved_tempdir queue.put(None) # Finish coverage if coverage_number and coverage: cov.stop() cov.save() # Each pool starts its own coverage, later combined by the main process. if coverage_number and coverage: cov = coverage.coverage( data_file='.coverage.{}_{}'.format( coverage_number, random.randint(0, 10000)), omit=omit_patterns) cov._warn_no_data = False cov.start() # What to do each time an individual test is started already_sent = set() def start_callback(test): # Let the main process know what test we are starting test = proto_test(test) if test not in already_sent: queue.put(test) already_sent.add(test) def finalize_callback(test_result): # Let the main process know what happened with the test run queue.put(test_result) result = ProtoTestResult(start_callback, finalize_callback) test = None try: test = loadTargets(target) except: err = sys.exc_info() t = ProtoTest() t.module = 'green.loader' t.class_name = 'N/A' t.description = 'Green encountered an error loading the unit test.' t.method_name = 'poolRunner' result.startTest(t) result.addError(t, err) result.stopTest(t) queue.put(t) queue.put(result) cleanup() return if getattr(test, 'run', False): # Loading was successful, lets do this try: test.run(result) except: # Some frameworks like testtools record the error AND THEN let it # through to crash things. So we only need to manufacture another error # if the underlying framework didn't, but either way we don't want to # crash. if result.errors: queue.put(result) else: err = sys.exc_info() result.startTest(test) result.addError(test, err) result.stopTest(test) queue.put(result) else: # loadTargets() returned an object without a run() method, probably None description = 'Test loader returned an un-runnable object. Is "{}" importable from your current location? Maybe you forgot an __init__.py in your directory? Unrunnable object looks like: {} of type {} with dir {}'.format( target, str(test), type(test), dir(test)) err = (TypeError, TypeError(description), None) t = ProtoTest() target_list = target.split('.') t.module = '.'.join(target_list[:-2]) if len(target_list) > 1 else target t.class_name = target.split('.')[-2] if len(target_list) > 1 else 'UnknownClass' t.description = description t.method_name = target.split('.')[-1] if len(target_list) > 1 else 'unknown_method' result.startTest(t) result.addError(t, err) result.stopTest(t) cleanup()
def poolRunner(target, queue, coverage_number=None, omit_patterns=[]): # pragma: no cover """ I am the function that pool worker processes run. I run one unit test. """ # Each pool worker gets his own temp directory, to avoid having tests that # are used to taking turns using the same temp file name from interfering # with eachother. So long as the test doesn't use a hard-coded temp # directory, anyway. saved_tempdir = tempfile.tempdir tempfile.tempdir = tempfile.mkdtemp() def cleanup(): # Restore the state of the temp directory if sys.version_info[0] == 2: # pragma: no cover shutil.rmtree(tempfile.tempdir, ignore_errors=True) tempfile.tempdir = saved_tempdir queue.put(None) # Finish coverage if coverage_number and coverage: cov.stop() cov.save() # Each pool starts its own coverage, later combined by the main process. if coverage_number and coverage: cov = coverage.coverage(data_file='.coverage.{}_{}'.format( coverage_number, random.randint(0, 10000)), omit=omit_patterns) cov._warn_no_data = False cov.start() # What to do each time an individual test is started already_sent = set() def start_callback(test): # Let the main process know what test we are starting test = proto_test(test) if test not in already_sent: queue.put(test) already_sent.add(test) def finalize_callback(test_result): # Let the main process know what happened with the test run queue.put(test_result) result = ProtoTestResult(start_callback, finalize_callback) test = None try: test = loadTargets(target) except: err = sys.exc_info() t = ProtoTest() t.module = 'green.loader' t.class_name = 'N/A' t.description = 'Green encountered an error loading the unit test.' t.method_name = 'poolRunner' result.startTest(t) result.addError(t, err) result.stopTest(t) queue.put(result) cleanup() return if getattr(test, 'run', False): # Loading was successful, lets do this try: test.run(result) # If your class setUpClass(self) method crashes, the test doesn't # raise an exception, but it does add an entry to errors. Some # other things add entries to errors as well, but they all call the # finalize callback. if result and (not result.finalize_callback_called) and getattr( result, 'errors', False): queue.put(test) queue.put(result) except: # Some frameworks like testtools record the error AND THEN let it # through to crash things. So we only need to manufacture another # error if the underlying framework didn't, but either way we don't # want to crash. if result.errors: queue.put(result) else: err = sys.exc_info() result.startTest(test) result.addError(test, err) result.stopTest(test) queue.put(result) else: # loadTargets() returned an object without a run() method, probably # None description = ('Test loader returned an un-runnable object. Is "{}" ' 'importable from your current location? Maybe you ' 'forgot an __init__.py in your directory? Unrunnable ' 'object looks like: {} of type {} with dir {}'.format( target, str(test), type(test), dir(test))) err = (TypeError, TypeError(description), None) t = ProtoTest() target_list = target.split('.') t.module = '.'.join( target_list[:-2]) if len(target_list) > 1 else target t.class_name = target.split( '.')[-2] if len(target_list) > 1 else 'UnknownClass' t.description = description t.method_name = target.split( '.')[-1] if len(target_list) > 1 else 'unknown_method' result.startTest(t) result.addError(t, err) result.stopTest(t) queue.put(result) cleanup()
def poolRunner(target, queue, coverage_number=None, omit_patterns=[], cov_config_file=True): # pragma: no cover """ I am the function that pool worker processes run. I run one unit test. coverage_config_file is a special option that is either a string specifying the custom coverage config file or the special default value True (which causes coverage to search for it's standard config files). """ # Each pool worker gets his own temp directory, to avoid having tests that # are used to taking turns using the same temp file name from interfering # with eachother. So long as the test doesn't use a hard-coded temp # directory, anyway. saved_tempdir = tempfile.tempdir tempfile.tempdir = tempfile.mkdtemp() def raise_internal_failure(msg): err = sys.exc_info() t = ProtoTest() t.module = "green.loader" t.class_name = "N/A" t.description = msg t.method_name = "poolRunner" result.startTest(t) result.addError(t, err) result.stopTest(t) queue.put(result) cleanup() def cleanup(): # Restore the state of the temp directory tempfile.tempdir = saved_tempdir queue.put(None) # Finish coverage if coverage_number: cov.stop() cov.save() # Each pool starts its own coverage, later combined by the main process. if coverage_number: cov = coverage.coverage( data_file=".coverage.{}_{}".format(coverage_number, random.randint(0, 10000)), omit=omit_patterns, config_file=cov_config_file, ) cov._warn_no_data = False cov.start() # What to do each time an individual test is started already_sent = set() def start_callback(test): # Let the main process know what test we are starting test = proto_test(test) if test not in already_sent: queue.put(test) already_sent.add(test) def finalize_callback(test_result): # Let the main process know what happened with the test run queue.put(test_result) result = ProtoTestResult(start_callback, finalize_callback) test = None try: loader = GreenTestLoader() test = loader.loadTargets(target) except: raise_internal_failure( "Green encountered an error loading the unit test.") return if getattr(test, "run", False): # Loading was successful, lets do this try: test.run(result) # If your class setUpClass(self) method crashes, the test doesn't # raise an exception, but it does add an entry to errors. Some # other things add entries to errors as well, but they all call the # finalize callback. if (result and (not result.finalize_callback_called) and getattr(result, "errors", False)): queue.put(test) queue.put(result) except: # Some frameworks like testtools record the error AND THEN let it # through to crash things. So we only need to manufacture another # error if the underlying framework didn't, but either way we don't # want to crash. if result.errors: queue.put(result) else: try: err = sys.exc_info() result.startTest(test) result.addError(test, err) result.stopTest(test) queue.put(result) except: raise_internal_failure( "Green encountered an error when running the test.") return else: # loadTargets() returned an object without a run() method, probably # None description = ('Test loader returned an un-runnable object. Is "{}" ' "importable from your current location? Maybe you " "forgot an __init__.py in your directory? Unrunnable " "object looks like: {} of type {} with dir {}".format( target, str(test), type(test), dir(test))) err = (TypeError, TypeError(description), None) t = ProtoTest() target_list = target.split(".") t.module = ".".join( target_list[:-2]) if len(target_list) > 1 else target t.class_name = target.split( ".")[-2] if len(target_list) > 1 else "UnknownClass" t.description = description t.method_name = (target.split(".")[-1] if len(target_list) > 1 else "unknown_method") result.startTest(t) result.addError(t, err) result.stopTest(t) queue.put(result) cleanup()
def poolRunner(target, queue, coverage_number=None, omit_patterns=[], cov_config_file=True): # pragma: no cover """ I am the function that pool worker processes run. I run one unit test. coverage_config_file is a special option that is either a string specifying the custom coverage config file or the special default value True (which causes coverage to search for it's standard config files). """ # Each pool worker gets his own temp directory, to avoid having tests that # are used to taking turns using the same temp file name from interfering # with eachother. So long as the test doesn't use a hard-coded temp # directory, anyway. saved_tempdir = tempfile.tempdir tempfile.tempdir = tempfile.mkdtemp() def cleanup(): # Restore the state of the temp directory # TODO: Make this not necessary on macOS+Python3 (see #173) if sys.version_info[0] == 2: shutil.rmtree(tempfile.tempdir, ignore_errors=True) tempfile.tempdir = saved_tempdir queue.put(None) # Finish coverage if coverage_number: cov.stop() cov.save() # Each pool starts its own coverage, later combined by the main process. if coverage_number: cov = coverage.coverage( data_file='.coverage.{}_{}'.format( coverage_number, random.randint(0, 10000)), omit=omit_patterns, config_file=cov_config_file) cov._warn_no_data = False cov.start() # What to do each time an individual test is started already_sent = set() def start_callback(test): # Let the main process know what test we are starting test = proto_test(test) if test not in already_sent: queue.put(test) already_sent.add(test) def finalize_callback(test_result): # Let the main process know what happened with the test run queue.put(test_result) result = ProtoTestResult(start_callback, finalize_callback) test = None try: loader = GreenTestLoader() test = loader.loadTargets(target) except: err = sys.exc_info() t = ProtoTest() t.module = 'green.loader' t.class_name = 'N/A' t.description = 'Green encountered an error loading the unit test.' t.method_name = 'poolRunner' result.startTest(t) result.addError(t, err) result.stopTest(t) queue.put(result) cleanup() return if getattr(test, 'run', False): # Loading was successful, lets do this try: test.run(result) # If your class setUpClass(self) method crashes, the test doesn't # raise an exception, but it does add an entry to errors. Some # other things add entries to errors as well, but they all call the # finalize callback. if result and (not result.finalize_callback_called) and getattr(result, 'errors', False): queue.put(test) queue.put(result) except: # Some frameworks like testtools record the error AND THEN let it # through to crash things. So we only need to manufacture another # error if the underlying framework didn't, but either way we don't # want to crash. if result.errors: queue.put(result) else: err = sys.exc_info() result.startTest(test) result.addError(test, err) result.stopTest(test) queue.put(result) else: # loadTargets() returned an object without a run() method, probably # None description = ('Test loader returned an un-runnable object. Is "{}" ' 'importable from your current location? Maybe you ' 'forgot an __init__.py in your directory? Unrunnable ' 'object looks like: {} of type {} with dir {}' .format(target, str(test), type(test), dir(test)) ) err = (TypeError, TypeError(description), None) t = ProtoTest() target_list = target.split('.') t.module = '.'.join(target_list[:-2]) if len(target_list) > 1 else target t.class_name = target.split('.')[-2] if len(target_list) > 1 else 'UnknownClass' t.description = description t.method_name = target.split('.')[-1] if len(target_list) > 1 else 'unknown_method' result.startTest(t) result.addError(t, err) result.stopTest(t) queue.put(result) cleanup()