def __init__(self, test_run_dir, source_dir, from_source=False): """ Arguments: test_run_dir: the "sandbox" directory in which to write temporary test data (for example the output directory of rendering a Groome template directory to compare with an expected directory). source_dir: the path to a source distribution or source checkout, or None if one is not available (e.g. if running from a package install). from_source: whether or not the script was initiated from a source checkout (e.g. by calling `python -m molt.commands.molt` as opposed to via an installed setup entry point). """ python_path = sys.executable # Call molt the "same" way that the current script execution # was initiated. call_molt_args = ([molt.__name__] if not from_source else [python_path, '-m', molt.commands.molt.__name__]) locator = Locator(source_dir) self.call_molt_args = call_molt_args self.groome_tests_dir = locator.groome_tests_dir() self.test_run_dir = test_run_dir
def run_molt_tests(from_source, source_dir=None, verbose=False, test_names=None, test_output_dir=None, test_runner_stream=None): """ Run all project tests, and return a unittest.TestResult instance. Arguments: from_source: whether or not the script was initiated from a source checkout (e.g. by calling `python -m molt.commands.molt` as opposed to via an installed setup entry point). source_dir: the path to a source distribution or source checkout, or None if one is not available (e.g. if running from a package install). test_names: the list of test-name prefixes to filter tests by. If None, all available tests are run. test_output_dir: the directory in which to leave test expectation failures. If None, test runs are written to a system temp directory and test expectation failures deleted. test_runner_stream: the stream object to pass to unittest.TextTestRunner. Defaults to sys.stderr. """ _log.info("running tests: from_source: %s" % from_source) if test_runner_stream is None: test_runner_stream = sys.stderr if test_output_dir is not None and not os.path.exists(test_output_dir): _log.info("creating test output dir: %s" % test_output_dir) os.makedirs(test_output_dir) locator = Locator(source_dir) doctest_paths = locator.doctest_paths() extra_package_dirs = locator.extra_package_dirs() for package_dir in extra_package_dirs: dir_path, package_name = os.path.split(package_dir) # This allows us to import the extra packages later on. _log.info("Adding to sys.path for %s: %s" % (package_name, repr(dir_path))) # TODO: we only need to do this when source_dir is provided. sys.path.append(dir_path) package_dirs = [os.path.dirname(molt.__file__)] + extra_package_dirs test_run_dir = make_test_run_dir(test_output_dir) # TODO: also add support for --quiet. verbosity = 2 if verbose else 1 test_config = TestConfig(test_run_dir, source_dir, from_source=from_source) try: test_result = run_tests(package_dirs=package_dirs, is_unittest_module=IS_UNITTEST_MODULE, test_config=test_config, doctest_paths=doctest_paths, verbosity=verbosity, test_runner_stream=test_runner_stream, test_names=test_names) finally: if test_output_dir is None or is_empty(test_run_dir): _log.info("cleaning up: deleting: %s" % test_run_dir) rmtree(test_run_dir) test_run_dir = None else: _log.info("test failures at: %s" % test_run_dir) return test_result, test_run_dir