Skip to content

Commit e56e482

Browse files
sulixshuahkh
authored andcommitted
kunit: tool: Report an error if any test has no subtests
It's possible for a test to have a subtest header, but zero valid subtests. We used to error on this if the test plan had no subtests listed, but it's possible to have subtests without a test plan (indeed, this is how parameterised tests work). Tests with 0 subtests now have the result NO_TESTS, and will report an error (which does not halt test execution, but is printed in a scary red colour and is noted in the results summary). Signed-off-by: David Gow <[email protected]> Reviewed-by: Daniel Latypov <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent c68077b commit e56e482

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

tools/testing/kunit/kunit_parser.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,6 @@ def parse_test_plan(lines: LineStream, test: Test) -> bool:
360360
test.log.append(lines.pop())
361361
expected_count = int(match.group(1))
362362
test.expected_count = expected_count
363-
if expected_count == 0:
364-
test.status = TestStatus.NO_TESTS
365-
test.add_error('0 tests run!')
366363
return True
367364

368365
TEST_RESULT = re.compile(r'^(ok|not ok) ([0-9]+) (- )?([^#]*)( # .*)?$')
@@ -589,6 +586,8 @@ def format_test_result(test: Test) -> str:
589586
return (green('[PASSED] ') + test.name)
590587
elif test.status == TestStatus.SKIPPED:
591588
return (yellow('[SKIPPED] ') + test.name)
589+
elif test.status == TestStatus.NO_TESTS:
590+
return (yellow('[NO TESTS RUN] ') + test.name)
592591
elif test.status == TestStatus.TEST_CRASHED:
593592
print_log(test.log)
594593
return (red('[CRASHED] ') + test.name)
@@ -731,6 +730,7 @@ def parse_test(lines: LineStream, expected_num: int, log: List[str]) -> Test:
731730
# test plan
732731
test.name = "main"
733732
parse_test_plan(lines, test)
733+
parent_test = True
734734
else:
735735
# If KTAP/TAP header is not found, test must be subtest
736736
# header or test result line so parse attempt to parser
@@ -744,7 +744,7 @@ def parse_test(lines: LineStream, expected_num: int, log: List[str]) -> Test:
744744
expected_count = test.expected_count
745745
subtests = []
746746
test_num = 1
747-
while expected_count is None or test_num <= expected_count:
747+
while parent_test and (expected_count is None or test_num <= expected_count):
748748
# Loop to parse any subtests.
749749
# Break after parsing expected number of tests or
750750
# if expected number of tests is unknown break when test
@@ -779,9 +779,15 @@ def parse_test(lines: LineStream, expected_num: int, log: List[str]) -> Test:
779779
parse_test_result(lines, test, expected_num)
780780
else:
781781
test.add_error('missing subtest result line!')
782+
783+
# Check for there being no tests
784+
if parent_test and len(subtests) == 0:
785+
test.status = TestStatus.NO_TESTS
786+
test.add_error('0 tests run!')
787+
782788
# Add statuses to TestCounts attribute in Test object
783789
bubble_up_test_results(test)
784-
if parent_test:
790+
if parent_test and not main:
785791
# If test has subtests and is not the main test object, print
786792
# footer.
787793
print_test_footer(test)

tools/testing/kunit/kunit_tool_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ def test_no_tests(self):
209209
kunit_parser.TestStatus.NO_TESTS,
210210
result.status)
211211

212+
def test_no_tests_no_plan(self):
213+
no_plan_log = test_data_path('test_is_test_passed-no_tests_no_plan.log')
214+
with open(no_plan_log) as file:
215+
result = kunit_parser.parse_run_tests(
216+
kunit_parser.extract_tap_lines(file.readlines()))
217+
self.assertEqual(0, len(result.test.subtests[0].subtests[0].subtests))
218+
self.assertEqual(
219+
kunit_parser.TestStatus.NO_TESTS,
220+
result.test.subtests[0].subtests[0].status)
221+
self.assertEqual(1, result.test.counts.errors)
222+
223+
212224
def test_no_kunit_output(self):
213225
crash_log = test_data_path('test_insufficient_memory.log')
214226
print_mock = mock.patch('builtins.print').start()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
TAP version 14
2+
1..1
3+
# Subtest: suite
4+
1..1
5+
# Subtest: case
6+
ok 1 - case # SKIP
7+
ok 1 - suite

0 commit comments

Comments
 (0)