This repository was archived by the owner on Jan 1, 2026. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +31
-8
lines changed
Expand file tree Collapse file tree 2 files changed +31
-8
lines changed Original file line number Diff line number Diff line change @@ -82,15 +82,14 @@ def parse_cppcheck(file_name):
8282 return errors
8383
8484
85- def generate_test_suite (errors , output_file ):
86- """Writes a JUnit test file from parsed Cppcheck errors .
85+ def generate_test_suite (errors ):
86+ """Converts parsed Cppcheck errors into JUnit XML tree .
8787
8888 Args:
8989 errors (Dict[str, List[CppcheckError]]):
90- output_file (str): File path to create JUnit XML file.
9190
9291 Returns:
93- Nothing .
92+ ElementTree.ElementTree: XML test suite .
9493 """
9594 test_suite = ElementTree .Element ('testsuite' )
9695 test_suite .attrib ['errors' ] = str (len (errors ))
@@ -112,8 +111,7 @@ def generate_test_suite(errors, output_file):
112111 error .severity ,
113112 error .message ))
114113
115- tree = ElementTree .ElementTree (test_suite )
116- tree .write (output_file , encoding = 'utf-8' , xml_declaration = True )
114+ return ElementTree .ElementTree (test_suite )
117115
118116
119117def main ():
@@ -138,7 +136,8 @@ def main():
138136 return EXIT_FAILURE
139137
140138 if len (errors ) > 0 :
141- generate_test_suite (errors , args .output_file )
139+ tree = generate_test_suite (errors )
140+ tree .write (args .output_file , encoding = 'utf-8' , xml_declaration = True )
142141
143142 return EXIT_SUCCESS
144143
Original file line number Diff line number Diff line change 55import unittest
66from xml .etree import ElementTree
77
8- from cppcheck_junit import parse_cppcheck
8+ from cppcheck_junit import CppcheckError , generate_test_suite , parse_cppcheck
99
1010
1111class ParseCppcheckTestCase (unittest .TestCase ):
@@ -65,5 +65,29 @@ def test_malformed(self):
6565 parse_cppcheck ('tests/cppcheck-out-malformed.xml' )
6666
6767
68+ class GenerateTestSuiteTestCase (unittest .TestCase ):
69+ def test_single (self ):
70+ errors = {'file_name' :
71+ [CppcheckError ('file_name' ,
72+ 4 ,
73+ 'error message' ,
74+ 'severity' ,
75+ 'error_id' ,
76+ 'verbose error message' )]}
77+ tree = generate_test_suite (errors )
78+ root = tree .getroot ()
79+ self .assertEqual (root .get ('errors' ), str (1 ))
80+ self .assertEqual (root .get ('failures' ), str (0 ))
81+ self .assertEqual (root .get ('tests' ), str (1 ))
82+
83+ test_case_element = root .find ('testcase' )
84+ self .assertEqual (test_case_element .get ('name' ), 'file_name' )
85+
86+ error_element = test_case_element .find ('error' )
87+ self .assertEqual (error_element .get ('file' ), 'file_name' )
88+ self .assertEqual (error_element .get ('line' ), str (4 ))
89+ self .assertEqual (error_element .get ('message' ), '4: (severity) error message' )
90+
91+
6892if __name__ == '__main__' :
6993 unittest .main ()
You can’t perform that action at this time.
0 commit comments