Refactor exception test to isolate single invocation in test_main.py - #329
Merged
Merged
Conversation
Fixed issues: - AaAz4NHds3ud2sinjoFd for python:S5778 rule Generated by SonarQube Agent (task: 337e4438-7d5f-4d33-9166-7cccf246508e)
Seppli11
approved these changes
Aug 24, 2026
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.





Extracted Mock() constructor calls outside the assertRaisesRegex block in test_main.py to ensure only create_scanner_engine() is the invocation being tested for exceptions. This clarifies test intent and resolves the SonarQube code smell that flagged multiple invocations that could potentially throw exceptions within a single exception test block.
View Project in SonarCloud
Fixed Issues
python:S5778 - Refactor this exception test to have only one invocation possibly throwing an exception. • MAJOR • View issue
Location:
tests/unit/test_main.py:168Why is this an issue?
When writing tests for exception handling, clarity is essential. If you chain multiple method calls within the code being tested for an exception, it becomes ambiguous which specific call is expected to raise that exception.
What changed
This hunk extracts the Mock() constructor calls into separate variables (
apiandcache) before theassertRaisesRegexblock. Previously,Mock()calls were inside the exception-testing block alongsidecreate_scanner_engine(), making it ambiguous which invocation was expected to raise the exception. By moving these calls outside the block, onlycreate_scanner_engine()remains as the single invocation that could throw an exception. This addresses the code smell about having multiple invocations that could possibly throw an exception inside an exception test block.SonarQube Remediation Agent uses AI. Check for mistakes.