-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(output): structured unittest support
- Loading branch information
Showing
9 changed files
with
265 additions
and
35 deletions.
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
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
90 changes: 90 additions & 0 deletions
90
rplugin/python3/ultest/handler/parsers/output/python/unittest.py
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,90 @@ | ||
from .. import parsec as p | ||
from ..base import ParsedOutput, ParseResult | ||
from ..parsec import generate | ||
from ..util import eol, join_chars, until_eol | ||
|
||
|
||
class ErroredTestError(Exception): | ||
... | ||
|
||
|
||
@generate | ||
def unittest_output(): | ||
try: | ||
yield p.many(p.exclude(p.any(), failed_test_title)) | ||
failed_tests = yield p.many1(failed_test) | ||
yield p.many(p.any()) | ||
return ParsedOutput(results=failed_tests) | ||
except ErroredTestError: | ||
return ParsedOutput(results=[]) | ||
|
||
|
||
@generate | ||
def failed_test(): | ||
name, namespace = yield failed_test_title | ||
file, error_line = yield failed_test_traceback | ||
error_message = yield failed_test_error_message | ||
return ParseResult( | ||
name=name, | ||
namespaces=[namespace], | ||
file=file, | ||
message=error_message, | ||
line=error_line, | ||
) | ||
|
||
|
||
@generate | ||
def failed_test_title(): | ||
text = ( | ||
yield p.many1(p.string("=")) | ||
>> eol | ||
>> (p.string("FAIL") ^ p.string("ERROR")) | ||
<< p.string(": ") | ||
) | ||
test = yield p.many1(p.none_of(" ")).parsecmap(join_chars) | ||
yield p.space() | ||
namespace = ( | ||
yield (p.string("(") >> p.many1(p.none_of(")")) << (p.string(")") >> until_eol)) | ||
.parsecmap(join_chars) | ||
.parsecmap(lambda s: s.split(".")[-1]) | ||
) | ||
if namespace == "_FailedTest": | ||
# Can't infer namespace from file that couldn't be imported | ||
raise ErroredTestError | ||
yield p.many1(p.string("-")) >> eol | ||
return test, namespace | ||
|
||
|
||
@generate | ||
def traceback_location(): | ||
file = ( | ||
yield p.spaces() | ||
>> p.string('File "') | ||
>> p.many1(p.none_of('"')).parsecmap(join_chars) | ||
<< p.string('"') | ||
) | ||
line = yield ( | ||
p.string(", line ") | ||
>> p.many1(p.digit()).parsecmap(join_chars).parsecmap(int) | ||
<< until_eol | ||
) | ||
return file, line | ||
|
||
|
||
@generate | ||
def failed_test_traceback(): | ||
yield p.string("Traceback") >> until_eol | ||
file, line = yield traceback_location | ||
yield p.many1(p.string(" ") >> until_eol) | ||
return file, line | ||
|
||
|
||
@generate | ||
def failed_test_error_message(): | ||
message = yield p.many1(p.exclude(until_eol, p.string("--") ^ p.string("=="))) | ||
remove_index = len(message) - 0 | ||
for line in reversed(message): | ||
if line != "": | ||
break | ||
remove_index -= 1 | ||
return message[:remove_index] # Ends with blank lines |
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,17 @@ | ||
from . import parsec as p | ||
from .parsec import generate | ||
|
||
join_chars = lambda chars: "".join(chars) | ||
|
||
|
||
@generate | ||
def until_eol(): | ||
text = yield p.many(p.exclude(p.any(), eol)).parsecmap(join_chars) | ||
yield eol | ||
return text | ||
|
||
|
||
@generate | ||
def eol(): | ||
new_line = yield p.string("\r\n") ^ p.string("\n") | ||
return new_line |
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 |
---|---|---|
@@ -1,13 +1,46 @@ | ||
F. | ||
F3 | ||
EF.F | ||
====================================================================== | ||
FAIL: test_d (test_a.TestMyClass) | ||
ERROR: test_c (test_a.TestClass) | ||
---------------------------------------------------------------------- | ||
Traceback (most recent call last): | ||
File "/home/ronan/tests/test_a.py", line 9, in test_d | ||
assert 33 == 3 | ||
File "/home/ronan/tests/test_a.py", line 37, in test_c | ||
a_function() | ||
File "/home/ronan/tests/tests/__init__.py", line 6, in a_function | ||
raise Exception | ||
Exception | ||
|
||
====================================================================== | ||
FAIL: test_b (test_a.TestClass) | ||
---------------------------------------------------------------------- | ||
Traceback (most recent call last): | ||
File "/home/ronan/tests/test_a.py", line 34, in test_b | ||
self.assertEqual({"a": 1, "b": 2, "c": 3}, {"a": 1, "b": 5, "c": 3, "d": 4}) | ||
AssertionError: {'a': 1, 'b': 2, 'c': 3} != {'a': 1, 'b': 5, 'c': 3, 'd': 4} | ||
- {'a': 1, 'b': 2, 'c': 3} | ||
? ^ | ||
|
||
+ {'a': 1, 'b': 5, 'c': 3, 'd': 4} | ||
? ^ ++++++++ | ||
|
||
|
||
====================================================================== | ||
FAIL: test_a (test_b.AnotherClass) | ||
---------------------------------------------------------------------- | ||
Traceback (most recent call last): | ||
File "/home/ronan/tests/test_b.py", line 7, in test_a | ||
assert 2 == 3 | ||
AssertionError | ||
|
||
====================================================================== | ||
FAIL: test_thing (tests.test_c.TestStuff) | ||
---------------------------------------------------------------------- | ||
Traceback (most recent call last): | ||
File "/home/ronan/tests/tests/test_c.py", line 6, in test_thing | ||
assert False | ||
AssertionError | ||
|
||
---------------------------------------------------------------------- | ||
Ran 2 tests in 0.001s | ||
Ran 5 tests in 0.001s | ||
|
||
FAILED (failures=1) | ||
FAILED (failures=3, errors=1) |
111 changes: 111 additions & 0 deletions
111
tests/unit/handler/parsers/output/python/test_unittest.py
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,111 @@ | ||
from unittest import TestCase | ||
|
||
from rplugin.python3.ultest.handler.parsers.output import OutputParser | ||
from rplugin.python3.ultest.handler.parsers.output.python.unittest import ( | ||
ErroredTestError, | ||
ParseResult, | ||
failed_test, | ||
) | ||
from tests.mocks import get_output | ||
|
||
|
||
class TestUnittestParser(TestCase): | ||
def test_parse_failed_test(self): | ||
raw = """====================================================================== | ||
FAIL: test_b (test_a.TestClass) | ||
---------------------------------------------------------------------- | ||
Traceback (most recent call last): | ||
File "/home/ronan/tests/test_a.py", line 34, in test_b | ||
self.assertEqual({"a": 1, "b": 2, "c": 3}, {"a": 1, "b": 5, "c": 3, "d": 4}) | ||
AssertionError: {'a': 1, 'b': 2, 'c': 3} != {'a': 1, 'b': 5, 'c': 3, 'd': 4} | ||
- {'a': 1, 'b': 2, 'c': 3} | ||
? ^ | ||
+ {'a': 1, 'b': 5, 'c': 3, 'd': 4} | ||
? ^ ++++++++ | ||
""" | ||
|
||
expected = ParseResult( | ||
name="test_b", | ||
namespaces=["TestClass"], | ||
file="/home/ronan/tests/test_a.py", | ||
line=34, | ||
message=[ | ||
"AssertionError: {'a': 1, 'b': 2, 'c': 3} != {'a': 1, 'b': 5, 'c': 3, 'd': 4}", | ||
"- {'a': 1, 'b': 2, 'c': 3}", | ||
"? ^", | ||
"", | ||
"+ {'a': 1, 'b': 5, 'c': 3, 'd': 4}", | ||
"? ^ ++++++++", | ||
], | ||
) | ||
result = failed_test.parse(raw) | ||
self.assertEqual(result, expected) | ||
|
||
def test_parse_errored_test_raises(self): | ||
raw = """====================================================================== | ||
ERROR: test_c (unittest.loader._FailedTest) | ||
---------------------------------------------------------------------- | ||
ImportError: Failed to import test module: test_c | ||
Traceback (most recent call last): | ||
File "/home/ronan/.pyenv/versions/3.8.6/lib/python3.8/unittest/loader.py", line 436, in _find_test_path | ||
module = self._get_module_from_name(name) | ||
File "/home/ronan/.pyenv/versions/3.8.6/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name | ||
__import__(name) | ||
File "/home/ronan/tests/test_c.py", line 6, in <module> | ||
class CTests(TestCase): | ||
File "/home/ronan/tests/test_c.py", line 8, in CTests | ||
@not_a_decorator | ||
NameError: name 'not_a_decorator' is not defined | ||
""" | ||
with self.assertRaises(ErroredTestError): | ||
failed_test.parse(raw) | ||
|
||
def test_parse_unittest(self): | ||
parser = OutputParser([]) | ||
raw = get_output("pyunit") | ||
result = parser.parse_failed("python#pyunit", raw) | ||
expected = [ | ||
ParseResult( | ||
name="test_c", | ||
namespaces=["TestClass"], | ||
file="/home/ronan/tests/test_a.py", | ||
message=["Exception"], | ||
output=None, | ||
line=37, | ||
), | ||
ParseResult( | ||
name="test_b", | ||
namespaces=["TestClass"], | ||
file="/home/ronan/tests/test_a.py", | ||
message=[ | ||
"AssertionError: {'a': 1, 'b': 2, 'c': 3} != {'a': 1, 'b': 5, 'c': 3, 'd': 4}", | ||
"- {'a': 1, 'b': 2, 'c': 3}", | ||
"? ^", | ||
"", | ||
"+ {'a': 1, 'b': 5, 'c': 3, 'd': 4}", | ||
"? ^ ++++++++", | ||
], | ||
output=None, | ||
line=34, | ||
), | ||
ParseResult( | ||
name="test_a", | ||
namespaces=["AnotherClass"], | ||
file="/home/ronan/tests/test_b.py", | ||
message=["AssertionError"], | ||
output=None, | ||
line=7, | ||
), | ||
ParseResult( | ||
name="test_thing", | ||
namespaces=["TestStuff"], | ||
file="/home/ronan/tests/tests/test_c.py", | ||
message=["AssertionError"], | ||
output=None, | ||
line=6, | ||
), | ||
] | ||
self.assertEqual(expected, result) |
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