def makeRunner(self): """ Return a L{runner.TrialRunner} object that is safe to use in tests. """ runner = trial._makeRunner(self.config) runner.stream = StringIO.StringIO() return runner
def call_trial(*tests, **kwargs): import spyne.test try: import twisted.scripts.trial except ImportError: return 1 global _ctr _ctr += 1 file_name = 'test_result.%d.subunit' % _ctr with SubUnitTee(file_name): tests_dir = os.path.dirname(spyne.test.__file__) sys.argv = ['trial', '--reporter=subunit'] sys.argv.extend(chain(*[glob(join(tests_dir, test)) for test in tests])) from twisted.scripts.trial import Options from twisted.scripts.trial import _makeRunner from twisted.scripts.trial import _getSuite config = Options() config.parseOptions() trialRunner = _makeRunner(config) suite = _getSuite(config) test_result = trialRunner.run(suite) try: subunit2junitxml(_ctr) except Exception as e: # this is not super important. print(e) return int(not test_result.wasSuccessful())
def makeRunner(self): """ Return a L{TrialRunner} object that is safe to use in tests. """ runner = trial._makeRunner(self.config) runner.stream = NativeStringIO() return runner
def call_trial(*tests): import spyne.test from glob import glob from itertools import chain global _ctr _ctr += 1 file_name = 'test_result.%d.subunit' % _ctr with SubUnitTee(file_name): tests_dir = os.path.dirname(spyne.test.__file__) sys.argv = ['trial', '--reporter=subunit'] sys.argv.extend(chain(*[glob(join(tests_dir, test)) for test in tests])) from twisted.scripts.trial import Options from twisted.scripts.trial import _makeRunner from twisted.scripts.trial import _getSuite config = Options() config.parseOptions() trialRunner = _makeRunner(config) suite = _getSuite(config) test_result = trialRunner.run(suite) try: subunit2junitxml(_ctr) except Exception as e: # this is not super important. print e return int(not test_result.wasSuccessful())
def test_exitfirst(self): """ Passing C{--exitfirst} wraps the reporter with a L{reporter._ExitWrapper} that stops on any non-success. """ self.options.parseOptions(["--exitfirst"]) runner = trial._makeRunner(self.options) self.assertTrue(runner._exitFirst)
def run(): config = Options() config.parseOptions() trialRunner = _makeRunner(config) suite = _getSuite(config) test_result = trialRunner.run(suite) return int(not test_result.wasSuccessful())
def test_dryRunWithJobs(self): """ L{_makeRunner} returns a L{TrialRunner} instance in C{DRY_RUN} mode when the C{--dry-run} option is passed, even if C{--jobs} is set. """ self.options.parseOptions(["--jobs", "4", "--dry-run"]) runner = trial._makeRunner(self.options) self.assertIsInstance(runner, TrialRunner) self.assertEqual(TrialRunner.DRY_RUN, runner.mode)
def test_jobs(self): """ L{_makeRunner} returns a L{DistTrialRunner} instance when the C{--jobs} option is passed, and passes the C{workerNumber} and C{workerArguments} parameters to it. """ self.options.parseOptions(["--jobs", "4", "--force-gc"]) runner = trial._makeRunner(self.options) self.assertIsInstance(runner, DistTrialRunner) self.assertEqual(4, runner._workerNumber) self.assertEqual(["--force-gc"], runner._workerArguments)
def trialMain(testClass): from twisted.trial import runner from twisted.scripts import trial as script config = script.Options() config.parseOptions() trialRunner = script._makeRunner(config) if len(sys.argv)>1: suite = runner.TestSuite() for t in sys.argv[1:]: suite.addTest(testClass(t)) else: sys.argv.append(sys.argv[0]) config.parseOptions() suite = script._getSuite(config) trialRunner.run(suite)
def getRunner(self): r = trial._makeRunner(self.config) r.stream = NativeStringIO() # XXX The runner should always take care of cleaning this up itself. # It's not clear why this is necessary. The runner always tears down # its log file. self.addCleanup(r._tearDownLogFile) # XXX The runner should always take care of cleaning this up itself as # well. It's necessary because TrialRunner._setUpTestdir might raise # an exception preventing Reporter.done from being run, leaving the # observer added by Reporter.__init__ still present in the system. # Something better needs to happen inside # TrialRunner._runWithoutDecoration to remove the need for this cludge. r._log = log.LogPublisher() return r
def getRunner(self): r = trial._makeRunner(self.config) r.stream = StringIO.StringIO() # XXX The runner should always take care of cleaning this up itself. # It's not clear why this is necessary. The runner always tears down # its log file. self.addCleanup(r._tearDownLogFile) # XXX The runner should always take care of cleaning this up itself as # well. It's necessary because TrialRunner._setUpTestdir might raise # an exception preventing Reporter.done from being run, leaving the # observer added by Reporter.__init__ still present in the system. # Something better needs to happen inside # TrialRunner._runWithoutDecoration to remove the need for this cludge. r._log = log.LogPublisher() return r
def test_basic_test(self): pkgname='fakepackage4' modname='fakemodule4' modcontents='\n\ def foofunc():\n\ x=1\n\ y=x\n\ ' testcontents='\n\ from twisted.trial import unittest\n\ from %s import %s\n\ class T(unittest.TestCase):\n\ def test_thing(self):\n\ %s.foofunc()\n\ ' % (pkgname, modname, modname) mockstdout = Mock() realstdout=sys.stdout sys.stdout = mockstdout mockstderr = Mock() realstderr=sys.stderr sys.stderr = mockstderr something = None try: fileutil.make_dirs(pkgname) fileutil.write_file(os.path.join(pkgname, '__init__.py'), '') fileutil.write_file(os.path.join(pkgname, modname+'.py'), modcontents) fileutil.make_dirs(os.path.join(pkgname, 'test')) fileutil.write_file(os.path.join(pkgname, 'test', '__init__.py'), '') fileutil.write_file(os.path.join(pkgname, 'test', 'test_'+modname+'.py'), testcontents) sys.path.append(os.getcwd()) trialcoverage.init_paths() trialcoverage.start_coverage() config = trial.Options() config.parseOptions(['--reporter', 'bwverbose-coverage', '%s.test' % pkgname]) trial._initialDebugSetup(config) trialRunner = trial._makeRunner(config) suite = trial._getSuite(config) something = trialRunner.run(suite) finally: sys.stdout = realstdout sys.stderr = realstderr if sys.modules.has_key(pkgname): del sys.modules[pkgname] fileutil.rm_dir(pkgname)
def run_tests(self): # We do the import from Twisted inside the function instead of the top # of the file because since Twisted is a setup_requires, we can't # assume that Twisted will be installed on the user's system prior, so # if we don't do the import here, then importing from this plugin will # fail. from twisted.scripts import trial if not self.testmodule: self.testmodule = "bridgedb.test" # Handle parsing the trial options passed through the setuptools # trial command. cmd_options = [] for opt in self.boolean_options: if getattr(self, opt.replace('-', '_'), None): cmd_options.append('--%s' % opt) for opt in ('debugger', 'jobs', 'random', 'reactor', 'reporter', 'testmodule', 'tbformat', 'without-module'): value = getattr(self, opt.replace('-', '_'), None) if value is not None: cmd_options.extend(['--%s' % opt, value]) config = trial.Options() config.parseOptions(cmd_options) config['tests'] = [ self.testmodule, ] trial._initialDebugSetup(config) trialRunner = trial._makeRunner(config) suite = trial._getSuite(config) # run the tests if self.until_failure: test_result = trialRunner.runUntilFailure(suite) else: test_result = trialRunner.run(suite) if test_result.wasSuccessful(): return 0 # success return 1 # failure
def run_tests(self): # We do the import from Twisted inside the function instead of the top # of the file because since Twisted is a setup_requires, we can't # assume that Twisted will be installed on the user's system prior, so # if we don't do the import here, then importing from this plugin will # fail. from twisted.scripts import trial if not self.testmodule: self.testmodule = "bridgedb.test" # Handle parsing the trial options passed through the setuptools # trial command. cmd_options = [] for opt in self.boolean_options: if getattr(self, opt.replace('-', '_'), None): cmd_options.append('--%s' % opt) for opt in ('debugger', 'jobs', 'random', 'reactor', 'reporter', 'testmodule', 'tbformat', 'without-module'): value = getattr(self, opt.replace('-', '_'), None) if value is not None: cmd_options.extend(['--%s' % opt, value]) config = trial.Options() config.parseOptions(cmd_options) config['tests'] = [self.testmodule,] trial._initialDebugSetup(config) trialRunner = trial._makeRunner(config) suite = trial._getSuite(config) # run the tests if self.until_failure: test_result = trialRunner.runUntilFailure(suite) else: test_result = trialRunner.run(suite) if test_result.wasSuccessful(): return 0 # success return 1 # failure
def getRunner(self): return trial._makeRunner(self.config)
def run_tests(self): global all_modules all_modules = self.distribution.get_command_obj( 'build_py').find_all_modules() # We do the import from Twisted inside the function instead of the top # of the file because since Twisted is a setup_requires, we can't # assume that Twisted will be installed on the user's system prior # to using Tahoe, so if we don't do the import here, then importing # from this plugin will fail. from twisted.scripts import trial # Handle parsing the trial options passed through the setuptools # trial command. cmd_options = [] if self.reactor is not None: cmd_options.extend(['--reactor', self.reactor]) else: # Cygwin requires the poll reactor to work at all. Linux # requires the poll reactor to avoid twisted bug #3218. # In general, the poll reactor is better than the select reactor, # but it is not available on all platforms. According to # exarkun on IRC, it is available but buggy on some versions of # Mac OS X, so just because you can install it doesn't mean we # want to use it on every platform. # Unfortunately this leads to this error with some # combinations of tools: # twisted.python.usage.UsageError: The specified reactor cannot be # used, failed with error: reactor already installed. if sys.platform in ("cygwin"): cmd_options.extend(['--reactor', 'poll']) if self.reporter is not None: cmd_options.extend(['--reporter', self.reporter]) if self.rterrors is not None: cmd_options.append('--rterrors') if self.debug_stacktraces is not None: cmd_options.append('--debug-stacktraces') config = trial.Options() config.parseOptions(cmd_options) args = self.test_args if type(args) == str: args = [args, ] config['tests'] = args if self.coverage: config.opt_coverage() trial._initialDebugSetup(config) trialRunner = trial._makeRunner(config) suite = trial._getSuite(config) # run the tests if self.until_failure: test_result = trialRunner.runUntilFailure(suite) else: test_result = trialRunner.run(suite) # write coverage data if config.tracer: sys.settrace(None) results = config.tracer.results() results.write_results(show_missing=1, summary=False, coverdir=config.coverdir) if test_result.wasSuccessful(): sys.exit(0) # success else: sys.exit(1) # failure
def getRunner(self): r = trial._makeRunner(self.config) self.runners.append(r) return r
r = datastore.Redis.instance() r.flushdb() else: from nova.tests.real_flags import * if len(argv) == 1 and len(config['tests']) == 0: # If no tests were specified run the ones imported in this file # NOTE(termie): "tests" is not a flag, just some Trial related stuff config['tests'].update(['__main__']) elif len(config['tests']): # If we specified tests check first whether they are in __main__ for arg in config['tests']: key = arg.split('.')[0] if hasattr(__main__, key): config['tests'].remove(arg) config['tests'].add('__main__.%s' % arg) trial_script._initialDebugSetup(config) trialRunner = trial_script._makeRunner(config) suite = trial_script._getSuite(config) if config['until-failure']: test_result = trialRunner.runUntilFailure(suite) else: test_result = trialRunner.run(suite) if config.tracer: sys.settrace(None) results = config.tracer.results() results.write_results(show_missing=1, summary=False, coverdir=config.coverdir) sys.exit(not test_result.wasSuccessful())
def run_tests(self): # We do the import from Twisted inside the function instead of the top # of the file because since Twisted is a setup_requires, we can't # assume that Twisted will be installed on the user's system prior # to using Tahoe, so if we don't do the import here, then importing # from this plugin will fail. from twisted.scripts import trial # Handle parsing the trial options passed through the setuptools # trial command. cmd_options = [] if self.reactor is not None: cmd_options.extend(['--reactor', self.reactor]) else: # Cygwin requires the poll reactor to work at all. Linux requires the poll reactor # to avoid twisted bug #3218. In general, the poll reactor is better than the # select reactor, but it is not available on all platforms. According to exarkun on # IRC, it is available but buggy on some versions of Mac OS X, so just because you # can install it doesn't mean we want to use it on every platform. # Unfortunately this leads to this error with some combinations of tools: # twisted.python.usage.UsageError: The specified reactor cannot be used, failed with error: reactor already installed. if sys.platform in ("cygwin"): cmd_options.extend(['--reactor', 'poll']) if self.reporter is not None: cmd_options.extend(['--reporter', self.reporter]) if self.rterrors is not None: cmd_options.append('--rterrors') if self.debug_stacktraces is not None: cmd_options.append('--debug-stacktraces') config = trial.Options() config.parseOptions(cmd_options) args = self.test_args if type(args) == str: args = [ args, ] config['tests'] = args if self.coverage: config.opt_coverage() trial._initialDebugSetup(config) trialRunner = trial._makeRunner(config) suite = trial._getSuite(config) # run the tests if self.until_failure: test_result = trialRunner.runUntilFailure(suite) else: test_result = trialRunner.run(suite) # write coverage data if config.tracer: sys.settrace(None) results = config.tracer.results() results.write_results(show_missing=1, summary=False, coverdir=config.coverdir) if test_result.wasSuccessful(): sys.exit(0) # success else: sys.exit(1) # failure
class CustomSuite(unittest.TestSuite): def __init__(self, tests=()): stripped = [] for test in tests: if isinstance(test, CustomSuite): stripped.append(test) elif any(member[0].startswith('test_') for member in inspect.getmembers(test)): stripped.append(test) super(CustomSuite, self).__init__(tests=stripped) if __name__ == "__main__": # We have to regen the plugins cache to be able to install # alternative reactors. Regen utility should be run in a separate # interpreter because reactors cannot be installed twice or unloaded. subprocess.call(["python", "regen_plugins_cache.py"]) config = Options() config.parseOptions() config['tbformat'] = 'verbose' config['exitfirst'] = True _initialDebugSetup(config) trialRunner = _makeRunner(config) test_loader = TestLoader() test_loader.suiteClass = CustomSuite test_suite = test_loader.discover(abspath(join('.', 'ipv8', 'test')), top_level_dir=abspath('.')) test_result = trialRunner.run(test_suite)
def getRunner(self): r = trial._makeRunner(self.config) r.stream = StringIO.StringIO() self.addCleanup(r._tearDownLogFile) return r
def main(): always_succeed = '--always-succeed' in sys.argv if always_succeed: sys.argv.remove('--always-succeed') # Copypasta from twisted.scripts.trial.run, to tweak the return values if len(sys.argv) == 1: sys.argv.append("--help") config = trial.Options() try: config.parseOptions() except usage.error, ue: raise SystemExit, "%s: %s" % (sys.argv[0], ue) trial._initialDebugSetup(config) trialRunner = trial._makeRunner(config) suite = trial._getSuite(config) if config['until-failure']: test_result = trialRunner.runUntilFailure(suite) else: test_result = trialRunner.run(suite) if config.tracer: sys.settrace(None) results = config.tracer.results() results.write_results(show_missing=1, summary=False, coverdir=config.coverdir) # Copypasta ends here if always_succeed or test_result.wasSuccessful(): return 0 else: return 2