def test_after_hash_is_not_description(self): parser = Parser() line = parser.parse_line('ok A description # Not part of description.') self.assertEqual('test', line.category) self.assertEqual('A description', line.description)
def test_finds_description(self): parser = Parser() line = parser.parse_line('ok 42 A passing test.') self.assertEqual('test', line.category) self.assertEqual('A passing test.', line.description)
def test_finds_todo(self): parser = Parser() line = parser.parse_line('ok A description # TODO Not done') self.assertEqual('test', line.category) self.assertTrue(line.todo)
def test_parses_yaml_no_end(self): sample = inspect.cleandoc( u"""TAP version 13 1..2 ok 1 A passing test --- test: sample yaml not ok 2 A failing test""" ) parser = Parser() lines = [] for line in parser.parse_text(sample): lines.append(line) try: import yaml from more_itertools import peekable # noqa converted_yaml = yaml.load(u"""test: sample yaml""") self.assertEqual(4, len(lines)) self.assertEqual(13, lines[0].version) self.assertEqual(converted_yaml, lines[2].yaml_block) self.assertEqual("test", lines[3].category) self.assertIsNone(lines[3].yaml_block) except ImportError: self.assertEqual(6, len(lines)) self.assertEqual(13, lines[0].version) for l in list(range(3, 5)): self.assertEqual("unknown", lines[l].category) self.assertEqual("test", lines[5].category)
def test_finds_skip(self): parser = Parser() line = parser.parse_line('ok A description # SKIP for now') self.assertEqual('test', line.category) self.assertTrue(line.skip)
def test_unrecognizable_line(self): """The parser returns an unrecognizable line.""" parser = Parser() line = parser.parse_line('This is not a valid TAP line. # srsly') self.assertEqual('unknown', line.category)
def test_parses_yaml_more_complex(self): sample = inspect.cleandoc(u"""TAP version 13 1..2 ok 1 A passing test --- message: test severity: fail data: got: - foo expect: - bar""") parser = Parser() lines = [] for line in parser.parse_text(sample): lines.append(line) if have_yaml: converted_yaml = yaml.safe_load(u""" message: test severity: fail data: got: - foo expect: - bar""") self.assertEqual(3, len(lines)) self.assertEqual(13, lines[0].version) self.assertEqual(converted_yaml, lines[2].yaml_block) else: self.assertEqual(11, len(lines)) self.assertEqual(13, lines[0].version) for l in list(range(3, 11)): self.assertEqual("unknown", lines[l].category)
def test_finds_description(self): parser = Parser() line = parser.parse_line("ok 42 A passing test.") self.assertEqual("test", line.category) self.assertEqual("A passing test.", line.description)
def test_ignores_plan_with_any_non_skip_directive(self): """The parser only recognizes SKIP directives in plans.""" parser = Parser() line = parser.parse_line('1..42 # TODO will not work.') self.assertEqual('unknown', line.category)
def test_after_hash_is_not_description(self): parser = Parser() line = parser.parse_line("ok A description # Not part of description.") self.assertEqual("test", line.category) self.assertEqual("A description", line.description)
def test_parses_yaml_no_end(self): sample = inspect.cleandoc(u"""TAP version 13 1..2 ok 1 A passing test --- test: sample yaml not ok 2 A failing test""") parser = Parser() lines = [] for line in parser.parse_text(sample): lines.append(line) if have_yaml: converted_yaml = yaml.safe_load(u"""test: sample yaml""") self.assertEqual(4, len(lines)) self.assertEqual(13, lines[0].version) self.assertEqual(converted_yaml, lines[2].yaml_block) self.assertEqual("test", lines[3].category) self.assertIsNone(lines[3].yaml_block) else: self.assertEqual(6, len(lines)) self.assertEqual(13, lines[0].version) for l in list(range(3, 5)): self.assertEqual("unknown", lines[l].category) self.assertEqual("test", lines[5].category)
def test_parses_yaml_no_end(self): sample = inspect.cleandoc(u"""TAP version 13 1..2 ok 1 A passing test --- test: sample yaml not ok 2 A failing test""") parser = Parser() lines = [] for line in parser.parse_text(sample): lines.append(line) try: import yaml from more_itertools import peekable # noqa converted_yaml = yaml.load(u"""test: sample yaml""") self.assertEqual(4, len(lines)) self.assertEqual(13, lines[0].version) self.assertEqual(converted_yaml, lines[2].yaml_block) self.assertEqual('test', lines[3].category) self.assertIsNone(lines[3].yaml_block) except ImportError: self.assertEqual(6, len(lines)) self.assertEqual(13, lines[0].version) for l in list(range(3, 5)): self.assertEqual('unknown', lines[l].category) self.assertEqual('test', lines[5].category)
def test_finds_number(self): """The parser extracts a test number.""" parser = Parser() line = parser.parse_line('ok 42 is the magic number.') self.assertEqual('test', line.category) self.assertEqual(42, line.number)
def test_finds_plan_with_skip(self): """The parser extracts a plan line containing a SKIP.""" parser = Parser() line = parser.parse_line('1..42 # Skipping this test file.') self.assertEqual('plan', line.category) self.assertTrue(line.skip)
def test_finds_plan(self): """The parser extracts a plan line.""" parser = Parser() line = parser.parse_line('1..42') self.assertEqual('plan', line.category) self.assertEqual(42, line.expected_tests)
def test_bail_out_line(self): """The parser extracts a bail out line.""" parser = Parser() line = parser.parse_line('Bail out! This is the reason to bail.') self.assertEqual('bail', line.category) self.assertEqual('This is the reason to bail.', line.reason)
def test_finds_version(self): """The parser extracts a version line.""" parser = Parser() line = parser.parse_line('TAP version 13') self.assertEqual('version', line.category) self.assertEqual(13, line.version)
def test_diagnostic_line(self): """The parser extracts a diagnostic line.""" text = '# An example diagnostic line' parser = Parser() line = parser.parse_line(text) self.assertEqual('diagnostic', line.category) self.assertEqual(text, line.text)
def test_finds_ok(self): """The parser extracts an ok line.""" parser = Parser() line = parser.parse_line("ok - This is a passing test line.") self.assertEqual("test", line.category) self.assertTrue(line.ok) self.assertTrue(line.number is None)
def test_finds_ok(self): """The parser extracts an ok line.""" parser = Parser() line = parser.parse_line('ok - This is a passing test line.') self.assertEqual('test', line.category) self.assertTrue(line.ok) self.assertTrue(line.number is None)
def test_finds_not_ok(self): """The parser extracts a not ok line.""" parser = Parser() line = parser.parse_line('not ok - This is a failing test line.') self.assertEqual('test', line.category) self.assertFalse(line.ok) self.assertTrue(line.number is None)
def test_finds_not_ok(self): """The parser extracts a not ok line.""" parser = Parser() line = parser.parse_line('not ok - This is a failing test line.') self.assertEqual('test', line.category) self.assertFalse(line.ok) self.assertTrue(line.number is None) self.assertEqual('', line.directive.text)
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_finds_not_ok(self): """The parser extracts a not ok line.""" parser = Parser() line = parser.parse_line("not ok - This is a failing test line.") self.assertEqual("test", line.category) self.assertFalse(line.ok) self.assertTrue(line.number is None) self.assertEqual("", line.directive.text)
def test_parses_stdin(self): parser = Parser() lines = [] for line in parser.parse_stdin(): 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)
def test_finds_directive(self): """The parser extracts a directive""" parser = Parser() test_line = "not ok - This line fails # TODO not implemented" line = parser.parse_line(test_line) directive = line.directive self.assertEqual("test", line.category) self.assertEqual("TODO not implemented", directive.text) self.assertFalse(directive.skip) self.assertTrue(directive.todo) self.assertEqual("not implemented", directive.reason)
def test_finds_directive(self): """The parser extracts a directive""" parser = Parser() test_line = 'not ok - This line fails # TODO not implemented' line = parser.parse_line(test_line) directive = line.directive self.assertEqual('test', line.category) self.assertEqual('TODO not implemented', directive.text) self.assertFalse(directive.skip) self.assertTrue(directive.todo) self.assertEqual('not implemented', directive.reason)
def test_parses_stdin(self): parser = Parser() lines = [] for line in parser.parse_stdin(): 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)
def test_parses_text(self): sample = inspect.cleandoc(u"""1..2 ok 1 A passing test not ok 2 A failing test""") parser = Parser() lines = [] for line in parser.parse_text(sample): 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)
def parse(self, tap_file, progress_cb=None): self.backend.configure() for line in Parser().parse_file(tap_file): if not isinstance(line, Result): continue test_case = self.backend.test_case_get_or_create(line.description) test_case_id = test_case['case_id'] self.backend.add_test_case_to_plan(test_case_id, self.backend.plan_id) test_case_run_id = self.backend.add_test_case_to_run( test_case_id, self.backend.run_id) comment = 'Result recorded via Kiwi TCMS tap-plugin' if line.ok: status_id = self.backend.get_status_id('PASSED') else: status_id = self.backend.get_status_id('FAILED') if line.skip: status_id = self.backend.get_status_id('WAIVED') comment = line.directive.text if line.todo: status_id = self.backend.get_status_id('PAUSED') comment = line.directive.text self.backend.update_test_case_run(test_case_run_id, status_id, comment) if progress_cb: progress_cb()
def test_parses_text(self): sample = inspect.cleandoc( u"""1..2 ok 1 A passing test not ok 2 A failing test""") parser = Parser() lines = [] for line in parser.parse_text(sample): 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)
def test_parses_yaml_more_complex(self): sample = inspect.cleandoc( u"""TAP version 13 1..2 ok 1 A passing test --- message: test severity: fail data: got: - foo expect: - bar output: |- a multiline string must be handled properly even with | pipes | here > and: there""" ) parser = Parser() lines = [] for line in parser.parse_text(sample): lines.append(line) if have_yaml: converted_yaml = yaml.safe_load( u''' message: test severity: fail data: got: - foo expect: - bar output: "a multiline string\\nmust be handled properly\\neven with | pipes\\n| here > and: there"''' # noqa ) self.assertEqual(3, len(lines)) self.assertEqual(13, lines[0].version) self.assertEqual(converted_yaml, lines[2].yaml_block) else: self.assertEqual(16, len(lines)) self.assertEqual(13, lines[0].version) for line_index in list(range(3, 11)): self.assertEqual("unknown", lines[line_index].category)
def test_malformed_yaml(self): self.maxDiff = None sample = inspect.cleandoc( u"""TAP version 13 1..2 ok 1 A passing test --- test: sample yaml \tfail: tabs are not allowed! ... not ok 2 A failing test""" ) yaml_err = inspect.cleandoc( u""" WARNING: Optional imports not found, TAP 13 output will be ignored. To parse yaml, see requirements in docs: https://tappy.readthedocs.io/en/latest/consumers.html#tap-version-13""" ) parser = Parser() lines = [] with captured_output() as (parse_out, _): for line in parser.parse_text(sample): lines.append(line) try: import yaml # noqa from more_itertools import peekable # noqa self.assertEqual(4, len(lines)) self.assertEqual(13, lines[0].version) with captured_output() as (out, _): self.assertIsNone(lines[2].yaml_block) self.assertEqual( "Error parsing yaml block. Check formatting.", out.getvalue().strip() ) self.assertEqual("test", lines[3].category) self.assertIsNone(lines[3].yaml_block) except ImportError: self.assertEqual(8, len(lines)) self.assertEqual(13, lines[0].version) for l in list(range(3, 7)): self.assertEqual("unknown", lines[l].category) self.assertEqual("test", lines[7].category) self.assertEqual(yaml_err, parse_out.getvalue().strip())
def test_parses_yaml_no_start(self): sample = inspect.cleandoc(u"""TAP version 13 1..2 ok 1 A passing test test: sample yaml ... not ok 2 A failing test""") parser = Parser() lines = [] for line in parser.parse_text(sample): lines.append(line) self.assertEqual(6, len(lines)) self.assertEqual(13, lines[0].version) self.assertIsNone(lines[2].yaml_block) for l in list(range(3, 5)): self.assertEqual('unknown', lines[l].category) self.assertEqual('test', lines[5].category)
def test_parses_mixed(self): # Test that we can parse both a version 13 and earlier version files # using the same parser. Make sure that parsing works regardless of # the order of the incoming documents. sample_version_13 = inspect.cleandoc( u"""TAP version 13 1..2 ok 1 A passing version 13 test --- test: sample yaml ... not ok 2 A failing version 13 test""" ) sample_pre_13 = inspect.cleandoc( """1..2 ok 1 A passing pre-13 test not ok 2 A failing pre-13 test""" ) parser = Parser() lines = [] lines.extend(parser.parse_text(sample_version_13)) lines.extend(parser.parse_text(sample_pre_13)) if have_yaml: self.assertEqual(13, lines[0].version) self.assertEqual("A passing version 13 test", lines[2].description) self.assertEqual("A failing version 13 test", lines[3].description) self.assertEqual("A passing pre-13 test", lines[5].description) self.assertEqual("A failing pre-13 test", lines[6].description) else: self.assertEqual(13, lines[0].version) self.assertEqual("A passing version 13 test", lines[2].description) self.assertEqual("A failing version 13 test", lines[6].description) self.assertEqual("A passing pre-13 test", lines[8].description) self.assertEqual("A failing pre-13 test", lines[9].description) # Test parsing documents in reverse order parser = Parser() lines = [] lines.extend(parser.parse_text(sample_pre_13)) lines.extend(parser.parse_text(sample_version_13)) if have_yaml: self.assertEqual("A passing pre-13 test", lines[1].description) self.assertEqual("A failing pre-13 test", lines[2].description) self.assertEqual(13, lines[3].version) self.assertEqual("A passing version 13 test", lines[5].description) self.assertEqual("A failing version 13 test", lines[6].description) else: self.assertEqual("A passing pre-13 test", lines[1].description) self.assertEqual("A failing pre-13 test", lines[2].description) self.assertEqual(13, lines[3].version) self.assertEqual("A passing version 13 test", lines[5].description) self.assertEqual("A failing version 13 test", lines[9].description)
def test_parses_yaml_more_complex(self): sample = inspect.cleandoc( u"""TAP version 13 1..2 ok 1 A passing test --- message: test severity: fail data: got: - foo expect: - bar""" ) parser = Parser() lines = [] for line in parser.parse_text(sample): lines.append(line) try: import yaml from more_itertools import peekable # noqa converted_yaml = yaml.load( u""" message: test severity: fail data: got: - foo expect: - bar""" ) self.assertEqual(3, len(lines)) self.assertEqual(13, lines[0].version) self.assertEqual(converted_yaml, lines[2].yaml_block) except ImportError: self.assertEqual(11, len(lines)) self.assertEqual(13, lines[0].version) for l in list(range(3, 11)): self.assertEqual("unknown", lines[l].category)
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)
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_stdin(self, mock_sys): mock_sys.stdin.__iter__.return_value = iter([ '1..2\n', 'ok 1 A passing test\n', 'not ok 2 A failing test\n', ]) mock_sys.stdin.__enter__.return_value = None mock_sys.stdin.__exit__.return_value = None parser = Parser() lines = [] for line in parser.parse_stdin(): 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)
def test_parses_yaml_no_start(self): sample = inspect.cleandoc( u"""TAP version 13 1..2 ok 1 A passing test test: sample yaml ... not ok 2 A failing test""" ) parser = Parser() lines = [] for line in parser.parse_text(sample): lines.append(line) self.assertEqual(6, len(lines)) self.assertEqual(13, lines[0].version) self.assertIsNone(lines[2].yaml_block) for l in list(range(3, 5)): self.assertEqual("unknown", lines[l].category) self.assertEqual("test", lines[5].category)
def test_parses_yaml_no_association(self): sample = inspect.cleandoc( u"""TAP version 13 1..2 ok 1 A passing test # Diagnostic line --- test: sample yaml ... not ok 2 A failing test""" ) parser = Parser() lines = [] for line in parser.parse_text(sample): lines.append(line) self.assertEqual(8, len(lines)) self.assertEqual(13, lines[0].version) self.assertIsNone(lines[2].yaml_block) self.assertEqual("diagnostic", lines[3].category) for l in list(range(4, 7)): self.assertEqual("unknown", lines[l].category) self.assertEqual("test", lines[7].category)
def __init__(self): self._parser = Parser()
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))