class TestGroup(object): def __init__(self, name, classnames=None, groupSetUp=lambda: None, groupTearDown=lambda: None): self.name = name self._classes = {} for classname in (classnames or []): self._loadClass(classname) self._loader = TestLoader() self.setUp = groupSetUp self.tearDown = groupTearDown def _loadClass(self, classname): moduleName, className = classname.rsplit('.', 1) cls = getattr(__import__(moduleName, globals(), locals(), [className]), className) self._classes[className] = cls def createSuite(self, testnames=None): if not testnames: testnames = sorted(self._classes.keys()) suite = TestSuite() for testname in testnames: testcase = testname.split('.') testclass = self._classes.get(testcase[0], None) if not testclass: continue if len(testcase) == 1: suite.addTest(self._loader.loadTestsFromTestCase(testclass)) else: suite.addTest( self._loader.loadTestsFromName(testcase[1], testclass)) return suite
class TestGroup(object): def __init__(self, name, classnames=None, groupSetUp=lambda:None, groupTearDown=lambda:None): self.name = name self._classes = {} for classname in (classnames or []): self._loadClass(classname) self._loader = TestLoader() self.setUp = groupSetUp self.tearDown = groupTearDown def _loadClass(self, classname): moduleName, className = classname.rsplit('.', 1) cls = getattr(__import__(moduleName, globals(), locals(), [className]), className) self._classes[className] = cls def createSuite(self, testnames=None): if not testnames: testnames = sorted(self._classes.keys()) suite = TestSuite() for testname in testnames: testcase = testname.split('.') testclass = self._classes.get(testcase[0], None) if not testclass: continue if len(testcase) == 1: suite.addTest(self._loader.loadTestsFromTestCase(testclass)) else: suite.addTest(self._loader.loadTestsFromName(testcase[1], testclass)) return suite
def main(): modules = ['test_360', 'test_dependencies', 'test_optional_dependencies', 'test_simple'] simple_suite = TestSuite() loader = TestLoader() result = TestResult() for module in modules: suite = loader.loadTestsFromName(module) simple_suite.addTest(suite) print print 'Running simple test suite...' print simple_suite.run(result) print print 'Ran {0} tests.'.format(result.testsRun) print if len(result.errors) > 0: print '#########################################################' print 'There are {0} errors. See below for tracebacks:'.format(len(result.errors)) print '#########################################################' print for error in result.errors: print error[1] print print '#########################################################' print 'There are {0} errors. See above for tracebacks.'.format(len(result.errors)) print '#########################################################' else: print 'All tests passed.' print
def build_suite(self, test_labels, extra_tests=None, **kwargs): ''' Override the base class method to return a suite consisting of all TestCase subclasses throughought the whole project. ''' if test_labels: suite = TestSuite() else: suite = DjangoTestSuiteRunner.build_suite( self, test_labels, extra_tests, **kwargs ) added_test_classes = set(t.__class__ for t in suite) loader = TestLoader() for fname in _get_module_names(os.getcwd()): module = _import(_to_importable_name(fname)) for test_class in _get_testcases(module): if test_class in added_test_classes: continue for method_name in loader.getTestCaseNames(test_class): testname = '.'.join([ module.__name__, test_class.__name__, method_name ]) if self._test_matches(testname, test_labels): suite.addTest(loader.loadTestsFromName(testname)) added_test_classes.add(test_class) return reorder_suite(suite, (TestCase,))
def test(module, func, coverage, verbosity, failfast): from unittest import TestLoader, TextTestRunner from os.path import abspath, normpath, dirname, join from sys import path if coverage: print('Run unittest with coverage monitor.') else: cov.stop() runner = TextTestRunner(verbosity=2 if verbosity else 1, failfast=failfast) loader = TestLoader() tests = None if module is None: tests = loader.discover('test') elif func is None: tests = loader.loadTestsFromName('test.test_' + module) else: path.append('test') module = __import__('test_' + module) loader = TestLoader() runner = TextTestRunner(verbosity=2) test_class = None for k, v in module.__dict__.items(): if k.endswith('TestCase') and k != 'BaseTestCase': test_class = v break test_func_name = 'test_' + func tests = loader.loadTestsFromName(test_func_name, test_class) runner.run(tests) if coverage: cov.stop() cov.save() print('Coverage Summary:') cov.report() base_dir = abspath(dirname(__file__)) cov_dir = normpath(join(base_dir, 'cov_report')) cov.html_report(directory=cov_dir) cov.erase()
def test(args): from unittest import TestLoader, TextTestRunner loader = TestLoader() if args.module: suite = loader.loadTestsFromName(args.module) else: suite = loader.discover(TESTS_DIR) runner = TextTestRunner(verbosity=2) runner.run(suite)
def unit_test(args): from unittest import TestLoader, TextTestRunner loader = TestLoader() if args.module: suite = loader.loadTestsFromName(args.module) else: suite = loader.discover('code/tests') runner = TextTestRunner() runner.run(suite)
def testName(self): loader = TestLoader() suite = unittest.TestSuite() #load=loader.loadTestsFromModule("TestCasesFile.py") load = loader.loadTestsFromName("TestCasesFile") suite.addTests(load) dir = os.getcwd() dt = datetime.datetime.now().strftime("%Y_%d_%m_%H%M") outputfile = open(dir + "//AutomationTestReport_" + dt + ".html", "w") runner = HTMLTestRunner.HTMLTestRunner(stream=outputfile, title="Test Execution report", description="Automation Test") runner.run(suite) outputfile.close()
def test(tests_path=None): loader = TestLoader() if tests_path: try: tests = loader.loadTestsFromName(tests_path) if not tests._tests: tests = loader.discover(tests_path, top_level_dir=".") except ModuleNotFoundError: tests = loader.discover(".") else: tests = loader.discover(".") test_runner = TextTestRunner(verbosity=2) settings.SENTRY_URL = None settings.BOT_TOKEN = None settings.DEVELOPER_BOT_TOKEN = None settings.DEVELOPER_USER_ID = None db_url, db_name = settings.DATABASE["url"].rsplit("/", 1) settings.DATABASE["url"] = f"{db_url}/test_{db_name}" db_engine = create_engine(settings.DATABASE["url"]) init_sqlalchemy(db_engine) alembic_cfg.set_main_option("sqlalchemy.url", settings.DATABASE["url"]) click.echo("Creating DB...") if database_exists(db_engine.url): drop_database(db_engine.url) create_database(db_engine.url) try: click.echo("Migrating DB...") command_migrate("head") click.echo("Running tests...") result = test_runner.run(tests) except Exception: result = None finally: click.echo("Deleting DB...") drop_database(db_engine.url) if not result or result.failures or result.errors: exit(-1)
def _run_unittest(cls, module_path, module_class_method, queue): sys.path.insert(0, module_path) stream = io.StringIO() try: loader = TestLoader() suite = loader.loadTestsFromName(module_class_method) except ValueError as ex: msg = "loadTestsFromName error finding unittest {}: {}" queue.put({ 'status': 'finished', 'result': 'error', 'output': msg.format(module_class_method, str(ex)) }) return runner = TextTestRunner(stream=stream, verbosity=0) unittest_result = runner.run(suite) unittest_result_entries = None if len(unittest_result.errors) > 0: result = 'error' unittest_result_entries = unittest_result.errors elif len(unittest_result.failures) > 0: result = 'fail' unittest_result_entries = unittest_result.failures elif len(unittest_result.skipped) > 0: result = 'skip' unittest_result_entries = unittest_result.skipped else: result = 'pass' stream.seek(0) output = { 'status': 'finished', 'result': result, 'output': stream.read() } if unittest_result_entries is not None: last_entry = unittest_result_entries[-1] lines = last_entry[1].splitlines() fail_reason = lines[-1] output['fail_reason'] = fail_reason stream.close() queue.put(output)
def _run_unittest(cls, module_path, module_class_method, queue): sys.path.insert(0, module_path) stream = io.StringIO() try: loader = TestLoader() suite = loader.loadTestsFromName(module_class_method) except ValueError as ex: msg = "loadTestsFromName error finding unittest {}: {}" queue.put({ 'status': 'finished', 'result': 'error', 'output': msg.format(module_class_method, str(ex)) }) return runner = TextTestRunner(stream=stream, verbosity=0) unittest_result = runner.run(suite) if len(unittest_result.errors) > 0: result = 'error' elif len(unittest_result.failures) > 0: result = 'fail' elif len(unittest_result.skipped) > 0: result = 'skip' else: result = 'pass' stream.seek(0) output = { 'status': 'finished', 'result': result, 'output': stream.read() } stream.close() queue.put(output)
def test_suite(): testloader = TestLoader() return testloader.loadTestsFromName('tests.aksyosc.tests.test_connector')
def fail(msg, code=1): sys.stderr.write("ERROR - %s\n" % msg) sys.exit(code) if len(sys.argv) < 2: fail("expected <name1> [<name2> ... <nameN>]") tests = [] loader = TestLoader() for i, name in enumerate(sys.argv[1:]): # detects end of arguments if name == "-": del sys.argv[0:i] break print "loading tests from %s" % name __import__(name) tests.extend(loader.loadTestsFromName(name)) if not tests: fail("no tests found") # debugging argv handling print "sys.argv: %s" % sys.argv logging.basicConfig(level=logging.DEBUG) suite = TestSuite(tests) TextTestRunner(verbosity=2).run(suite)
path = os.path.join(dirpath, name) if os.path.isdir(path): modules += discoverTestModules(path) elif name.endswith(".py") and name.startswith("test_"): modules.append(path.replace(".py", "").replace("/", ".")) return modules loader = TestLoader() alltests = TestSuite() for module in discoverTestModules("Pegasus/test"): # If not testing service, skip service test modules if not test_service and module.startswith("Pegasus.test.service"): continue # First, try importing the module to make sure it works __import__(module) # Now load the tests from the module suite = loader.loadTestsFromName(module) alltests.addTest(suite) runner = TextTestRunner(verbosity=2) result = runner.run(alltests) if result.wasSuccessful(): sys.exit(0) else: sys.exit(1)
modules = [] # Find top level modules for all test profiles for p in main['tests']: prefixes = map(lambda x: x[:x.find('.')] if x.find('.') > -1 else x, map(lambda x: x if isinstance(x, basestring) else x['tests'],main['tests'][p])) modules.extend(prefixes) # Eliminate duplicates modules = list(set(modules)) t = TestLoader() all_tests = [] # Get every test of every module for m in modules: __import__(m + '.tests') all_tests.extend(get_tests(t.loadTestsFromName(m + '.tests'))) untested_all = list(all_tests) for p in main['tests']: untested = list(all_tests) # Remove every test of this profile from the untested list for t in main['tests'][p]: if isinstance(t, dict): t = t['tests'] untested = filter(lambda x: x.find(t) != 0,untested) untested_all = filter(lambda x: x.find(t) != 0,untested_all) # Now the untested list contains only tests not regarded in this profile if len(untested) == 0: print '\033[92mProfile',p,'tests everything!\033[0m\n' else: print '\033[93mProfile',p,'misses',len(untested),'of',len(all_tests),'tests:\033[0m\n',untested,'\n'
from unittest import TestSuite, TestLoader import django if hasattr(django, 'setup'): django.setup() TEST_MODULES = [ 'saleor.cart.tests', 'saleor.checkout.tests', 'saleor.communication.tests', 'saleor.core.tests', #'saleor.delivery.tests', 'saleor.order.tests', #'saleor.payment.tests', #'saleor.product.tests', 'saleor.registration.tests', 'saleor.userprofile.tests'] suite = TestSuite() loader = TestLoader() for module in TEST_MODULES: suite.addTests(loader.loadTestsFromName(module))
def test_suite(): testloader = TestLoader() return testloader.loadTestsFromName('tests.aksyfs.tests.test_aksyfuse')
def test_suite(): testloader = TestLoader() return testloader.loadTestsFromName('tests.aksyosc.tests.test_osc')
from unittest import TestSuite, TestLoader TEST_MODULES = [ 'saleor.cart.tests', 'saleor.checkout.tests', 'saleor.communication.tests', 'saleor.core.tests', #'saleor.delivery.tests', 'saleor.order.tests', #'saleor.payment.tests', #'saleor.product.tests', 'saleor.registration.tests', 'saleor.userprofile.tests'] suite = TestSuite() loader = TestLoader() for module in TEST_MODULES: suite.addTests(loader.loadTestsFromName(module))
def test_suite(): testloader = TestLoader() return testloader.loadTestsFromName('tests.aksy.devices.akai.z48.ftests.test_disktools')
^^^ reduces testing time from 25min to 1min """ import sys from unittest import TestLoader from unittest.main import _convert_name from unittest.util import strclass from unittest.suite import TestSuite args = sys.argv[1:] testSource = args[0] # "tests/test_generate_cBioPortal_file_cwl.py" loader = TestLoader() # load all the test suites from the provided input needsPrefix = False # if we need to prepend the source name to the final label ; this is needed for dir path inputs if testSource.endswith(".py"): testSuite = loader.loadTestsFromName(_convert_name(testSource)) else: needsPrefix = True testSuite = loader.discover(testSource) # recursive generator to get all the TestCase entries from all the TestSuites; TestSuites can contain arbitrary levels of nested TestSuites's that contain TestCases def get_all_tests(testSuite): for item in testSuite: if not isinstance(item, TestSuite): yield item else: yield from get_all_tests(item) testsList = [i for i in get_all_tests(testSuite)]
def find_tests_and_apps(self, label): """Construct a test suite of all test methods with the specified name. Returns an instantiated test suite corresponding to the label provided. """ tests = [] from unittest import TestLoader loader = TestLoader() from django.db.models import get_app, get_apps for app_models_module in get_apps(): app_name = app_models_module.__name__.rpartition(".")[0] if app_name == label: from django.test.simple import build_suite tests.append(build_suite(app_models_module)) from django.test.simple import get_tests app_tests_module = get_tests(app_models_module) for sub_module in [m for m in app_models_module, app_tests_module if m is not None]: # print "Checking for %s in %s" % (label, sub_module) for name in dir(sub_module): obj = getattr(sub_module, name) import types if isinstance(obj, (type, types.ClassType)) and issubclass(obj, unittest.TestCase): test_names = loader.getTestCaseNames(obj) # print "Checking for %s in %s.%s" % (label, obj, test_names) if label in test_names: tests.append(loader.loadTestsFromName(label, obj)) try: module = sub_module from django.test import _doctest as doctest from django.test.testcases import DocTestRunner doctests = doctest.DocTestSuite(module, checker=self.doctestOutputChecker, runner=DocTestRunner) # Now iterate over the suite, looking for doctests whose name # matches the pattern that was given for test in doctests: if test._dt_test.name in ( "%s.%s" % (module.__name__, ".".join(parts[1:])), "%s.__test__.%s" % (module.__name__, ".".join(parts[1:])), ): tests.append(test) except TypeError as e: raise Exception("%s appears not to be a module: %s" % (module, e)) except ValueError: # No doctests found. pass # If no tests were found, then we were given a bad test label. if not tests: raise ValueError(("Test label '%s' does not refer to a " + "test method or app") % label) # Construct a suite out of the tests that matched. return unittest.TestSuite(tests)
def test_suite(): testloader = TestLoader() return testloader.loadTestsFromName('tests.aksyosc.tests.test_handler')