@@ -102,6 +102,15 @@ def test_malformed(self): # type: () -> None
102102
103103
104104class GenerateTestSuiteTestCase (unittest .TestCase ):
105+ # Expected attributes from JUnit XSD
106+ # ref: https://raw.githubusercontent.com/windyroad/JUnit-Schema/master/JUnit.xsd
107+ # @TODO: Better thing to do here would be to curl or otherwise access the
108+ # spec above instead of hardcoding pieces of it here
109+ junit_testsuite_attributes = ['name' , 'timestamp' , 'hostname' , 'tests' ,
110+ 'failures' , 'errors' , 'time' ]
111+ junit_testcase_attributes = ['name' , 'classname' , 'time' ]
112+ junit_error_attributes = ['type' ]
113+
105114 def test_single (self ): # type: () -> None
106115 errors = {'file_name' :
107116 [CppcheckError ('file_name' ,
@@ -111,18 +120,27 @@ def test_single(self): # type: () -> None
111120 'error_id' ,
112121 'verbose error message' )]}
113122 tree = generate_test_suite (errors )
114- root = tree .getroot ()
115- self .assertEqual (root .get ('errors' ), str (1 ))
116- self .assertEqual (root .get ('failures' ), str (0 ))
117- self .assertEqual (root .get ('tests' ), str (1 ))
118-
119- test_case_element = root .find ('testcase' )
120- self .assertEqual (test_case_element .get ('name' ), 'file_name' )
121-
122- error_element = test_case_element .find ('error' )
123+ testsuite_element = tree .getroot ()
124+ self .assertEqual (testsuite_element .get ('errors' ), str (1 ))
125+ self .assertEqual (testsuite_element .get ('failures' ), str (0 ))
126+ self .assertEqual (testsuite_element .get ('tests' ), str (1 ))
127+ # Check that testsuite element is compliant with the spec
128+ for required_attribute in self .junit_testsuite_attributes :
129+ self .assertTrue (required_attribute in testsuite_element .attrib .keys ())
130+
131+ testcase_element = testsuite_element .find ('testcase' )
132+ self .assertEqual (testcase_element .get ('name' ), 'file_name' )
133+ # Check that test_case is compliant with the spec
134+ for required_attribute in self .junit_testcase_attributes :
135+ self .assertTrue (required_attribute in testcase_element .attrib .keys ())
136+
137+ error_element = testcase_element .find ('error' )
123138 self .assertEqual (error_element .get ('file' ), 'file_name' )
124139 self .assertEqual (error_element .get ('line' ), str (4 ))
125140 self .assertEqual (error_element .get ('message' ), '4: (severity) error message' )
141+ # Check that error element is compliant with the spec
142+ for required_attribute in self .junit_error_attributes :
143+ self .assertTrue (required_attribute in error_element .attrib .keys ())
126144
127145 def test_missing_file (self ): # type: () -> None
128146 errors = {'' :
@@ -141,32 +159,57 @@ def test_missing_file(self): # type: () -> None
141159 'that may increase the checking time. For more details, '
142160 'use --enable=information.' )]}
143161 tree = generate_test_suite (errors )
144- root = tree .getroot ()
145- self .assertEqual (root .get ('errors' ), str (1 ))
146- self .assertEqual (root .get ('failures' ), str (0 ))
147- self .assertEqual (root .get ('tests' ), str (1 ))
148-
149- test_case_element = root .find ('testcase' )
150- self .assertEqual (test_case_element .get ('name' ), '' )
151-
152- error_element = test_case_element .find ('error' )
162+ testsuite_element = tree .getroot ()
163+ self .assertEqual (testsuite_element .get ('errors' ), str (1 ))
164+ self .assertEqual (testsuite_element .get ('failures' ), str (0 ))
165+ self .assertEqual (testsuite_element .get ('tests' ), str (1 ))
166+ # Check that testsuite element is compliant with the spec
167+ for required_attribute in self .junit_testsuite_attributes :
168+ self .assertTrue (required_attribute in testsuite_element .attrib .keys ())
169+
170+ testcase_element = testsuite_element .find ('testcase' )
171+ self .assertEqual (testcase_element .get ('name' ), '' )
172+ # Check that test_case is compliant with the spec
173+ for required_attribute in self .junit_testcase_attributes :
174+ self .assertTrue (required_attribute in testcase_element .attrib .keys ())
175+
176+ error_element = testcase_element .find ('error' )
153177 self .assertEqual (error_element .get ('file' ), '' )
154178 self .assertEqual (error_element .get ('line' ), str (0 ))
155179 self .assertEqual (error_element .get ('message' ),
156180 '0: (information) Too many #ifdef configurations - cppcheck only checks '
157181 '12 configurations. Use --force to check all '
158182 'configurations. For more details, use '
159183 '--enable=information.' )
184+ # Check that error element is compliant with the spec
185+ for required_attribute in self .junit_error_attributes :
186+ self .assertTrue (required_attribute in error_element .attrib .keys ())
160187
161188
162189class GenerateSingleSuccessTestSuite (unittest .TestCase ):
190+ # Expected attributes from JUnit XSD
191+ # ref: https://raw.githubusercontent.com/windyroad/JUnit-Schema/master/JUnit.xsd
192+ # @TODO: Better thing to do here would be to curl or otherwise access the
193+ # spec above instead of hardcoding pieces of it here
194+ junit_testsuite_attributes = ['name' , 'timestamp' , 'hostname' , 'tests' ,
195+ 'failures' , 'errors' , 'time' ]
196+ junit_testcase_attributes = ['name' , 'classname' , 'time' ]
197+
163198 def test (self ): # type: () -> None
164199 tree = generate_single_success_test_suite ()
165- root = tree .getroot ()
166- self .assertEqual (root .get ('tests' ), str (1 ))
167-
168- test_case_element = root .find ('testcase' )
169- self .assertEqual (test_case_element .get ('name' ), 'Cppcheck success' )
200+ testsuite_element = tree .getroot ()
201+ self .assertEqual (testsuite_element .get ('tests' ), str (1 ))
202+ self .assertEqual (testsuite_element .get ('errors' ), str (0 ))
203+ self .assertEqual (testsuite_element .get ('failures' ), str (0 ))
204+ # Check that testsuite element is compliant with the spec
205+ for required_attribute in self .junit_testsuite_attributes :
206+ self .assertTrue (required_attribute in testsuite_element .attrib .keys ())
207+
208+ testcase_element = testsuite_element .find ('testcase' )
209+ self .assertEqual (testcase_element .get ('name' ), 'Cppcheck success' )
210+ # Check that test_case is compliant with the spec
211+ for required_attribute in self .junit_testcase_attributes :
212+ self .assertTrue (required_attribute in testcase_element .attrib .keys ())
170213
171214
172215class ParseArgumentsTestCase (unittest .TestCase ):
0 commit comments