def main(testing=False, coverage_testing=False): parser = argparse.ArgumentParser( add_help=False, description="Green is a clean, colorful test runner for Python unit tests.") target_args = parser.add_argument_group("Target Specification") target_args.add_argument('targets', action='store', nargs='*', default=['.'], help=("""Targets to test. If blank, then discover all testcases in the current directory tree. Can be a directory (or package), file (or module), or fully-qualified 'dotted name' like proj.tests.test_things.TestStuff. If a directory (or package) is specified, then we will attempt to discover all tests under the directory (even if the directory is a package and the tests would not be accessible through the package's scope). In all other cases, only tests accessible from introspection of the object will be loaded.""")) concurrency_args = parser.add_argument_group("Concurrency Options") concurrency_args.add_argument('-s', '--subprocesses', action='store', type=int, default=1, metavar='NUM', help="Number of subprocesses to use to run tests. Note that your " "tests need to be written to avoid using the same resources (temp " "files, sockets, ports, etc.) for the multi-process mode to work " "well. Default is 1, meaning try to autodetect the number of CPUs " "in the system. 1 will disable using subprocesses. Note that for " "trivial tests (tests that take < 1ms), running everything in a " "single process may be faster.") format_args = parser.add_argument_group("Format Options") format_args.add_argument('-m', '--html', action='store_true', default=False, help="HTML5 format. Overrides terminal color options if specified.") format_args.add_argument('-t', '--termcolor', action='store_true', default=None, help="Force terminal colors on. Default is to autodetect.") format_args.add_argument('-T', '--notermcolor', action='store_true', default=None, help="Force terminal colors off. Default is to autodetect.") out_args = parser.add_argument_group("Output Options") out_args.add_argument('-d', '--debug', action='count', default=0, help=("Enable internal debugging statements. Implies --logging. Can " "be specified up to three times for more debug output.")) out_args.add_argument('-h', '--help', action='store_true', default=False, help="Show this help message and exit.") out_args.add_argument('-l', '--logging', action='store_true', default=False, help="Don't configure the root logger to redirect to /dev/null") out_args.add_argument('-V', '--version', action='store_true', default=False, help="Print the version of Green and Python and exit.") out_args.add_argument('-v', '--verbose', action='count', default=1, help=("Verbose. Can be specified up to three times for more verbosity. " "Recommended levels are -v and -vv.")) cov_args = parser.add_argument_group( "Coverage Options ({})".format(coverage_version)) cov_args.add_argument('-r', '--run-coverage', action='store_true', default=False, help=("Produce coverage output.")) cov_args.add_argument('-o', '--omit', action='store', default=None, metavar='PATTERN', help=("Comma-separated file-patterns to omit from coverage. Default " "is something like '*/test*,*/termstyle*,*/mock*,*(temp " "dir)*,*(python system packages)*'")) args = parser.parse_args() # Clear out all the passed-in-options just in case someone tries to run a # test that assumes sys.argv is clean. I can't guess at the script name # that they want, though, so we'll just leave ours. sys.argv = sys.argv[:1] # Help? if args.help: # pragma: no cover parser.print_help() return 0 # Just print version and exit? if args.version: from green.version import pretty_version sys.stdout.write(pretty_version()+'\n') return 0 # Handle logging options if args.debug: logging.basicConfig( level=logging.DEBUG, format="%(asctime)s %(levelname)9s %(message)s") elif not args.logging: logging.basicConfig(filename=os.devnull) # These options both disable termcolor if args.html or args.notermcolor: args.termcolor = False # Coverage? omit = [] if args.run_coverage: if args.omit: omit = args.omit.split(',') else: omit = [ '*/test*', '*/termstyle*', '*/mock*', tempfile.gettempdir() + '*'] if 'green' not in args.targets and (False in [t.startswith('green.') for t in args.targets]): omit.extend([ '*Python.framework*', '*site-packages*']) if not coverage: sys.stderr.write( "Fatal: The 'coverage' module is not installed. Have you " "run 'pip install coverage'???") return 3 if (not testing) or coverage_testing: cov = coverage.coverage(data_file='.coverage', omit=omit) cov.start() # Set up our various main objects from green.loader import getTests from green.runner import GreenTestRunner from green.output import GreenStream import green.output if args.debug: green.output.debug_level = args.debug stream = GreenStream(sys.stderr, html = args.html) runner = GreenTestRunner(verbosity = args.verbose, stream = stream, termcolor=args.termcolor, subprocesses=args.subprocesses, run_coverage=args.run_coverage, omit=omit) # Discover/Load the TestSuite tests = getTests(args.targets) # We didn't even load 0 tests... if not tests: logging.debug( "No test loading attempts succeeded. Created an empty test suite.") tests = unittest.suite.TestSuite() # Actually run the tests if testing: result = lambda: None result.wasSuccessful = lambda: 0 else: result = runner.run(tests) # pragma: no cover if args.run_coverage and ((not testing) or coverage_testing): stream.writeln() cov.stop() cov.save() cov.combine() cov.save() cov.report(file=stream, omit=omit) return(int(not result.wasSuccessful()))
def main(testing=False, coverage_testing=False): args = config.parseArguments() args = config.mergeConfig(args, testing, coverage_testing) if getattr(args, 'html', False): # pragma: no cover print(""" The --html flag is scheduled to be removed in version 2.0 due to it being a pain to maintain and no one using it. If you actually use it, please open an issue stating so! https://github.com/CleanCut/green/issues/new Unless some people request it, it will be removed in 2.0 """) import time time.sleep(2) if args.shouldExit: return args.exitCode # Clear out all the passed-in-options just in case someone tries to run a # test that assumes sys.argv is clean. I can't guess at the script name # that they want, though, so we'll just leave ours. sys.argv = sys.argv[:1] # Set up our various main objects from green.loader import loadTargets from green.runner import run from green.output import GreenStream, debug import green.output from green.suite import GreenTestSuite GreenTestSuite.args = args if args.debug: green.output.debug_level = args.debug stream = GreenStream(sys.stdout, html = args.html) # Location of shell completion file if args.completion_file: print(os.path.join(os.path.dirname(__file__), 'shell_completion.sh')) return 0 # Argument-completion for bash and zsh (for test-target completion) if args.completions: from green.loader import getCompletions print(getCompletions(args.targets)) return 0 # Option-completion for bash and zsh if args.options: print('\n'.join(sorted(args.store_opt.options))) return 0 # Add debug logging for stuff that happened before this point here if config.files_loaded: debug("Loaded config file(s): {}".format( ', '.join(config.files_loaded))) # Discover/Load the test suite if testing: test_suite = None else: test_suite = loadTargets(args.targets, file_pattern = args.file_pattern) # We didn't even load 0 tests... if not test_suite: debug( "No test loading attempts succeeded. Created an empty test suite.") test_suite = GreenTestSuite() # Actually run the test_suite if testing: result = lambda: None result.wasSuccessful = lambda: 0 else: result = run(test_suite, stream, args) # pragma: no cover if args.run_coverage and ((not testing) or coverage_testing): stream.writeln() args.cov.stop() args.cov.save() args.cov.combine() args.cov.save() args.cov.report(file=stream, omit=args.omit_patterns) return(int(not result.wasSuccessful()))
def main(testing=False, coverage_testing=False): args = config.parseArguments() args = config.mergeConfig(args, testing, coverage_testing) if args.shouldExit: return args.exitCode # Clear out all the passed-in-options just in case someone tries to run a # test that assumes sys.argv is clean. I can't guess at the script name # that they want, though, so we'll just leave ours. sys.argv = sys.argv[:1] # Set up our various main objects from green.loader import loadTargets from green.runner import GreenTestRunner from green.output import GreenStream, debug import green.output if args.debug: green.output.debug_level = args.debug stream = GreenStream(sys.stdout, html = args.html) runner = GreenTestRunner(verbosity = args.verbose, stream = stream, termcolor=args.termcolor, subprocesses=args.subprocesses, run_coverage=args.run_coverage, omit=args.omit) # Location of shell completion file if args.completion_file: print(os.path.join(os.path.dirname(__file__), 'shell_completion.sh')) return 0 # Argument-completion for bash and zsh (for test-target completion) if args.completions: from green.loader import getCompletions print(getCompletions(args.targets)) return 0 # Option-completion for bash and zsh if args.options: print('\n'.join(sorted(args.store_opt.options))) return 0 # Add debug logging for stuff that happened before this point here if config.files_loaded: debug("Loaded config file(s): {}".format( ', '.join(config.files_loaded))) # Discover/Load the TestSuite if testing: test_suite = None else: test_suite = loadTargets(args.targets) # We didn't even load 0 tests... if not test_suite: debug( "No test loading attempts succeeded. Created an empty test suite.") test_suite = unittest.suite.TestSuite() # Actually run the test_suite if testing: result = lambda: None result.wasSuccessful = lambda: 0 else: result = runner.run(test_suite) # pragma: no cover if args.run_coverage and ((not testing) or coverage_testing): stream.writeln() args.cov.stop() args.cov.save() args.cov.combine() args.cov.save() args.cov.report(file=stream, omit=args.omit) return(int(not result.wasSuccessful()))