1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
from pathlib import Path
import pytest
from junitparser import cli
from junitparser import version
DATA_DIR = Path(__file__).parent / "data"
@pytest.mark.parametrize(
"file, expected_exitcode",
[("jenkins.xml", 1), ("no_fails.xml", 0), ("normal.xml", 1)],
)
def test_verify(file: str, expected_exitcode: int):
path = DATA_DIR / file
assert cli.verify([path]) == expected_exitcode
def test_merge(tmp_path: Path):
files = [DATA_DIR / "jenkins.xml", DATA_DIR / "pytest_success.xml"]
suites = ["JUnitXmlReporter", "JUnitXmlReporter.constructor", "pytest"]
outfile = tmp_path / "merged.xml"
cli.merge(files, str(outfile))
xml = outfile.read_text()
for s in suites:
assert f'name="{s}"' in xml
def test_merge_output_to_terminal(capsys: pytest.CaptureFixture):
ret = cli.main(["merge", str(DATA_DIR / "normal.xml"), "-"])
assert ret == 0
captured = capsys.readouterr()
assert captured.out.startswith("<?xml version='1.0'")
def test_verify_with_glob():
ret = cli.main(["verify", "--glob", str(DATA_DIR / "pytest_*.xml")])
# we expect failure, as one of the files has errors
assert ret == 1
class Test_CommandlineOptions:
@classmethod
def setup_class(cls):
cls.parser = cli._parser("junitparser")
@pytest.mark.parametrize("arg", ["-v", "--version"])
def test_version(self, arg, capsys):
with pytest.raises(SystemExit) as e:
self.parser.parse_args([arg])
captured = capsys.readouterr()
assert e.value.code == 0
assert captured.out == f"junitparser {version}\n"
def test_help_shows_subcommands(self, capsys):
with pytest.raises(SystemExit) as e:
self.parser.parse_args(["--help"])
captured = capsys.readouterr()
assert "{merge,verify} ...\n" in captured.out
assert e.value.code == 0
@pytest.mark.parametrize("command", ["merge", "verify"])
def test_subcommand_help(self, command):
with pytest.raises(SystemExit) as e:
self.parser.parse_args([command, "--help"])
assert e.value.code == 0
@pytest.mark.parametrize("command", ["merge", "verify"])
def test_subcommands_help_general_options(self, command, capsys):
with pytest.raises(SystemExit):
self.parser.parse_args([command, "--help"])
captured = capsys.readouterr()
assert "[--glob]" in captured.out
assert "paths [paths ...]" in captured.out
def test_merge_help_options(self, capsys):
with pytest.raises(SystemExit):
self.parser.parse_args(["merge", "--help"])
captured = capsys.readouterr()
assert "[--suite-name SUITE_NAME]" in captured.out
assert "output\n" in captured.out
@pytest.mark.parametrize("command", ["merge", "verify"])
def test_option_glob(
self,
command,
):
args = self.parser.parse_args([command, "--glob", "pytest_*.xml", "-"])
assert args.paths_are_globs
def test_verify_argument_path(self):
files = ["foo", "bar"]
args = self.parser.parse_args(["verify", *files])
assert args.paths == files
def test_merge_argument_path(self):
files = ["foo", "bar"]
args = self.parser.parse_args(["merge", *files, "-"])
assert args.paths == files
def test_merge_option_suite_name(self):
args = self.parser.parse_args(["merge", "--suite-name", "foo", "_", "-"])
assert args.suite_name == "foo"
def test_merge_argument_output(self):
args = self.parser.parse_args(["merge", "foo", "bar"])
assert args.output == "bar"
|