Python unittest.TextTestRunner() Examples

The following are 30 code examples for showing how to use unittest.TextTestRunner(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module unittest , or try the search function .

Example 1
Project: pywren-ibm-cloud   Author: pywren   File: tests.py    License: Apache License 2.0 7 votes vote down vote up
def run_tests(test_to_run, config=None):
    global CONFIG, STORAGE_CONFIG, STORAGE

    CONFIG = json.load(args.config) if config else default_config()
    STORAGE_CONFIG = extract_storage_config(CONFIG)
    STORAGE = InternalStorage(STORAGE_CONFIG).storage_handler

    suite = unittest.TestSuite()
    if test_to_run == 'all':
        suite.addTest(unittest.makeSuite(TestPywren))
    else:
        try:
            suite.addTest(TestPywren(test_to_run))
        except ValueError:
            print("unknown test, use: --help")
            sys.exit()

    runner = unittest.TextTestRunner()
    runner.run(suite) 
Example 2
Project: jawfish   Author: war-and-code   File: test_runner.py    License: MIT License 6 votes vote down vote up
def testRunnerRegistersResult(self):
        class Test(unittest.TestCase):
            def testFoo(self):
                pass
        originalRegisterResult = unittest.runner.registerResult
        def cleanup():
            unittest.runner.registerResult = originalRegisterResult
        self.addCleanup(cleanup)

        result = unittest.TestResult()
        runner = unittest.TextTestRunner(stream=io.StringIO())
        # Use our result object
        runner._makeResult = lambda: result

        self.wasRegistered = 0
        def fakeRegisterResult(thisResult):
            self.wasRegistered += 1
            self.assertEqual(thisResult, result)
        unittest.runner.registerResult = fakeRegisterResult

        runner.run(unittest.TestSuite())
        self.assertEqual(self.wasRegistered, 1) 
Example 3
Project: jawfish   Author: war-and-code   File: test_runner.py    License: MIT License 6 votes vote down vote up
def test_startTestRun_stopTestRun_called(self):
        class LoggingTextResult(LoggingResult):
            separator2 = ''
            def printErrors(self):
                pass

        class LoggingRunner(unittest.TextTestRunner):
            def __init__(self, events):
                super(LoggingRunner, self).__init__(io.StringIO())
                self._events = events

            def _makeResult(self):
                return LoggingTextResult(self._events)

        events = []
        runner = LoggingRunner(events)
        runner.run(unittest.TestSuite())
        expected = ['startTestRun', 'stopTestRun']
        self.assertEqual(events, expected) 
Example 4
Project: verge3d-blender-addon   Author: Soft8Soft   File: support.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _run_suite(suite):
    """Run tests from a unittest.TestSuite-derived class."""
    if verbose:
        runner = unittest.TextTestRunner(sys.stdout, verbosity=2,
                                         failfast=failfast)
    else:
        runner = BasicTestRunner()

    result = runner.run(suite)
    if not result.wasSuccessful():
        if len(result.errors) == 1 and not result.failures:
            err = result.errors[0][1]
        elif len(result.failures) == 1 and not result.errors:
            err = result.failures[0][1]
        else:
            err = "multiple errors occurred"
            if not verbose: err += "; run in verbose mode for details"
        raise TestFailed(err) 
Example 5
Project: browserscope   Author: elsigh   File: gaeunit.py    License: Apache License 2.0 6 votes vote down vote up
def _render_plain(self, package_name, test_name):
        self.response.headers["Content-Type"] = "text/plain"
        runner = unittest.TextTestRunner(self.response.out)
        suite, error = _create_suite(package_name, test_name, _LOCAL_TEST_DIR)
        if not error:
            self.response.out.write("====================\n" \
                                    "GAEUnit Test Results\n" \
                                    "====================\n\n")
            _run_test_suite(runner, suite)
        else:
            self.error(404)
            self.response.out.write(error)


##############################################################################
# JSON test classes
############################################################################## 
Example 6
Project: github-stats   Author: lipis   File: runner.py    License: MIT License 6 votes vote down vote up
def main(sdk_path, test_path, test_pattern):
  # If the SDK path points to a Google Cloud SDK installation
  # then we should alter it to point to the GAE platform location.
  if os.path.exists(os.path.join(sdk_path, 'platform/google_appengine')):
    sdk_path = os.path.join(sdk_path, 'platform/google_appengine')

  # Make sure google.appengine.* modules are importable.
  fixup_paths(sdk_path)

  # Make sure all bundled third-party packages are available.
  import dev_appserver
  dev_appserver.fix_sys_path()

  # Loading appengine_config from the current project ensures that any
  # changes to configuration there are available to all tests (e.g.
  # sys.path modifications, namespaces, etc.)
  try:
    import appengine_config
    (appengine_config)
  except ImportError:
    print('Note: unable to import appengine_config.')

  # Discover and run tests.
  suite = unittest.loader.TestLoader().discover(test_path, test_pattern)
  return unittest.TextTestRunner(verbosity=2).run(suite) 
Example 7
Project: throttle   Author: rbarrois   File: setup.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def run(self):
        """Run the test suite."""
        try:
            import unittest2 as unittest
        except ImportError:
            import unittest

        if self.verbose:
            verbosity=1
        else:
            verbosity=0

        loader = unittest.defaultTestLoader
        suite = unittest.TestSuite()

        if self.test_suite == self.DEFAULT_TEST_SUITE:
            for test_module in loader.discover('.'):
                suite.addTest(test_module)
        else:
            suite.addTest(loader.loadTestsFromName(self.test_suite))

        result = unittest.TextTestRunner(verbosity=verbosity).run(suite)

        if not result.wasSuccessful():
            sys.exit(1) 
Example 8
Project: pandoc-xnos   Author: tomduck   File: test.py    License: GNU General Public License v3.0 6 votes vote down vote up
def main():
    """Runs the suite of unit tests"""

    # Do the tests
    suite = unittest.TestSuite()
    suite.addTests(unittest.makeSuite(TestXnos))
    suite.addTests(unittest.makeSuite(TestPandocAttributes))
    result = unittest.TextTestRunner(verbosity=1).run(suite)
    n_errors = len(result.errors)
    n_failures = len(result.failures)

    if n_errors or n_failures:
        print('\n\nSummary: %d errors and %d failures reported\n'%\
            (n_errors, n_failures))

    print()

    sys.exit(n_errors+n_failures) 
Example 9
Project: everyclass-server   Author: everyclass   File: server.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def test():
    """Run the unit tests."""
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests) 
Example 10
Project: macops   Author: google   File: setup.py    License: Apache License 2.0 5 votes vote down vote up
def run(self):
    suite = unittest.defaultTestLoader.discover('.', pattern='*_test.py')
    unittest.TextTestRunner().run(suite) 
