def test_runCoverage(self): """ Running coverage in process mode doesn't crash """ try: import coverage; coverage except: self.skipTest("Coverage needs to be installed for this test") sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) # pkg/__init__.py fh = open(os.path.join(sub_tmpdir, '__init__.py'), 'w') fh.write('\n') fh.close() fh = open(os.path.join(sub_tmpdir, 'test_coverage.py'), 'w') fh.write(dedent( """ import unittest class A(unittest.TestCase): def testPasses(self): pass""")) fh.close() # Load the tests os.chdir(self.tmpdir) tests = loadTargets('.') self.args.processes = 2 self.args.run_coverage = True self.args.cov = MagicMock() run(tests, self.stream, self.args, testing=True) os.chdir(TestProcesses.startdir) self.assertIn('OK', self.stream.getvalue())
def test_uncaughtException(self): """ Exceptions that escape the test framework get caught by poolRunner and reported as a failure. For example, the testtools implementation of TestCase unwisely (but deliberately) lets SystemExit exceptions through. """ global skip_testtools if skip_testtools: self.skipTest('testtools must be installed to run this test.') sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) # pkg/__init__.py fh = open(os.path.join(sub_tmpdir, '__init__.py'), 'w') fh.write('\n') fh.close() fh = open(os.path.join(sub_tmpdir, 'test_uncaught.py'), 'w') fh.write(dedent( """ import testtools class Uncaught(testtools.TestCase): def test_uncaught(self): raise SystemExit(0) """)) fh.close() # Load the tests os.chdir(self.tmpdir) tests = loadTargets('.') self.args.processes = 2 run(tests, self.stream, self.args) os.chdir(TestProcesses.startdir) self.assertIn('FAILED', self.stream.getvalue())
def test_hmaStream(self): """ run() can use a hmaStream for output. """ gs = hmaStream(self.stream) run(hmaTestSuite(), gs, args=self.args) self.assertIn('No Tests Found', self.stream.getvalue())
def test_catchProcessSIGINT(self): """ run() can catch SIGINT while running a process. """ if platform.system() == 'Windows': self.skipTest('This test is for posix-specific behavior.') # Mock the list of TestResult instances that should be stopped, # otherwise the actual TestResult that is running this test will be # told to stop when we send SIGINT sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) saved__results = unittest.signals._results unittest.signals._results = weakref.WeakKeyDictionary() self.addCleanup(setattr, unittest.signals, '_results', saved__results) fh = open(os.path.join(sub_tmpdir, 'test_sigint.py'), 'w') fh.write(dedent( """ import os import signal import unittest class SIGINTCase(unittest.TestCase): def test00(self): os.kill({}, signal.SIGINT) """.format(os.getpid()))) fh.close() os.chdir(sub_tmpdir) tests = loadTargets('test_sigint') self.args.processes = 2 run(tests, self.stream, self.args) os.chdir(TestProcesses.startdir)
def test_empty(self): """ run() does not crash with empty suite and processes """ suite = hmaTestSuite() self.args.processes = 2 self.args.termcolor = False run(suite, self.stream, self.args)
def test_collisionProtection(self): """ If tempfile.gettempdir() is used for dir, using same testfile name will not collide. """ sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) # Child setup # pkg/__init__.py fh = open(os.path.join(sub_tmpdir, '__init__.py'), 'w') fh.write('\n') fh.close() # pkg/target_module.py fh = open(os.path.join(sub_tmpdir, 'some_module.py'), 'w') fh.write('a = 1\n') fh.close() # pkg/test/__init__.py os.mkdir(os.path.join(sub_tmpdir, 'test')) fh = open(os.path.join(sub_tmpdir, 'test', '__init__.py'), 'w') fh.write('\n') fh.close() # pkg/test/test_target_module.py fh = open(os.path.join(sub_tmpdir, 'test', 'test_some_module.py'), 'w') fh.write(dedent( """ import os import tempfile import unittest import {}.some_module class A(unittest.TestCase): def setUp(self): self.tmpdir = tempfile.gettempdir() self.filename = os.path.join(tempfile.gettempdir(), 'file.txt') def testOne(self): for msg in [str(x) for x in range(100)]: fh = open(self.filename, 'w') fh.write(msg) fh.close() self.assertEqual(msg, open(self.filename).read()) def testTwo(self): for msg in [str(x) for x in range(100,200)]: fh = open(self.filename, 'w') fh.write(msg) fh.close() self.assertEqual(msg, open(self.filename).read()) """.format(os.path.basename(sub_tmpdir)))) fh.close() # Load the tests os.chdir(self.tmpdir) tests = loadTargets('.') self.args.processes = 2 self.args.termcolor = False try: run(tests, self.stream, self.args) except KeyboardInterrupt: os.kill(os.getpid(), signal.SIGINT) os.chdir(TestProcesses.startdir) self.assertIn('OK', self.stream.getvalue())
def test_stdout(self): """ run() can use sys.stdout as the stream. """ saved_stdout = sys.stdout sys.stdout = self.stream self.addCleanup(setattr, sys, 'stdout', saved_stdout) run(hmaTestSuite(), sys.stdout, args=self.args) self.assertIn('No Tests Found', self.stream.getvalue())
def test_detectNumProcesses(self): """ args.processes = 0 causes auto-detection of number of processes. """ sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) # pkg/__init__.py fh = open(os.path.join(sub_tmpdir, '__init__.py'), 'w') fh.write('\n') fh.close() fh = open(os.path.join(sub_tmpdir, 'test_autoprocesses.py'), 'w') fh.write(dedent( """ import unittest class A(unittest.TestCase): def testPasses(self): pass""")) fh.close() # Load the tests os.chdir(self.tmpdir) tests = loadTargets('.') self.args.processes = 0 run(tests, self.stream, self.args) os.chdir(TestProcesses.startdir) self.assertIn('OK', self.stream.getvalue())
def test_failedSaysSo(self): """ A failing test case causes the whole run to report 'FAILED' """ sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) fh = open(os.path.join(sub_tmpdir, 'test_failed.py'), 'w') fh.write(dedent( """ import unittest class Failed(unittest.TestCase): def test01(self): self.assertTrue(False) """.format(os.getpid()))) fh.close() os.chdir(sub_tmpdir) tests = loadTargets('test_failed') result = run(tests, self.stream, self.args) os.chdir(self.startdir) self.assertEqual(result.testsRun, 1) self.assertIn('FAILED', self.stream.getvalue())
def test_systemExit(self): """ Raising a SystemExit gets caught and reported. """ sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) fh = open(os.path.join(sub_tmpdir, 'test_systemexit.py'), 'w') fh.write(dedent( """ import unittest class SystemExitCase(unittest.TestCase): def test00(self): raise SystemExit(1) def test01(self): pass """.format(os.getpid()))) fh.close() os.chdir(sub_tmpdir) tests = loadTargets('test_systemexit') result = run(tests, self.stream, self.args) os.chdir(self.startdir) self.assertEqual(result.testsRun, 2)
def test_warnings(self): """ setting warnings='always' doesn't crash """ self.args.warnings = 'always' sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) fh = open(os.path.join(sub_tmpdir, 'test_warnings.py'), 'w') fh.write(dedent( """ import unittest class Warnings(unittest.TestCase): def test01(self): pass """.format(os.getpid()))) fh.close() os.chdir(sub_tmpdir) tests = loadTargets('test_warnings') result = run(tests, self.stream, self.args) os.chdir(self.startdir) self.assertEqual(result.testsRun, 1) self.assertIn('OK', self.stream.getvalue())
def test_failfast(self): """ failfast causes the testing to stop after the first failure. """ sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) fh = open(os.path.join(sub_tmpdir, 'test_failfast.py'), 'w') fh.write(dedent( """ import unittest class SIGINTCase(unittest.TestCase): def test00(self): raise Exception def test01(self): pass """.format(os.getpid()))) fh.close() os.chdir(sub_tmpdir) tests = loadTargets('test_failfast') self.args.failfast = True result = run(tests, self.stream, self.args) os.chdir(self.startdir) self.assertEqual(result.testsRun, 1)
def test_verbose3(self): """ verbose=3 causes version output, and an empty test case passes. """ self.args.verbose = 3 sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir) fh = open(os.path.join(sub_tmpdir, 'test_verbose3.py'), 'w') fh.write(dedent( """ import unittest class Verbose3(unittest.TestCase): def test01(self): pass """.format(os.getpid()))) fh.close() os.chdir(sub_tmpdir) tests = loadTargets('test_verbose3') result = run(tests, self.stream, self.args) os.chdir(self.startdir) self.assertEqual(result.testsRun, 1) self.assertIn('hma', self.stream.getvalue()) self.assertIn('OK', self.stream.getvalue())
def test_noTestsFound(self): """ When we don't find any tests, we say so. """ run(hmaTestSuite(), self.stream, self.args) self.assertIn('No Tests Found', self.stream.getvalue())