Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main(test_module, exit = None, buffer = None):
import green.loader, green.runner
if buffer:
green_args.quiet_stdout = True
try:
suite = green.loader.GreenTestLoader().loadTestsFromModule(test_module) # new API (v2.9+)
except AttributeError:
suite = green.loader.loadFromModule(test_module) # legacy API
results = green.runner.run(suite, sys.stdout, green_args)
# Return the results in an object with a "result" attribute, same as unittest.main()
return collections.namedtuple("Tuple", "result")(results)
def start_callback(test):
# Let the main process know what test we are starting
test = proto_test(test)
if test not in already_sent:
queue.put(test)
already_sent.add(test)
def finalize_callback(test_result):
# Let the main process know what happened with the test run
queue.put(test_result)
result = ProtoTestResult(start_callback, finalize_callback)
test = None
try:
loader = GreenTestLoader()
test = loader.loadTargets(target)
except:
err = sys.exc_info()
t = ProtoTest()
t.module = 'green.loader'
t.class_name = 'N/A'
t.description = 'Green encountered an error loading the unit test.'
t.method_name = 'poolRunner'
result.startTest(t)
result.addError(t, err)
result.stopTest(t)
queue.put(result)
cleanup()
return
if getattr(test, 'run', False):
def __init__(self, verbose=-1, **kwargs):
super(DjangoRunner, self).__init__(**kwargs)
self.verbose = verbose
self.loader = GreenTestLoader()
# 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: # pragma: no cover
loader = GreenTestLoader()
test_suite = loader.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
result = run(test_suite, stream, args, testing)
# Generate a test report if required
if args.junit_report:
from green.junit import JUnitXML
adapter = JUnitXML()
def loadTestsFromModule(self, module):
tests = super(GreenTestLoader, self).loadTestsFromModule(module)
return flattenTestSuite(tests)
def getCompletions(target):
# This option expects 0 or 1 targets
if type(target) == list:
target = target[0]
if target == '.':
target = ''
# Discover tests and load them into a suite
# First try the completion as-is. It might be at a valid spot.
loader = GreenTestLoader()
test_suite = loader.loadTargets(target)
if not test_suite:
# Next, try stripping to the previous '.'
last_dot_idx = target.rfind('.')
to_complete = None
if last_dot_idx > 0:
to_complete = target[:last_dot_idx]
elif len(target):
# Oops, there was no previous '.' -- try filesystem matches
to_complete = glob.glob(target + '*')
if not to_complete:
to_complete = '.'
test_suite = loader.loadTargets(to_complete)
# Reduce the suite to a list of relevant dotted names
dotted_names = set()