def test_lazy_nonzero(self): """__nonzero__ works correctly for lazy suites""" TC = self.TC tests = [TC('test_one'), TC('test_two')] def gen_tests(): for test in tests: yield test lazy = LazySuite(gen_tests) assert lazy assert lazy assert lazy count = 0 for test in lazy: print test assert test count += 1 self.assertEqual(count, 2, "Expected 2 tests, got %s" % count) assert lazy def gen_tests_empty(): for test in []: yield test return empty = LazySuite(gen_tests_empty) assert not empty for test in empty: assert False, "Loaded a test from empty suite: %s" % test
def test_lazy_and_nonlazy(self): TC = self.TC tests = [TC('test_one'), TC('test_two')] def gen_tests(): for test in tests: yield test nonlazy = LazySuite(tests) lazy = LazySuite(gen_tests) assert lazy assert nonlazy lazytests = [] nonlazytests = [] for t in lazy: print "lazy %s" % t lazytests.append(t) for t in nonlazy: print "nonlazy %s" % t nonlazytests.append(t) slazy = map(str, lazytests) snonlazy = map(str, nonlazytests) assert slazy == snonlazy, \ "Lazy and Nonlazy produced different test lists (%s vs %s)" \ % (slazy, snonlazy)
def run_my_tests(): all_tests = () for path in paths: all_tests = itertools.chain(all_tests, TestLoader().loadTestsFromDir(path)) suite = LazySuite(all_tests) nose.run(suite=suite, argv=["--with-coverage", "--cover-html", "--cover-package=pyredux"])
def run_my_tests(): all_tests = () #for path in paths: all_tests = itertools.chain(all_tests, TestLoader().loadTestsFromDir(paths)) suite = LazySuite(all_tests) run(suite=suite)
def run_my_tests(): all_tests = () all_tests = itertools.chain(all_tests, TestLoader().loadTestsFromDir('.')) suite = LazySuite(all_tests) run(suite=suite, argv=[ 'run_all.py', '--cover-branches', '--with-coverage', '--cover-html' ])
def test_test_generator(self): TC = self.TC tests = [TC('test_one'), TC('test_two')] def gen_tests(): for test in tests: yield test suite = LazySuite(gen_tests) self.assertEqual(list([test for test in suite]), tests)
def run(self): """ Run bcfg2-test """ core = self.get_core() clients = Bcfg2.Options.setup.clients or core.metadata.clients ignore = self.get_ignore() if Bcfg2.Options.setup.children: if Bcfg2.Options.setup.children > len(clients): self.logger.info("Refusing to spawn more children than " "clients to test, setting children=%s" % len(clients)) Bcfg2.Options.setup.children = len(clients) perchild = int( ceil(len(clients) / float(Bcfg2.Options.setup.children + 1))) queue = Queue() for child in range(Bcfg2.Options.setup.children): start = child * perchild end = (child + 1) * perchild child = Process(target=self.run_child, args=(clients[start:end], queue)) child.start() def generate_tests(): """ Read test results for the clients """ start = Bcfg2.Options.setup.children * perchild for client in clients[start:]: yield ClientTest(core, client, ignore) for i in range(start): # pylint: disable=W0612 yield ClientTestFromQueue(*queue.get()) else: def generate_tests(): """ Run tests for the clients """ for client in clients: yield ClientTest(core, client, ignore) result = TestProgram(argv=sys.argv[:1] + Bcfg2.Options.setup.nose_options, suite=LazySuite(generate_tests), exit=False) # block until all children have completed -- should be # immediate since we've already gotten all the results we # expect for child in active_children(): child.join() core.shutdown() if result.success: os._exit(0) # pylint: disable=W0212 else: os._exit(1) # pylint: disable=W0212
def loadTestsFromName(self, name, module=None, discovered=False): """Load tests from the entity with the given name. The name may indicate a file, directory, module, or any object within a module. See `nose.util.split_test_name` for details on test name parsing. """ # FIXME refactor this method into little bites? log.debug("load from %s (%s)", name, module) suite = self.suiteClass # give plugins first crack plug_tests = self.config.plugins.loadTestsFromName(name, module) if plug_tests: return suite(plug_tests) addr = TestAddress(name, workingDir=self.workingDir) if module: # Two cases: # name is class.foo # The addr will be incorrect, since it thinks class.foo is # a dotted module name. It's actually a dotted attribute # name. In this case we want to use the full submitted # name as the name to load from the module. # name is module:class.foo # The addr will be correct. The part we want is the part after # the :, which is in addr.call. if addr.call: name = addr.call parent, obj = self.resolve(name, module) if (isclass(parent) and getattr(parent, '__module__', None) != module.__name__ and not isinstance(obj, Failure)): parent = transplant_class(parent, module.__name__) obj = getattr(parent, obj.__name__) log.debug("parent %s obj %s module %s", parent, obj, module) if isinstance(obj, Failure): return suite([obj]) else: return suite( ContextList([self.makeTest(obj, parent)], context=parent)) else: if addr.module: try: if addr.filename is None: module = resolve_name(addr.module) else: self.config.plugins.beforeImport( addr.filename, addr.module) # FIXME: to support module.name names, # do what resolve-name does and keep trying to # import, popping tail of module into addr.call, # until we either get an import or run out of # module parts try: module = self.importer.importFromPath( addr.filename, addr.module) finally: self.config.plugins.afterImport( addr.filename, addr.module) except (KeyboardInterrupt, SystemExit): raise except: exc = sys.exc_info() return suite([ Failure(exc[0], exc[1], exc[2], address=addr.totuple()) ]) if addr.call: return self.loadTestsFromName(addr.call, module) else: return self.loadTestsFromModule(module, addr.filename, discovered=discovered) elif addr.filename: path = addr.filename if addr.call: package = getpackage(path) if package is None: return suite([ Failure(ValueError, "Can't find callable %s in file %s: " "file is not a python module" % (addr.call, path), address=addr.totuple()) ]) return self.loadTestsFromName(addr.call, module=package) else: if op_isdir(path): # In this case we *can* be lazy since we know # that each module in the dir will be fully # loaded before its tests are executed; we # also know that we're not going to be asked # to load from . and ./some_module.py *as part # of this named test load* return LazySuite(lambda: self.loadTestsFromDir(path)) elif op_isfile(path): return self.loadTestsFromFile(path) else: return suite([ Failure(OSError, "No such file %s" % path, address=addr.totuple()) ]) else: # just a function? what to do? I think it can only be # handled when module is not None return suite([ Failure(ValueError, "Unresolvable test name %s" % name, address=addr.totuple()) ])
def test_basic_iteration(self): ls = LazySuite(gen) for t in iter_compat(ls): assert isinstance(t, unittest.TestCase)
def _run(): suite = LazySuite(tests) run(suite=suite)
def create_test_suite(): this_dir = os.path.dirname(os.path.abspath(__file__)) return LazySuite(TestLoader().loadTestsFromDir(this_dir))