|
| 1 | +from unittest import TestCase |
| 2 | + |
| 3 | +from rplugin.python3.ultest.handler.parsers.output import OutputParser |
| 4 | +from rplugin.python3.ultest.handler.parsers.output.python.unittest import ( |
| 5 | + ErroredTestError, |
| 6 | + ParseResult, |
| 7 | + failed_test, |
| 8 | +) |
| 9 | +from tests.mocks import get_output |
| 10 | + |
| 11 | + |
| 12 | +class TestUnittestParser(TestCase): |
| 13 | + def test_parse_failed_test(self): |
| 14 | + raw = """====================================================================== |
| 15 | +FAIL: test_b (test_a.TestClass) |
| 16 | +---------------------------------------------------------------------- |
| 17 | +Traceback (most recent call last): |
| 18 | + File "/home/ronan/tests/test_a.py", line 34, in test_b |
| 19 | + self.assertEqual({"a": 1, "b": 2, "c": 3}, {"a": 1, "b": 5, "c": 3, "d": 4}) |
| 20 | +AssertionError: {'a': 1, 'b': 2, 'c': 3} != {'a': 1, 'b': 5, 'c': 3, 'd': 4} |
| 21 | +- {'a': 1, 'b': 2, 'c': 3} |
| 22 | +? ^ |
| 23 | +
|
| 24 | ++ {'a': 1, 'b': 5, 'c': 3, 'd': 4} |
| 25 | +? ^ ++++++++ |
| 26 | +
|
| 27 | +""" |
| 28 | + |
| 29 | + expected = ParseResult( |
| 30 | + name="test_b", |
| 31 | + namespaces=["TestClass"], |
| 32 | + file="/home/ronan/tests/test_a.py", |
| 33 | + line=34, |
| 34 | + message=[ |
| 35 | + "AssertionError: {'a': 1, 'b': 2, 'c': 3} != {'a': 1, 'b': 5, 'c': 3, 'd': 4}", |
| 36 | + "- {'a': 1, 'b': 2, 'c': 3}", |
| 37 | + "? ^", |
| 38 | + "", |
| 39 | + "+ {'a': 1, 'b': 5, 'c': 3, 'd': 4}", |
| 40 | + "? ^ ++++++++", |
| 41 | + ], |
| 42 | + ) |
| 43 | + result = failed_test.parse(raw) |
| 44 | + self.assertEqual(result, expected) |
| 45 | + |
| 46 | + def test_parse_errored_test_raises(self): |
| 47 | + raw = """====================================================================== |
| 48 | +ERROR: test_c (unittest.loader._FailedTest) |
| 49 | +---------------------------------------------------------------------- |
| 50 | +ImportError: Failed to import test module: test_c |
| 51 | +Traceback (most recent call last): |
| 52 | + File "/home/ronan/.pyenv/versions/3.8.6/lib/python3.8/unittest/loader.py", line 436, in _find_test_path |
| 53 | + module = self._get_module_from_name(name) |
| 54 | + File "/home/ronan/.pyenv/versions/3.8.6/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name |
| 55 | + __import__(name) |
| 56 | + File "/home/ronan/tests/test_c.py", line 6, in <module> |
| 57 | + class CTests(TestCase): |
| 58 | + File "/home/ronan/tests/test_c.py", line 8, in CTests |
| 59 | + @not_a_decorator |
| 60 | +NameError: name 'not_a_decorator' is not defined |
| 61 | +
|
| 62 | +""" |
| 63 | + with self.assertRaises(ErroredTestError): |
| 64 | + failed_test.parse(raw) |
| 65 | + |
| 66 | + def test_parse_unittest(self): |
| 67 | + parser = OutputParser([]) |
| 68 | + raw = get_output("pyunit") |
| 69 | + result = parser.parse_failed("python#pyunit", raw) |
| 70 | + expected = [ |
| 71 | + ParseResult( |
| 72 | + name="test_c", |
| 73 | + namespaces=["TestClass"], |
| 74 | + file="/home/ronan/tests/test_a.py", |
| 75 | + message=["Exception"], |
| 76 | + output=None, |
| 77 | + line=37, |
| 78 | + ), |
| 79 | + ParseResult( |
| 80 | + name="test_b", |
| 81 | + namespaces=["TestClass"], |
| 82 | + file="/home/ronan/tests/test_a.py", |
| 83 | + message=[ |
| 84 | + "AssertionError: {'a': 1, 'b': 2, 'c': 3} != {'a': 1, 'b': 5, 'c': 3, 'd': 4}", |
| 85 | + "- {'a': 1, 'b': 2, 'c': 3}", |
| 86 | + "? ^", |
| 87 | + "", |
| 88 | + "+ {'a': 1, 'b': 5, 'c': 3, 'd': 4}", |
| 89 | + "? ^ ++++++++", |
| 90 | + ], |
| 91 | + output=None, |
| 92 | + line=34, |
| 93 | + ), |
| 94 | + ParseResult( |
| 95 | + name="test_a", |
| 96 | + namespaces=["AnotherClass"], |
| 97 | + file="/home/ronan/tests/test_b.py", |
| 98 | + message=["AssertionError"], |
| 99 | + output=None, |
| 100 | + line=7, |
| 101 | + ), |
| 102 | + ParseResult( |
| 103 | + name="test_thing", |
| 104 | + namespaces=["TestStuff"], |
| 105 | + file="/home/ronan/tests/tests/test_c.py", |
| 106 | + message=["AssertionError"], |
| 107 | + output=None, |
| 108 | + line=6, |
| 109 | + ), |
| 110 | + ] |
| 111 | + self.assertEqual(expected, result) |
0 commit comments