Commit dc8c0a9df2315a6873c40e0d0595123bb4dfb275
1 parent
c788ceaf
tests: Add source position to warning messages
Warning message includes source file and line number, hope this is not dependent on python version..
Showing
1 changed file
with
26 additions
and
4 deletions
tests/common/log_helper/test_log_helper.py
| ... | ... | @@ -172,6 +172,25 @@ class TestLogHelper(unittest.TestCase): |
| 172 | 172 | """Check that warnings are captured and printed correctly""" |
| 173 | 173 | output = self._run_test(['enable', 'warn', 'warning']) |
| 174 | 174 | |
| 175 | + # find out which line contains the call to warnings.warn: | |
| 176 | + warnings_line = None | |
| 177 | + with open(TEST_FILE, 'rt') as reader: | |
| 178 | + for line_idx, line in enumerate(reader): | |
| 179 | + if 'warnings.warn' in line: | |
| 180 | + warnings_line = line_idx + 1 | |
| 181 | + break | |
| 182 | + self.assertNotEqual(warnings_line, None) | |
| 183 | + | |
| 184 | + imported_file = join(dirname(abspath(__file__)), | |
| 185 | + 'log_helper_test_imported.py') | |
| 186 | + imported_line = None | |
| 187 | + with open(imported_file, 'rt') as reader: | |
| 188 | + for line_idx, line in enumerate(reader): | |
| 189 | + if 'warnings.warn' in line: | |
| 190 | + imported_line = line_idx + 1 | |
| 191 | + break | |
| 192 | + self.assertNotEqual(imported_line, None) | |
| 193 | + | |
| 175 | 194 | expect = '\n'.join([ |
| 176 | 195 | 'WARNING ' + log_helper_test_main.WARNING_MESSAGE, |
| 177 | 196 | 'ERROR ' + log_helper_test_main.ERROR_MESSAGE, |
| ... | ... | @@ -179,12 +198,15 @@ class TestLogHelper(unittest.TestCase): |
| 179 | 198 | 'WARNING ' + log_helper_test_imported.WARNING_MESSAGE, |
| 180 | 199 | 'ERROR ' + log_helper_test_imported.ERROR_MESSAGE, |
| 181 | 200 | 'CRITICAL ' + log_helper_test_imported.CRITICAL_MESSAGE, |
| 182 | - 'WARNING ' + log_helper_test_main.ACTUAL_WARNING, | |
| 201 | + 'WARNING {0}:{1}: UserWarning: {2}' | |
| 202 | + .format(TEST_FILE, warnings_line, log_helper_test_main.ACTUAL_WARNING), | |
| 183 | 203 | ' warnings.warn(ACTUAL_WARNING)', # warnings include source line |
| 184 | - 'WARNING ' + log_helper_test_imported.ACTUAL_WARNING, | |
| 204 | + '', | |
| 205 | + 'WARNING {0}:{1}: UserWarning: {2}' | |
| 206 | + .format(imported_file, imported_line, log_helper_test_imported.ACTUAL_WARNING), | |
| 185 | 207 | ' warnings.warn(ACTUAL_WARNING)', # warnings include source line |
| 186 | 208 | ]) |
| 187 | - self.assertEqual(output, expect) | |
| 209 | + self.assertEqual(output.strip(), expect) | |
| 188 | 210 | |
| 189 | 211 | def _assert_json_messages(self, output, messages): |
| 190 | 212 | try: |
| ... | ... | @@ -205,7 +227,7 @@ class TestLogHelper(unittest.TestCase): |
| 205 | 227 | we might get errors or false positives between sequential tests runs) |
| 206 | 228 | |
| 207 | 229 | When arg `run_third_party` is `True`, we do not run the `TEST_FILE` as |
| 208 | - main moduel but the `TEST_FILE_3RD_PARTY` and return contents of | |
| 230 | + main module but the `TEST_FILE_3RD_PARTY` and return contents of | |
| 209 | 231 | `stderr` instead of `stdout`. |
| 210 | 232 | |
| 211 | 233 | TODO: use tests.utils.call_and_capture | ... | ... |