def PageTestSuite(storydir, package=None, setUp=setUpGlobs): """Create a suite of page tests for files found in storydir. :param storydir: the directory containing the page tests. :param package: the package to resolve storydir relative to. Defaults to the caller's package. Each file is added as a separate DocFileTest. """ # we need to normalise the package name here, because it # involves checking the parent stack frame. Otherwise the # files would be looked up relative to this module. package = doctest._normalize_module(package) abs_storydir = doctest._module_relative_path(package, storydir) filenames = set(filename for filename in os.listdir(abs_storydir) if filename.lower().endswith('.txt')) suite = unittest.TestSuite() # Add tests to the suite individually. if filenames: checker = doctest.OutputChecker() paths = [os.path.join(storydir, filename) for filename in filenames] suite.addTest( LayeredDocFileSuite(paths=paths, package=package, checker=checker, stdout_logging=False, layer=PageTestLayer, setUp=setUp)) return suite
def PageTestSuite(storydir, package=None, setUp=setUpGlobs): """Create a suite of page tests for files found in storydir. :param storydir: the directory containing the page tests. :param package: the package to resolve storydir relative to. Defaults to the caller's package. Each file is added as a separate DocFileTest. """ # we need to normalise the package name here, because it # involves checking the parent stack frame. Otherwise the # files would be looked up relative to this module. package = doctest._normalize_module(package) abs_storydir = doctest._module_relative_path(package, storydir) filenames = set(filename for filename in os.listdir(abs_storydir) if filename.lower().endswith(".txt")) suite = unittest.TestSuite() # Add tests to the suite individually. if filenames: checker = doctest.OutputChecker() paths = [os.path.join(storydir, filename) for filename in filenames] suite.addTest( LayeredDocFileSuite( paths=paths, package=package, checker=checker, stdout_logging=False, layer=PageTestLayer, setUp=setUp ) ) return suite
def _patched_DocFileTest(path, module_relative=True, package=None, globs=None, parser=DocTestParser(), **options): if globs is None: globs = {} if package and not module_relative: raise ValueError("Package may only be specified for module-" "relative paths.") # Relativize the path. if module_relative: package = _normalize_module(package) path = _module_relative_path(package, path) # Find the file and read it. name = os.path.basename(path) doc = open(path, 'U').read() # Convert it to a test, and wrap it in a DocFileCase. test = parser.get_doctest(doc, globs, name, path, 0) return DocFileCase(test, **options)
def _patched_testfile( filename, module_relative=True, name=None, package=None, globs=None, verbose=None, report=True, optionflags=0, extraglobs=None, raise_on_error=False, parser=DocTestParser(), ): global master if package and not module_relative: raise ValueError("Package may only be specified for module-" "relative paths.") # Relativize the path if module_relative: package = _normalize_module(package) filename = _module_relative_path(package, filename) # If no name was given, then use the file's name. if name is None: name = os.path.basename(filename) # Assemble the globals. if globs is None: globs = {} else: globs = globs.copy() if extraglobs is not None: globs.update(extraglobs) if raise_on_error: runner = DebugRunner(verbose=verbose, optionflags=optionflags) else: runner = DocTestRunner(verbose=verbose, optionflags=optionflags) # Read the file, convert it to a test, and run it. s = open(filename, "U").read() test = parser.get_doctest(s, globs, name, filename, 0) runner.run(test) if report: runner.summarize() if master is None: master = runner else: master.merge(runner) return runner.failures, runner.tries
def getDB(filename, package=None): """Return a DB by it's path.""" if package is not None: filename = doctest._module_relative_path(package, filename) package = package.__file__ else: package = __file__ filename = os.path.join(os.path.dirname(package), filename) fileStorage = FileStorage(filename) storage = DemoStorage("Demo Storage", fileStorage) return DB(storage)
def _patched_testfile(filename, module_relative=True, name=None, package=None, globs=None, verbose=None, report=True, optionflags=0, extraglobs=None, raise_on_error=False, parser=DocTestParser()): global master if package and not module_relative: raise ValueError("Package may only be specified for module-" "relative paths.") # Relativize the path if module_relative: package = _normalize_module(package) filename = _module_relative_path(package, filename) # If no name was given, then use the file's name. if name is None: name = os.path.basename(filename) # Assemble the globals. if globs is None: globs = {} else: globs = globs.copy() if extraglobs is not None: globs.update(extraglobs) if raise_on_error: runner = DebugRunner(verbose=verbose, optionflags=optionflags) else: runner = DocTestRunner(verbose=verbose, optionflags=optionflags) # Read the file, convert it to a test, and run it. s = open(filename, 'U').read() test = parser.get_doctest(s, globs, name, filename, 0) runner.run(test) if report: runner.summarize() if master is None: master = runner else: master.merge(runner) return runner.failures, runner.tries
def _load_testfile(filename): # Copied from Python 3.6 doctest._load_testfile to ensure utf-8 # encoding on Python 2. package = doctest._normalize_module(None, 3) filename = doctest._module_relative_path(package, filename) if getattr(package, '__loader__', None) is not None: if hasattr(package.__loader__, 'get_data'): file_contents = package.__loader__.get_data(filename) file_contents = file_contents.decode("utf-8") # get_data() opens files as 'rb', so one must do the equivalent # conversion as universal newlines would do. return file_contents.replace(os.linesep, '\n'), filename with codecs.open(filename, encoding="utf-8") as f: return f.read(), filename
def __init__(self, filename, module_relative=True, package=None, source=None, name=None, tags=None, variables=[], outputdir=None, setUp=None, tearDown=None, **kw): unittest.TestCase.__init__(self) filename = doctest._module_relative_path(package, filename) suite = robot.parsing.TestData(source=filename) suite_parent = os.path.dirname(filename) def recurse(child_suite, test_case, suite_parent): if source and child_suite.source != source: child_suite.testcase_table.tests = [] elif name: tests = child_suite.testcase_table.tests child_suite.testcase_table.tests =\ filter(lambda x: x.name == name, tests) test_case._relpath =\ os.path.relpath(child_suite.source, suite_parent) for grandchild in getattr(child_suite, 'children', []): recurse(grandchild, test_case, suite_parent) recurse(suite, self, suite_parent) # Set suite to be run bu runTest self._robot_suite = suite # Set outputdir for log, report and screenshots self._robot_outputdir = outputdir # Set test method name from the test name self._testMethodName = normalize(name or 'runTest') # Set tags to be included in test's __str__ self._tags = tags # Set variables to pass for pybot self._variables = variables setattr(self, self._testMethodName, self.runTest) # Set test fixture setup and teardown methods when given if setUp: setattr(self, 'setUp', setUp) if tearDown: setattr(self, 'tearDown', tearDown)
def _patched_load_testfile(filename, package, module_relative, encoding=None): if module_relative: package = doctest._normalize_module(package, 3) filename = doctest._module_relative_path(package, filename) if hasattr(package, "__loader__"): if hasattr(package.__loader__, "get_data"): file_contents = package.__loader__.get_data(filename) if encoding is not None: # Python 3 file_contents = file_contents.decode(encoding) # get_data() opens files as 'rb', so one must do the equivalent # conversion as universal newlines would do. return file_contents.replace(os.linesep, "\n"), filename if encoding: # Python 3: return open(filename, encoding=encoding).read(), filename else: return open(filename, "U").read(), filename
def RobotTestSuite(*paths, **kw): """Build up a test suite similarly to doctest.DocFileSuite """ suite = unittest.TestSuite() if 'ROBOTSUITE_LEVEL' in os.environ: try: suite.level = int(os.environ.get('ROBOTSUITE_LEVEL', 1)) except ValueError: pass if kw.get('module_relative', True): kw['package'] = doctest._normalize_module(kw.get('package')) variables = get_robot_variables() for path in paths: filename = doctest._module_relative_path(kw['package'], path) robot_suite = robot_parsing.TestData(source=filename) # Split the robot suite into separate test cases outputdir = [] def recurs(child_suite): suite_base = os.path.basename(child_suite.source) suite_dir = os.path.splitext(suite_base)[0] outputdir.append(suite_dir) for test in child_suite.testcase_table.tests: test_dir = normalize(test.name) outputdir.append(test_dir) suite.addTest( RobotTestCase(path, name=test.name, tags=test.tags.value, variables=variables, source=child_suite.source, outputdir='/'.join(outputdir), **kw)) outputdir.pop() for grandchild in getattr(child_suite, 'children', []): recurs(grandchild) outputdir.pop() recurs(robot_suite) return suite
def _patched_DocFileTest(path, module_relative=True, package=None, globs=None, parser=DocTestParser(), **options): if globs is None: globs = {} if package and not module_relative: raise ValueError("Package may only be specified for module-" "relative paths.") # Relativize the path. if module_relative: package = _normalize_module(package) path = _module_relative_path(package, path) # Find the file and read it. name = os.path.basename(path) doc = open(path, "U").read() # Convert it to a test, and wrap it in a DocFileCase. test = parser.get_doctest(doc, globs, name, path, 0) return DocFileCase(test, **options)
def RobotTestSuite(*paths, **kw): """Build up a test suite similarly to doctest.DocFileSuite """ suite = unittest.TestSuite() if 'ROBOTSUITE_LEVEL' in os.environ: try: suite.level = int(os.environ.get('ROBOTSUITE_LEVEL', 1)) except ValueError: pass if kw.get('module_relative', True): kw['package'] = doctest._normalize_module(kw.get('package')) variables = get_robot_variables() for path in paths: filename = doctest._module_relative_path(kw['package'], path) robot_suite = robot_parsing.TestData(source=filename) # Split the robot suite into separate test cases outputdir = [] def recurs(child_suite): suite_base = os.path.basename(child_suite.source) suite_dir = os.path.splitext(suite_base)[0] outputdir.append(suite_dir) for test in child_suite.testcase_table.tests: test_dir = normalize(test.name) outputdir.append(test_dir) suite.addTest(RobotTestCase(path, name=test.name, tags=test.tags.value, variables=variables, source=child_suite.source, outputdir='/'.join(outputdir), **kw)) outputdir.pop() for grandchild in getattr(child_suite, 'children', []): recurs(grandchild) outputdir.pop() recurs(robot_suite) return suite
def _patched_load_testfile(filename, package, module_relative, encoding=None): if module_relative: package = doctest._normalize_module(package, 3) filename = doctest._module_relative_path(package, filename) if hasattr(package, '__loader__'): if hasattr(package.__loader__, 'get_data'): file_contents = package.__loader__.get_data(filename) if encoding is not None: # Python 3 file_contents = file_contents.decode(encoding) # get_data() opens files as 'rb', so one must do the equivalent # conversion as universal newlines would do. return file_contents.replace(os.linesep, '\n'), filename if encoding: # Python 3: return open(filename, encoding=encoding).read(), filename else: return open(filename, 'U').read(), filename
def update_event(self, inp=-1): self.set_output_val(0, doctest._module_relative_path(self.input(0), self.input(1)))
def __init__(self, filename, module_relative=True, package=None, source=None, name=None, tags=None, variables=None, outputdir=None, setUp=None, tearDown=None, critical=None, noncritical=None, **kw): unittest.TestCase.__init__(self) filename = doctest._module_relative_path(package, filename) suite = robot_parsing.TestData(source=filename) suite_parent = os.path.dirname(filename) self._relative_path = None def walk(child_suite, test_case, suite_parent): found = False if source and child_suite.source != source: child_suite.testcase_table.tests = [] elif name: tests = child_suite.testcase_table.tests child_suite.testcase_table.tests = \ list(filter(lambda x: x.name == name, tests)) test_case._relative_path = \ os.path.relpath(child_suite.source, suite_parent) if len(list(child_suite.testcase_table.tests)): found = True for grandchild in getattr(child_suite, 'children', [])[:]: if not walk(grandchild, test_case, suite_parent): child_suite.children.remove(grandchild) else: found = True return found walk(suite, self, suite_parent) # Mimic DocTestCase to support plone.testing's way of settings test # layer as doctest global: class LayerPlaceHolder(object): globs = None setattr(self, '_dt_test', LayerPlaceHolder()) setattr(self._dt_test, 'globs', {}) # Set suite to be run by runTest self._robot_suite = suite # Set outputdir for log, report and screenshots self._robot_outputdir = outputdir # Set test method name from the test name self._testMethodName = normalize(name or 'runTest', replace_spaces=False) # Set tags to be included in tests' __str__ self._tags = tags # Set variables to pass for pybot self._variables = variables or [] setattr(self, self._testMethodName, self.runTest) # Set tags that should be considered (non)critical self._critical = critical or [] self._noncritical = noncritical or [] # Set test fixture setup and teardown methods when given if setUp: setattr(self, 'setUp', types.MethodType(setUp, self)) if tearDown: setattr(self, 'tearDown', types.MethodType(tearDown, self)) # Set module name from the package to please some report formatter self.__module__ = package.__name__
def _resolve_relative_test_filename(filename): if os.path.isabs(filename): return filename package = doctest._normalize_module(None, 3) return doctest._module_relative_path(package, filename)