def test_parse_empty_file(self): temp = tempfile.NamedTemporaryFile(delete=False) temp.close() parser = Parser() lines = [] for line in parser.parse_file(temp.name): lines.append(line) self.assertEqual(0, len(lines))
def test_parses_file(self): sample = inspect.cleandoc("""1..2 ok 1 A passing test not ok 2 A failing test""") temp = tempfile.NamedTemporaryFile(delete=False) temp.write(sample.encode('utf-8')) temp.close() parser = Parser() lines = [] for line in parser.parse_file(temp.name): lines.append(line) self.assertEqual(3, len(lines)) self.assertEqual('plan', lines[0].category) self.assertEqual('test', lines[1].category) self.assertTrue(lines[1].ok) self.assertIsNone(lines[1].yaml_block) self.assertEqual('test', lines[2].category) self.assertFalse(lines[2].ok)
def test_parses_file(self): sample = inspect.cleandoc( """1..2 ok 1 A passing test not ok 2 A failing test""") temp = tempfile.NamedTemporaryFile(delete=False) temp.write(sample.encode('utf-8')) temp.close() parser = Parser() lines = [] for line in parser.parse_file(temp.name): lines.append(line) self.assertEqual(3, len(lines)) self.assertEqual('plan', lines[0].category) self.assertEqual('test', lines[1].category) self.assertTrue(lines[1].ok) self.assertEqual('test', lines[2].category) self.assertFalse(lines[2].ok)
import sys from tap.parser import Parser from tap.line import Result, Unknown, Diagnostic out = ["### TAP Output for " + sys.argv[2]] p = Parser() in_error = False for line in p.parse_file(sys.argv[1]): if isinstance(line, Result): if in_error: out.append("") out.append("</pre></code></details>") out.append("") out.append("----") out.append("") in_error = False if not line.ok and not line.todo: in_error = True out.append("FAILURE Test #%d: ``%s``" % (line.number, line.description)) out.append("") out.append("<details><summary>Show log</summary><code><pre>") elif isinstance(line, Diagnostic) and in_error: out.append(line.text)
class Loader(object): """Load TAP lines into unittest-able objects.""" ignored_lines = set(['diagnostic', 'unknown']) def __init__(self): self._parser = Parser() def load(self, files): """Load any files found into a suite. Any directories are walked and their files are added as TAP files. :returns: A ``unittest.TestSuite`` instance """ suite = unittest.TestSuite() for filepath in files: if os.path.isdir(filepath): self._find_tests_in_directory(filepath, suite) else: suite.addTest(self.load_suite_from_file(filepath)) return suite def load_suite_from_file(self, filename): """Load a test suite with test lines from the provided TAP file. :returns: A ``unittest.TestSuite`` instance """ suite = unittest.TestSuite() rules = Rules(filename, suite) if not os.path.exists(filename): rules.handle_file_does_not_exist() return suite line_generator = self._parser.parse_file(filename) return self._load_lines(filename, line_generator, suite, rules) def load_suite_from_stdin(self): """Load a test suite with test lines from the TAP stream on STDIN. :returns: A ``unittest.TestSuite`` instance """ suite = unittest.TestSuite() rules = Rules('stream', suite) line_generator = self._parser.parse_stdin() return self._load_lines('stream', line_generator, suite, rules) def _find_tests_in_directory(self, directory, suite): """Find test files in the directory and add them to the suite.""" for dirpath, dirnames, filenames in os.walk(directory): for filename in filenames: filepath = os.path.join(dirpath, filename) suite.addTest(self.load_suite_from_file(filepath)) def _load_lines(self, filename, line_generator, suite, rules): """Load a suite with lines produced by the line generator.""" line_counter = 0 for line in line_generator: line_counter += 1 if line.category in self.ignored_lines: continue if line.category == 'test': suite.addTest(Adapter(filename, line)) rules.saw_test() elif line.category == 'plan': if line.skip: rules.handle_skipping_plan(line) return suite rules.saw_plan(line, line_counter) elif line.category == 'bail': rules.handle_bail(line) return suite elif line.category == 'version': rules.saw_version_at(line_counter) rules.check(line_counter) return suite
class Loader(object): """Load TAP lines into unittest-able objects.""" ignored_lines = set(['diagnostic', 'unknown']) def __init__(self): self._parser = Parser() def load(self, files): """Load any files found into a suite. Any directories are walked and their files are added as TAP files. :returns: A ``unittest.TestSuite`` instance """ suite = unittest.TestSuite() for filepath in files: if os.path.isdir(filepath): self._find_tests_in_directory(filepath, suite) else: suite.addTest(self.load_suite_from_file(filepath)) return suite def load_suite_from_file(self, filename): """Load a test suite with test lines from the provided TAP file. :returns: A ``unittest.TestSuite`` instance """ suite = unittest.TestSuite() rules = Rules(filename, suite) if not os.path.exists(filename): rules.handle_file_does_not_exist() return suite line_counter = 0 for line in self._parser.parse_file(filename): line_counter += 1 if line.category in self.ignored_lines: continue if line.category == 'test': suite.addTest(Adapter(filename, line)) rules.saw_test() elif line.category == 'plan': if line.skip: rules.handle_skipping_plan(line) return suite rules.saw_plan(line, line_counter) elif line.category == 'bail': rules.handle_bail(line) return suite elif line.category == 'version': rules.saw_version_at(line_counter) rules.check(line_counter) return suite def _find_tests_in_directory(self, directory, suite): """Find test files in the directory and add them to the suite.""" for dirpath, dirnames, filenames in os.walk(directory): for filename in filenames: filepath = os.path.join(dirpath, filename) suite.addTest(self.load_suite_from_file(filepath))