Example 11
Project: find_forks   Author: frost-nzcr4   File: run.py    License: MIT License 5 votes vote down vote up
def main():
    """Main function to run as shell script."""
    loader = unittest.TestLoader()
    suite = loader.discover(path.abspath(path.dirname(__file__)), pattern='test_*.py')
    runner = unittest.TextTestRunner(buffer=True)
    runner.run(suite) 
Example 12
Project: grimoirelab-sortinghat   Author: chaoss   File: setup.py    License: GNU General Public License v3.0 5 votes vote down vote up
def run_tests(self):
        test_suite = unittest.TestLoader().discover('.', pattern='test*.py')
        result = unittest.TextTestRunner(buffer=True).run(test_suite)
        sys.exit(not result.wasSuccessful()) 
Example 13
Project: toolium   Author: Telefonica   File: test_test_cases.py    License: Apache License 2.0 5 votes vote down vote up
def run_mock(test_name):
    """Run a unit test from mock class

    :param test_name: test name that must be executed
    :returns: test instance
    """
    # BasicTestCase import should be inside method to avoid collecting tests error in behave with python 3.7
    from toolium.test_cases import BasicTestCase

    class MockTestClass(BasicTestCase):
        def setUp(self):
            root_path = os.path.dirname(os.path.realpath(__file__))
            self.config_files.set_config_directory(os.path.join(root_path, 'conf'))
            super(MockTestClass, self).setUp()

        def mock_pass(self):
            pass

        def mock_fail(self):
            raise AssertionError('test error')

    suite = unittest.TestSuite()
    test = MockTestClass(test_name)
    suite.addTest(test)
    unittest.TextTestRunner().run(suite)
    return test 
Example 14
Project: myhdl   Author: myhdl   File: test_all.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def main():
    unittest.main(defaultTest='suite',
                  testRunner=unittest.TextTestRunner(verbosity=2)) 
Example 15
Project: myhdl   Author: myhdl   File: test_all.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def main():
    unittest.main(defaultTest='suite',
                  testRunner=unittest.TextTestRunner(verbosity=2)) 
Example 16
Project: myhdl   Author: myhdl   File: test_all.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def main():
    unittest.main(defaultTest='suite',
                  testRunner=unittest.TextTestRunner(verbosity=2)) 
Example 17
Project: myhdl   Author: myhdl   File: test_all.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def main():
    unittest.main(defaultTest='suite',
                  testRunner=unittest.TextTestRunner(verbosity=2)) 
Example 18
Project: jawfish   Author: war-and-code   File: test_runner.py    License: MIT License 5 votes vote down vote up
def test_init(self):
        runner = unittest.TextTestRunner()
        self.assertFalse(runner.failfast)
        self.assertFalse(runner.buffer)
        self.assertEqual(runner.verbosity, 1)
        self.assertEqual(runner.warnings, None)
        self.assertTrue(runner.descriptions)
        self.assertEqual(runner.resultclass, unittest.TextTestResult) 
