-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more CLI tests #152
Merged
Merged
Add more CLI tests #152
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
32b50a6
cli: reduce complexity with path-construction
Cube707 87bc3bf
cli: use an abstract parent parser for shared options
Cube707 9fcf998
cli: fix strange handling of args input
Cube707 f8c7282
cli: add default value for `suite_name`
Cube707 82aca4b
sync with 'master'
Cube707 029f0f6
ignore flake8 error
Cube707 020ea22
test: use pathlib in cli test
Cube707 c979cc7
test: global constant for `data/`
Cube707 d486449
test: add cli test for merging
Cube707 e12aef9
test: import `cli` module to avoid to many imports
Cube707 60850fe
test: cli option handling
Cube707 b642f33
test: cli argument parsing
Cube707 416d59a
test: fix cli test which fails only under `lxml`
Cube707 5e44eef
test: fix linting issues
Cube707 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<testsuites> | ||
<testsuite name="pytest" errors="0" failures="1" skipped="0" tests="1" time="0.039" timestamp="2025-01-14T20:33:43.564504+01:00"> | ||
<testcase classname="tests.test_cli" name="test_merge" time="0.001"> | ||
<failure message="NameError: name 'file' is not defined" /> | ||
</testcase> | ||
</testsuite> | ||
</testsuites> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<testsuites> | ||
<testsuite name="pytest" errors="0" failures="0" skipped="0" tests="1" time="0.026" timestamp="2025-01-14T20:33:46.249864+01:00" > | ||
<testcase classname="tests.test_fromfile" name="test_fromfile" time="0.001" /> | ||
</testsuite> | ||
</testsuites> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,107 @@ | ||
import os | ||
from pathlib import Path | ||
import pytest | ||
from junitparser.cli import verify | ||
from junitparser import cli | ||
from junitparser import version | ||
|
||
DATA_DIR = Path(__file__).parent / "data" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"file, expected_exitcode", | ||
[("data/jenkins.xml", 1), ("data/no_fails.xml", 0), ("data/normal.xml", 1)], | ||
[("jenkins.xml", 1), ("no_fails.xml", 0), ("normal.xml", 1)], | ||
) | ||
def test_verify(file, expected_exitcode): | ||
path = os.path.join(os.path.dirname(__file__), file) | ||
assert verify([path]) == expected_exitcode | ||
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" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I presume the variable is not needed if unused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
verify_parser
is still relevant and used and might be modified in the future.Its variable binding is only unused because it currently only has the attributes inherited from the
abstract_parser
and no explicit ones of its own.I believe it should be kept for consistency with the other parsers and to increase the readability for future modifications.
That's why I ignored the warning for flake8