def test_multiple_with_failures(self, run_playbook, parser): result = run_playbook('test_multiple_with_failures.yml') lines = list(parser.parse_text(result.stdout.str())) plan = lines[-1] assert result.ret == 2 assert plan.category == 'plan' assert plan.expected_tests == 5
def test_diagnostic(self, run_playbook, parser): result = run_playbook('test_diagnostic.yml') lines = list(parser.parse_text(result.stdout.str())) diagnostics = lines[1:3] assert diagnostics[0].category == 'diagnostic' assert diagnostics[0].text == '# stat: setup (1)' assert diagnostics[1].category == 'diagnostic' assert diagnostics[1].text == '# stat: setup (2)'
def test_skip_with_items(self, run_playbook, parser): result = run_playbook('test_skip_with_items.yml') lines = list(parser.parse_text(result.stdout.str())) test = lines[1] assert result.ret == 0 assert test.category == 'test' assert test.ok is True assert test.directive.reason == 'No items in the list' assert test.skip is True assert test.todo is False
def test_skip(self, run_playbook, parser): result = run_playbook('test_skip.yml') lines = list(parser.parse_text(result.stdout.str())) test = lines[1] assert result.ret == 0 assert test.category == 'test' assert test.ok is True assert test.directive.reason == 'Conditional check failed' assert test.skip is True assert test.todo is False
def test_unexpected_failure(self, run_playbook, parser): result = run_playbook('test_unexpected_failure.yml') lines = list(parser.parse_text(result.stdout.str())) test = lines[1] assert result.ret == 2 assert test.category == 'test' assert test.ok is False assert test.description == '- assert: expect success' assert test.skip is False assert test.todo is False
def run_tests(uri, psql, base_path, init_script, nuke_script, test_script, verbose=False, stream=sys.stderr, pgtap_init_file=DEFAULT_PGTAP_INIT_FILE, init_only=False, nuke_only=False, no_init_nuke=False): skip_tests = init_only or nuke_only if (not skip_tests and not no_init_nuke) or nuke_only: print('-> Running cleanup script') run_script(uri, [os.path.join(base_path, nuke_script)], psql) if (not skip_tests) or init_only: print('-> Running initialization script') run_script(uri, [os.path.join(base_path, init_script)], psql) if skip_tests: return print('-> Running test initialization script') run_script(uri, [os.path.join(base_path, test_script)], psql) scripts_to_test = discover_test_scripts(base_path) tmpdir = os.path.join(tempfile.gettempdir(), 'pgtest') os.makedirs(tmpdir, exist_ok=True) tap_files = [] print('-> Running tests') runner = unittest.TextTestRunner(verbosity=verbose, stream=stream) parser = tap.parser.Parser() for path in scripts_to_test: stdout, stderr = run_script(uri, [pgtap_init_file, path], psql, check=False) is_error = False for l in parser.parse_text(stdout): if l.category == 'test' and not l.ok: is_error = True print(f'{colorama.Fore.RED}{l}{colorama.Style.RESET_ALL}') elif is_error and l.category == 'diagnostic': print(f'{colorama.Fore.RED}{l.text}{colorama.Style.RESET_ALL}') else: is_error = False if stderr: print( '========================= captured stderr ============================' ) print(f'{colorama.Fore.RED}{stderr}{colorama.Style.RESET_ALL}', file=sys.stderr) relpath = os.path.relpath(path, base_path) outpath = os.path.join(tmpdir, relpath) os.makedirs(os.path.dirname(outpath), exist_ok=True) with open(outpath, 'w', encoding='utf-8') as f: f.write(stdout) tap_files.append(outpath) loader = tap.loader.Loader() suite = loader.load(tap_files) shutil.rmtree(tmpdir) result = runner.run(suite) return _get_status(result)
def test_tap_version(self, run_playbook, parser): result = run_playbook('test_tap_version.yml') lines = list(parser.parse_text(result.stdout.str())) version = lines[0] assert version.category == 'version' assert version.version == 13
def tap_to_markdown(tap_results: str): """Convierte de formato TAP a Markdown para Github. Returns: una tupla (Counts, str) donde el segundo elemento es texto Markdown con un resumen de los resultados (a colocar en check_run.output.text). """ # Parse TAP results. plan = None lines = [] parser = tap.parser.Parser() ok_num = 0 fail_num = 0 skip_num = 0 warn_num = 0 for test in parser.parse_text(tap_results): if test.category == "plan": plan = test if test.category != "test": continue if WARN_RE.search(test.description): suffix = " :warning:" warn_num += 1 elif test.skip: suffix = "" skip_num += 1 elif test.ok: suffix = " :heavy_check_mark:" ok_num += 1 else: suffix = " :x:" fail_num += 1 description = WARN_RE.sub("", test.description) lines.append(f"- {description}{suffix}") yaml_lines = [] yaml_block = test.yaml_block or {} for item, data in yaml_block.items(): if data.count("\n") > 1: if item.startswith("_"): yaml_lines.append(f"```\n{data}\n```") else: yaml_lines.append(f"- {item}") yaml_lines.append(indent2(f"```\n{data}\n```\n")) else: yaml_lines.append(f"- {item}: {data}" if item[0] != "_" else f"{data}") lines.append(indentjoin((yaml_lines))) lines.append("") if plan: expected_ok = plan.expected_tests - skip_num else: expected_ok = ok_num + fail_num counts = Counts(ok_num, fail_num, warn_num, skip_num, expected_ok) return (counts, "\n".join(lines))