Skip to content

Commit

Permalink
Merge 'tests/unit: Fix exception verification in json2code_test.py' f…
Browse files Browse the repository at this point in the history
…rom Kefu Chai

Previously, exception verification code was placed after the function call
within assertRaises(), making it unreachable. This caused the test to
pass without actually verifying the exception details.

Move the verification code outside the assertRaises() block by capturing
the exception first. This ensures the test properly validates both the
exception type and its attributes.

Example:
Before:
```py
  with self.assertRaises(ValueError):
    function_call()
    self.assertEqual(exc.message, "expected message")  # Never executed
```
After:
```py
  with self.assertRaises(ValueError) as e:
    function_call()

  self.assertEqual(e.exception.message, "expected message")  # Executed
```

Closes #2654

* https://github.com/scylladb/seastar:
  tests/unit: Use http.HTTPStatus constants instead of raw status codes
  tests/unit: Fix exception verification in json2code_test.py
  • Loading branch information
xemul committed Feb 26, 2025
2 parents 927788f + 26c287f commit 6391983
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions tests/unit/json2code_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import unittest
import urllib.request
import urllib.parse
from http import HTTPStatus


class TestJson2Code(unittest.TestCase):
Expand Down Expand Up @@ -79,11 +80,11 @@ def test_missing_path_param(self):
with self.assertRaises(urllib.error.HTTPError) as e:
with urllib.request.urlopen(url):
pass
ex = e.exception
self.assertEqual(ex.code, 404)
response = json.loads(ex.read().decode('utf-8'))
self.assertEqual(response['message'], 'Not found')
self.assertEqual(response['code'], 404)
ex = e.exception
self.assertEqual(ex.code, HTTPStatus.NOT_FOUND)
response = json.loads(ex.read().decode('utf-8'))
self.assertEqual(response['message'], 'Not found')
self.assertEqual(response['code'], HTTPStatus.NOT_FOUND)


if __name__ == '__main__':
Expand Down

0 comments on commit 6391983

Please sign in to comment.