Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/unit: Fix exception verification in json2code_test.py #2654

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tchaikov
Copy link
Contributor

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:

  with self.assertRaises(ValueError):
    function_call()
    self.assertEqual(exc.message, "expected message")  # Never executed

After:

  with self.assertRaises(ValueError) as e:
    function_call()

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

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
```

Signed-off-by: Kefu Chai <[email protected]>
Replace hardcoded HTTP status code 404 with http.HTTPStatus.NOT_FOUND
to improve code readability and maintainability. Using symbolic constants
makes the test's intent clearer and helps prevent typos.

Before:
  self.assertEqual(response.status_code, 404)

After:
  self.assertEqual(response.status_code, http.HTTPStatus.NOT_FOUND)

Signed-off-by: Kefu Chai <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant