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 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_testmoduleOnModule(self): """ Check that --testmodule loads a suite which contains the tests referred to in test-case-name inside its parameter. """ self.config.opt_testmodule(sibpath('moduletest.py')) self.assertSuitesEqual(trial._getSuite(self.config), ['twisted.trial.test.test_test_visitor'])
def test_testmoduleOnSelfModule(self): """ When given a module that refers to *itself* in the test-case-name variable, check that --testmodule only adds the tests once. """ self.config.opt_testmodule(sibpath('moduleself.py')) self.assertSuitesEqual(trial._getSuite(self.config), ['twisted.trial.test.moduleself'])
def test_testmoduleOnModule(self): """ Check that --testmodule loads a suite which contains the tests referred to in test-case-name inside its parameter. """ self.config.opt_testmodule(sibpath('moduletest.py')) self.assertSuitesEqual(trial._getSuite(self.config), ['twisted.trial.test.test_log'])
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_testmoduleOnScript(self): """ Check that --testmodule loads tests referred to in test-case-name buffer variables. """ self.config.opt_testmodule(sibpath('scripttest.py')) self.assertSuitesEqual( trial._getSuite(self.config), ['twisted.trial.test.test_log', 'twisted.trial.test.test_runner'])
def test_testmoduleTwice(self): """ When the same module is specified with two --testmodule flags, it should only appear once in the suite. """ self.config.opt_testmodule(sibpath('moduletest.py')) self.config.opt_testmodule(sibpath('moduletest.py')) self.assertSuitesEqual(trial._getSuite(self.config), ['twisted.trial.test.test_test_visitor'])
def test_testmoduleTwice(self): """ When the same module is specified with two --testmodule flags, it should only appear once in the suite. """ self.config.opt_testmodule(sibpath('moduletest.py')) self.config.opt_testmodule(sibpath('moduletest.py')) self.assertSuitesEqual(trial._getSuite(self.config), ['twisted.trial.test.test_log'])
def test_testmoduleOnScript(self): """ Check that --testmodule loads tests referred to in test-case-name buffer variables. """ self.config.opt_testmodule(sibpath('scripttest.py')) self.assertSuitesEqual(trial._getSuite(self.config), ['twisted.trial.test.test_test_visitor', 'twisted.trial.test.test_class'])
def test_testmoduleOnSourceAndTarget(self): """ If --testmodule is specified twice, once for module A and once for a module which refers to module A, then make sure module A is only added once. """ self.config.opt_testmodule(sibpath('moduletest.py')) self.config.opt_testmodule(sibpath('test_test_visitor.py')) self.assertSuitesEqual(trial._getSuite(self.config), ['twisted.trial.test.test_test_visitor'])
def test_testmoduleOnSourceAndTarget(self): """ If --testmodule is specified twice, once for module A and once for a module which refers to module A, then make sure module A is only added once. """ self.config.opt_testmodule(sibpath('moduletest.py')) self.config.opt_testmodule(sibpath('test_log.py')) self.assertSuitesEqual(trial._getSuite(self.config), ['twisted.trial.test.test_log'])
def runTrial(*args): from twisted.trial import reporter config = trial.Options() config.parseOptions(args) output = NativeStringIO() myRunner = runner.TrialRunner(reporter.VerboseTextReporter, stream=output, workingDirectory=config['temp-directory']) suite = trial._getSuite(config) myRunner.run(suite) return output.getvalue()
def runTrial(*args): from twisted.trial import reporter config = trial.Options() config.parseOptions(args) output = StringIO.StringIO() myRunner = runner.TrialRunner( reporter.VerboseTextReporter, stream=output, workingDirectory=config['temp-directory']) suite = trial._getSuite(config) result = myRunner.run(suite) return output.getvalue()
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 setUp(self): self.managerTransport = StringTransport() self.managerAMP = LocalWorkerAMP() self.managerAMP.makeConnection(self.managerTransport) self.result = TestResult() self.workerTransport = StringTransport() self.worker = AMP() self.worker.makeConnection(self.workerTransport) config = trial.Options() self.testName = "twisted.doesnexist" config['tests'].append(self.testName) self.testCase = trial._getSuite(config)._tests.pop() self.managerAMP.run(self.testCase, self.result) self.managerTransport.clear()
def setUp(self): self.managerTransport = StringTransport() self.managerAMP = LocalWorkerAMP() self.managerAMP.makeConnection(self.managerTransport) self.result = TestResult() self.workerTransport = StringTransport() self.worker = AMP() self.worker.makeConnection(self.workerTransport) config = trial.Options() self.testName = "twisted.doesnexist" config["tests"].append(self.testName) self.testCase = trial._getSuite(config)._tests.pop() self.managerAMP.run(self.testCase, self.result) self.managerTransport.clear()
def _tests_func(self, tests, worker_index): if not isinstance(tests, (list, set)): tests = [tests] args = ['-e'] args.extend(tests) config = Options() config.parseOptions(args) stream = BufferWritesDevice() runner = self._make_runner(config=config, stream=stream) suite = _getSuite(config) result = setup_test_db(worker_index, None, runner.run, suite) result = TestResult().from_trial_result(result) return result
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 test_preserveArgumentOrder(self): """ Multiple tests passed on the command line are not reordered. """ tests = [ "twisted.trial.test.test_tests", "twisted.trial.test.test_assertions", "twisted.trial.test.test_deferred", ] self.config.parseOptions(tests) suite = trial._getSuite(self.config) names = testNames(suite) expectedSuite = TestSuite(map(self.loader.loadByName, tests)) expectedNames = testNames(expectedSuite) self.assertEqual(names, expectedNames)
def test_preserveArgumentOrder(self): """ Multiple tests passed on the command line are not reordered. """ tests = [ "twisted.trial.test.test_tests", "twisted.trial.test.test_assertions", "twisted.trial.test.test_deferreds", ] self.config.parseOptions(tests) suite = trial._getSuite(self.config) names = testNames(suite) expectedSuite = TestSuite(map(self.loader.loadByName, tests)) expectedNames = testNames(expectedSuite) self.assertEqual(names, expectedNames)
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 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
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 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
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