def test_error(self): """ Exception raised running unit test is reported as an error """ # Parent directory setup os.chdir(self.tmpdir) sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) basename = os.path.basename(sub_tmpdir) # Child setup fh = open(os.path.join(basename, '__init__.py'), 'w') fh.write('\n') fh.close() fh = open(os.path.join(basename, 'test_pool_runner_dotted_fail.py'), 'w') fh.write(dedent( """ import unittest class A(unittest.TestCase): def testError(self): raise AttributeError """)) fh.close() module_name = basename + '.test_pool_runner_dotted_fail.A.testError' result = Queue() poolRunner(module_name, result) result.get() self.assertEqual(len(result.get().errors), 1)
def test_bad_attr(self): """ Accessing a bad attribute is only reported once (see #150) """ # Parent directory setup os.chdir(self.tmpdir) sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) basename = os.path.basename(sub_tmpdir) # Child setup fh = open(os.path.join(basename, "__init__.py"), "w") fh.write("\n") fh.close() fh = open(os.path.join(basename, "test_pool_runner_bad_attr.py"), "w") fh.write( dedent( """ import unittest class A(unittest.TestCase): def testBadAttr(self): "".garbage """ ) ) fh.close() module_name = basename + ".test_pool_runner_bad_attr.A.testBadAttr" result = Queue() poolRunner(module_name, result) result.get_nowait() # should get the target name result.get_nowait() # should get the result result.get_nowait() # should get None # should raise Empty unless the extra result bug is present self.assertRaises(Empty, result.get_nowait)
def test_normalRun(self): """ Runs normally """ saved_coverage = process.coverage process.coverage = MagicMock() self.addCleanup(setattr, process, 'coverage', saved_coverage) # Parent directory setup os.chdir(self.tmpdir) sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) basename = os.path.basename(sub_tmpdir) # Child setup fh = open(os.path.join(basename, '__init__.py'), 'w') fh.write('\n') fh.close() fh = open(os.path.join(basename, 'test_pool_runner_dotted.py'), 'w') fh.write(dedent( """ import unittest class A(unittest.TestCase): def testPass(self): pass """)) fh.close() module_name = basename + '.test_pool_runner_dotted.A.testPass' result = Queue() poolRunner(module_name, result, 1) result.get() self.assertEqual(len(result.get().passing), 1)
def test_foreign_suite(self): """ Load tests does not reuse the tests and instead returns another TestSuite (or maybe not even a unittest.TestSuite). """ with open(os.path.join(self.basename, 'test_load_keys_foreign_suite.py'), 'w') as f: f.write(textwrap.dedent( """ import unittest class A(unittest.TestCase): def test_that_will_fail(self): self.fail() def load_tests(loader, tests, pattern): class B(unittest.TestCase): def test_that_succeeds(self): pass suite = unittest.TestSuite() suite.addTests(loader.loadTestsFromTestCase(B)) return suite """)) module_name = self.basename + '.test_load_keys_foreign_suite' result = Queue() poolRunner(module_name, result, 0) result.get() proto_test_result = result.get() self.assertEqual(len(proto_test_result.passing), 1) self.assertEqual(len(proto_test_result.errors), 0) self.assertEqual(len(proto_test_result.failures), 0) self.assertEqual(proto_test_result.passing[0].class_name, 'B')
def test_error(self): """ Exception raised running unit test is reported as an error """ # Parent directory setup os.chdir(self.tmpdir) sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) basename = os.path.basename(sub_tmpdir) # Child setup fh = open(os.path.join(basename, "__init__.py"), "w") fh.write("\n") fh.close() fh = open(os.path.join(basename, "test_pool_runner_dotted_fail.py"), "w") fh.write( dedent( """ import unittest class A(unittest.TestCase): def testError(self): raise AttributeError """ ) ) fh.close() module_name = basename + ".test_pool_runner_dotted_fail.A.testError" result = Queue() poolRunner(module_name, result) result.get() self.assertEqual(len(result.get().errors), 1)
def test_normalRun(self): """ Runs normally """ saved_coverage = process.coverage process.coverage = MagicMock() self.addCleanup(setattr, process, "coverage", saved_coverage) # Parent directory setup os.chdir(self.tmpdir) sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) basename = os.path.basename(sub_tmpdir) # Child setup fh = open(os.path.join(basename, "__init__.py"), "w") fh.write("\n") fh.close() fh = open(os.path.join(basename, "test_pool_runner_dotted.py"), "w") fh.write( dedent( """ import unittest class A(unittest.TestCase): def testPass(self): pass """ ) ) fh.close() module_name = basename + ".test_pool_runner_dotted.A.testPass" result = Queue() poolRunner(module_name, result, 1) result.get() self.assertEqual(len(result.get().passing), 1)
def test_none_cancels(self): """ Check that if load_tests returns None, no tests are run. """ with open(os.path.join(self.basename, 'test_load_keys_none_cancels.py'), 'w') as fh: fh.write(textwrap.dedent( """ import unittest class A(unittest.TestCase): def test_that_will_fail(self): self.fail() def load_tests(loader, tests, pattern): return None """)) module_name = self.basename + '.test_load_keys_none_cancels' result = Queue() poolRunner(module_name, result, 0) result.get() proto_test_result = result.get() self.assertEqual(len(proto_test_result.errors), 1) self.assertEqual(len(proto_test_result.passing), 0) self.assertEqual(len(proto_test_result.failures), 0)
def test_monkey_patch(self): """ Check that monkey-patching a TestCase in the load_tests function actually changes the referenced class. """ with open(os.path.join(self.basename, 'test_load_tests_monkeypatch.py'), 'w') as f: f.write(textwrap.dedent( """ import unittest class A(unittest.TestCase): passing = False def test_that_will_fail(self): self.assertTrue(self.passing) def load_tests(loader, tests, pattern): A.passing = True return tests """)) module_name = self.basename + '.test_load_tests_monkeypatch' result = Queue() poolRunner(module_name, result, 0) result.get() proto_test_result = result.get() self.assertEqual(len(proto_test_result.passing), 1) self.assertEqual(len(proto_test_result.failures), 0) self.assertEqual(len(proto_test_result.errors), 0) self.assertEqual(proto_test_result.passing[0].class_name, 'A')
def test_bad_attr(self): """ Accessing a bad attribute is only reported once (see #150) """ # Parent directory setup os.chdir(self.tmpdir) sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) basename = os.path.basename(sub_tmpdir) # Child setup fh = open(os.path.join(basename, '__init__.py'), 'w') fh.write('\n') fh.close() fh = open(os.path.join(basename, 'test_pool_runner_bad_attr.py'), 'w') fh.write(dedent( """ import unittest class A(unittest.TestCase): def testBadAttr(self): "".garbage """)) fh.close() module_name = basename + '.test_pool_runner_bad_attr.A.testBadAttr' result = Queue() poolRunner(module_name, result) result.get_nowait() # should get the target name result.get_nowait() # should get the result result.get_nowait() # should get None # should raise Empty unless the extra result bug is present self.assertRaises(Empty, result.get_nowait)
def test_foreign_suite(self): """ Load tests does not reuse the tests and instead returns another TestSuite (or maybe not even a unittest.TestSuite). """ with open( os.path.join(self.basename, 'test_load_keys_foreign_suite.py'), 'w') as f: f.write( textwrap.dedent(""" import unittest class A(unittest.TestCase): def test_that_will_fail(self): self.fail() def load_tests(loader, tests, pattern): class B(unittest.TestCase): def test_that_succeeds(self): pass suite = unittest.TestSuite() suite.addTests(loader.loadTestsFromTestCase(B)) return suite """)) module_name = self.basename + '.test_load_keys_foreign_suite' result = Queue() poolRunner(module_name, result, 0) result.get() proto_test_result = result.get() self.assertEqual(len(proto_test_result.passing), 1) self.assertEqual(len(proto_test_result.errors), 0) self.assertEqual(len(proto_test_result.failures), 0) self.assertEqual(proto_test_result.passing[0].class_name, 'B')
def test_monkey_patch(self): """ Check that monkey-patching a TestCase in the load_tests function actually changes the referenced class. """ with open( os.path.join(self.basename, 'test_load_tests_monkeypatch.py'), 'w') as f: f.write( textwrap.dedent(""" import unittest class A(unittest.TestCase): passing = False def test_that_will_fail(self): self.assertTrue(self.passing) def load_tests(loader, tests, pattern): A.passing = True return tests """)) module_name = self.basename + '.test_load_tests_monkeypatch' result = Queue() poolRunner(module_name, result, 0) result.get() proto_test_result = result.get() self.assertEqual(len(proto_test_result.passing), 1) self.assertEqual(len(proto_test_result.failures), 0) self.assertEqual(len(proto_test_result.errors), 0) self.assertEqual(proto_test_result.passing[0].class_name, 'A')
def test_none_cancels(self): """ Check that if load_tests returns None, no tests are run. """ with open( os.path.join(self.basename, 'test_load_keys_none_cancels.py'), 'w') as fh: fh.write( textwrap.dedent(""" import unittest class A(unittest.TestCase): def test_that_will_fail(self): self.fail() def load_tests(loader, tests, pattern): return None """)) module_name = self.basename + '.test_load_keys_none_cancels' result = Queue() poolRunner(module_name, result, 0) result.get() proto_test_result = result.get() self.assertEqual(len(proto_test_result.errors), 1) self.assertEqual(len(proto_test_result.passing), 0) self.assertEqual(len(proto_test_result.failures), 0)
def test_SyntaxErrorInUnitTest(self): """ SyntaxError gets reported as an error loading the unit test """ saved_coverage = process.coverage process.coverage = MagicMock() self.addCleanup(setattr, process, "coverage", saved_coverage) # Parent directory setup os.chdir(self.tmpdir) sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) basename = os.path.basename(sub_tmpdir) # Child setup fh = open(os.path.join(basename, "__init__.py"), "w") fh.write("\n") fh.close() fh = open(os.path.join(basename, "test_pool_syntax_error.py"), "w") fh.write("aoeu") fh.close() result = Queue() poolRunner(basename, result, 1) result.get() self.assertEqual(len(result.get().errors), 1)
def test_SyntaxErrorInUnitTest(self): """ SyntaxError gets reported as an error loading the unit test """ saved_coverage = process.coverage process.coverage = MagicMock() self.addCleanup(setattr, process, 'coverage', saved_coverage) # Parent directory setup os.chdir(self.tmpdir) sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) basename = os.path.basename(sub_tmpdir) # Child setup fh = open(os.path.join(basename, '__init__.py'), 'w') fh.write('\n') fh.close() fh = open(os.path.join(basename, 'test_pool_syntax_error.py'), 'w') fh.write("aoeu") fh.close() result = Queue() poolRunner(basename, result, 1) result.get() self.assertEqual(len(result.get().errors), 1)