Example 19
Project: jawfish   Author: war-and-code   File: test_runner.py    License: MIT License 5 votes vote down vote up
def testBufferAndFailfast(self):
        class Test(unittest.TestCase):
            def testFoo(self):
                pass
        result = unittest.TestResult()
        runner = unittest.TextTestRunner(stream=io.StringIO(), failfast=True,
                                           buffer=True)
        # Use our result object
        runner._makeResult = lambda: result
        runner.run(Test('testFoo'))

        self.assertTrue(result.failfast)
        self.assertTrue(result.buffer) 
Example 20
Project: jawfish   Author: war-and-code   File: test_runner.py    License: MIT License 5 votes vote down vote up
def test_works_with_result_without_startTestRun_stopTestRun(self):
        class OldTextResult(ResultWithNoStartTestRunStopTestRun):
            separator2 = ''
            def printErrors(self):
                pass

        class Runner(unittest.TextTestRunner):
            def __init__(self):
                super(Runner, self).__init__(io.StringIO())

            def _makeResult(self):
                return OldTextResult()

        runner = Runner()
        runner.run(unittest.TestSuite()) 
Example 21
Project: jawfish   Author: war-and-code   File: test_runner.py    License: MIT License 5 votes vote down vote up
def test_resultclass(self):
        def MockResultClass(*args):
            return args
        STREAM = object()
        DESCRIPTIONS = object()
        VERBOSITY = object()
        runner = unittest.TextTestRunner(STREAM, DESCRIPTIONS, VERBOSITY,
                                         resultclass=MockResultClass)
        self.assertEqual(runner.resultclass, MockResultClass)

        expectedresult = (runner.stream, DESCRIPTIONS, VERBOSITY)
        self.assertEqual(runner._makeResult(), expectedresult) 
Example 22
Project: jawfish   Author: war-and-code   File: test_runner.py    License: MIT License 5 votes vote down vote up
def testStdErrLookedUpAtInstantiationTime(self):
        # see issue 10786
        old_stderr = sys.stderr
        f = io.StringIO()
        sys.stderr = f
        try:
            runner = unittest.TextTestRunner()
            self.assertTrue(runner.stream.stream is f)
        finally:
            sys.stderr = old_stderr 
Example 23
Project: jawfish   Author: war-and-code   File: test_runner.py    License: MIT License 5 votes vote down vote up
def testSpecifiedStreamUsed(self):
        # see issue 10786
        f = io.StringIO()
        runner = unittest.TextTestRunner(f)
        self.assertTrue(runner.stream.stream is f) 
Example 24
Project: jawfish   Author: war-and-code   File: test_program.py    License: MIT License 5 votes vote down vote up
def test_NonExit(self):
        program = unittest.main(exit=False,
                                argv=["foobar"],
                                testRunner=unittest.TextTestRunner(stream=io.StringIO()),
                                testLoader=self.FooBarLoader())
        self.assertTrue(hasattr(program, 'result')) 
Example 25
Project: jawfish   Author: war-and-code   File: test_program.py    License: MIT License 5 votes vote down vote up
def test_ExitAsDefault(self):
        self.assertRaises(
            SystemExit,
            unittest.main,
            argv=["foobar"],
            testRunner=unittest.TextTestRunner(stream=io.StringIO()),
            testLoader=self.FooBarLoader()) 
Example 26
Project: jawfish   Author: war-and-code   File: test_result.py    License: MIT License 5 votes vote down vote up
def testFailFastSetByRunner(self):
        runner = unittest.TextTestRunner(stream=io.StringIO(), failfast=True)
        def test(result):
            self.assertTrue(result.failfast)
        result = runner.run(test) 
Example 27
Project: jawfish   Author: war-and-code   File: test_result.py    License: MIT License 5 votes vote down vote up
def testOldResultWithRunner(self):
        class Test(unittest.TestCase):
            def testFoo(self):
                pass
        runner = unittest.TextTestRunner(resultclass=OldResult,
                                          stream=io.StringIO())
        # This will raise an exception if TextTestRunner can't handle old
        # test result objects
        runner.run(Test('testFoo')) 
Example 28
Project: jawfish   Author: war-and-code   File: test_setups.py    License: MIT License 5 votes vote down vote up
def getRunner(self):
        return unittest.TextTestRunner(resultclass=resultFactory,
                                          stream=io.StringIO()) 
Example 29
Project: jawfish   Author: war-and-code   File: test_break.py    License: MIT License 5 votes vote down vote up
def testRunner(self):
        # Creating a TextTestRunner with the appropriate argument should
        # register the TextTestResult it creates
        runner = unittest.TextTestRunner(stream=io.StringIO())

        result = runner.run(unittest.TestSuite())
        self.assertIn(result, unittest.signals._results) 
Example 30
Project: OpenTrader   Author: OpenTrading   File: cmd2plus.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def runTranscriptTests(self, callargs):
        class TestMyAppCase(Cmd2TestCase):
            CmdApp = self.__class__
        self.__class__.testfiles = callargs
        sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main()
        testcase = TestMyAppCase()
        runner = unittest.TextTestRunner()
        result = runner.run(testcase)
        result.printErrors()