Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 13, 2025

Overview

This PR significantly improves test coverage for the InsightVM-Python project, increasing overall coverage from 29.7% to 51.3% (+21.6 percentage points, a 72.7% relative increase). The changes add 128+ new test cases across 5 new test files, following established testing patterns and maintaining consistency with existing code.

Motivation

The project's test coverage was below the minimum target of 70% outlined in CONTRIBUTING.md. Critical modules like config.py and ui.py had 0% coverage, while key API modules (Scans, Reports, Sites) had coverage below 50%. This lack of testing made it difficult to:

  • Catch regressions during refactoring
  • Ensure API methods work correctly
  • Maintain code quality as the project grows

Changes Made

New Test Files

1. Configuration Management (tests/test_config.py)

  • 16 tests covering config initialization, persistence, preferences, and state management
  • Coverage improvement: 0% → 86%
  • Tests config file I/O, JSON error handling, default values, and tool state persistence

2. UI Utilities (tests/test_ui.py)

  • 24 tests for colored output, prompts, progress bars, and tables
  • Coverage improvement: 0% → 72.9%
  • Tests both rich-enhanced output and fallback ASCII mode
  • Validates user interaction methods (confirm, prompt, select_menu)

3. Scans API (tests/test_rapid7/test_scans.py)

  • 34 tests for scan lifecycle management
  • Coverage improvement: 20.8% → 67.5%
  • Tests scan creation, start/stop/pause/resume operations, status monitoring
  • Includes integration tests for complete scan workflows

4. Reports API (tests/test_rapid7/test_reports.py)

  • 32 tests for report generation and management
  • Coverage improvement: 29.5% → 42.1%
  • Tests CRUD operations, report generation, instance management, downloads
  • Validates binary content handling with return_raw=True

5. Sites API (tests/test_rapid7/test_sites.py)

  • 22 tests for site management
  • Coverage improvement: 46.8% → 68.1%
  • Tests site creation, configuration, scan template/engine management
  • Validates error handling for nonexistent resources

Testing Patterns Established

All new tests follow consistent patterns:

  • Fixtures: Reusable mock_auth and API client fixtures
  • Mocking: Comprehensive use of unittest.mock to avoid real API calls
  • Error Handling: Tests for HTTP errors and edge cases using pytest.raises
  • Integration Tests: End-to-end workflow scenarios
  • Type Validation: Ensures methods return correct types per signatures

Additional Improvements

  • Updated .gitignore to exclude coverage.json
  • Followed existing conventions from test_assets.py and test_base.py
  • All tests use environment variable mocking via conftest.py
  • Maintained Google-style docstrings for all test methods

Test Results

======================== 155 passed, 6 skipped in 5.36s ========================

Coverage by Module:

  • base.py: 100% (complete)
  • auth.py: 100% (complete)
  • client.py: 100% (complete)
  • constants.py: 100% (complete)
  • config.py: 86.0% (+86%)
  • assets.py: 78.6% (existing)
  • ui.py: 72.9% (+72.9%)
  • sites.py: 68.1% (+21.3%)
  • scans.py: 67.5% (+46.7%)
  • reports.py: 42.1% (+12.6%)

Verification

All new tests can be run with:

pytest --cov=src/rapid7 --cov-report=term-missing

Individual test files:

pytest tests/test_config.py -v
pytest tests/test_ui.py -v
pytest tests/test_rapid7/test_scans.py -v
pytest tests/test_rapid7/test_reports.py -v
pytest tests/test_rapid7/test_sites.py -v

Future Work

While this PR brings coverage to 51.3%, additional tests could be added for:

  • Asset Groups API (20.6%)
  • Solutions API (22.8%)
  • Vulnerabilities API (18.6%)
  • Scan Templates API (15.9%)
  • Users API (23.7%)
  • Other API modules currently below 30%

These improvements provide a solid foundation and testing patterns that can be extended to remaining modules to reach the 70%+ target.

Related Issues

Closes #[issue-number]

Original prompt

improve test coverage

Created from VS Code via the GitHub Pull Request extension.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits October 13, 2025 22:57
Co-authored-by: talltechy <43618761+talltechy@users.noreply.github.com>
… 46.0%

Co-authored-by: talltechy <43618761+talltechy@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR focuses on improving test coverage for the InsightVM-Python codebase, targeting a minimum of 70% coverage as specified in CONTRIBUTING.md. The work includes comprehensive unit tests for core utility modules to increase coverage from 29.7% to 46%.

Key changes:

  • Added comprehensive test coverage for the UI module (72.9% coverage)
  • Added comprehensive test coverage for the Config module (86% coverage)
  • Updated .gitignore to exclude coverage.json files

Reviewed Changes

Copilot reviewed 2 out of 13 changed files in this pull request and generated no comments.

File Description
tests/test_ui.py Comprehensive unit tests for UI module covering Color enum, UI class methods, SimpleProgressBar, and Rich library integration
tests/test_config.py Comprehensive unit tests for Config class covering initialization, file operations, preferences, tool configurations, and state management

Copy link
Contributor

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bandit found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

import json
import pytest
from pathlib import Path
from unittest.mock import Mock, patch, mock_open

Check warning

Code scanning / Prospector (reported by Codacy)

'unittest.mock.Mock' imported but unused (F401) Warning test

'unittest.mock.Mock' imported but unused (F401)
"""

import pytest
from unittest.mock import Mock, patch, MagicMock

Check warning

Code scanning / Prospector (reported by Codacy)

'unittest.mock.MagicMock' imported but unused (F401) Warning test

'unittest.mock.MagicMock' imported but unused (F401)

import pytest
from unittest.mock import Mock, patch, MagicMock
from io import StringIO

Check warning

Code scanning / Prospector (reported by Codacy)

'io.StringIO' imported but unused (F401) Warning test

'io.StringIO' imported but unused (F401)
import pytest
from unittest.mock import Mock, patch, MagicMock
from io import StringIO
import sys

Check warning

Code scanning / Prospector (reported by Codacy)

'sys' imported but unused (F401) Warning test

'sys' imported but unused (F401)
Copy link
Contributor

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bandit (reported by Codacy) found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Copy link
Contributor

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pylintpython3 (reported by Codacy) found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Copilot AI and others added 2 commits October 13, 2025 23:08
…> 50.6%

Co-authored-by: talltechy <43618761+talltechy@users.noreply.github.com>
Co-authored-by: talltechy <43618761+talltechy@users.noreply.github.com>
Copilot AI changed the title [WIP] Enhance test coverage for existing codebase Improve test coverage from 29.7% to 51.3% - Add comprehensive tests for core modules Oct 13, 2025
@talltechy talltechy requested a review from Copilot October 13, 2025 23:13
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 5 out of 19 changed files in this pull request and generated 3 comments.

Comment on lines +18 to +22
"""Mock authentication for testing."""
auth = Mock()
auth.base_url = "https://test.insightvm.example.com:3780"
auth.auth = Mock()
return auth
Copy link

Copilot AI Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mock_auth fixture is duplicated across all test files. This should be moved to conftest.py to avoid code duplication and ensure consistency across tests.

Copilot uses AI. Check for mistakes.
Comment on lines +17 to +22
def mock_auth(self):
"""Mock authentication for testing."""
auth = Mock()
auth.base_url = "https://test.insightvm.example.com:3780"
auth.auth = Mock()
return auth
Copy link

Copilot AI Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mock_auth fixture is duplicated across all test files. This should be moved to conftest.py to avoid code duplication and ensure consistency across tests.

Copilot uses AI. Check for mistakes.
Comment on lines +17 to +22
def mock_auth(self):
"""Mock authentication for testing."""
auth = Mock()
auth.base_url = "https://test.insightvm.example.com:3780"
auth.auth = Mock()
return auth
Copy link

Copilot AI Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mock_auth fixture is duplicated across all test files. This should be moved to conftest.py to avoid code duplication and ensure consistency across tests.

Copilot uses AI. Check for mistakes.
report_id = 123
mock_request.return_value = {}

result = reports_api.delete_report(report_id)

Check warning

Code scanning / Prospector (reported by Codacy)

local variable 'result' is assigned to but never used (F841) Warning test

local variable 'result' is assigned to but never used (F841)
scan_id = 123
mock_request.return_value = {}

result = scans_api.stop_scan(scan_id)

Check warning

Code scanning / Prospector (reported by Codacy)

local variable 'result' is assigned to but never used (F841) Warning test

local variable 'result' is assigned to but never used (F841)
scan_id = 123
mock_request.return_value = {}

result = scans_api.pause_scan(scan_id)

Check warning

Code scanning / Prospector (reported by Codacy)

local variable 'result' is assigned to but never used (F841) Warning test

local variable 'result' is assigned to but never used (F841)
scan_id = 123
mock_request.return_value = {}

result = scans_api.resume_scan(scan_id)

Check warning

Code scanning / Prospector (reported by Codacy)

local variable 'result' is assigned to but never used (F841) Warning test

local variable 'result' is assigned to but never used (F841)
@patch('rapid7.api.sites.BaseAPI._request')
def test_create_site(self, mock_request, sites_api):
"""Test creating a new site."""
site_config = {

Check warning

Code scanning / Prospector (reported by Codacy)

local variable 'site_config' is assigned to but never used (F841) Warning test

local variable 'site_config' is assigned to but never used (F841)
site_id = 123
mock_request.return_value = {}

result = sites_api.delete_site(site_id)

Check warning

Code scanning / Prospector (reported by Codacy)

local variable 'result' is assigned to but never used (F841) Warning test

local variable 'result' is assigned to but never used (F841)
template_id = "full-audit"
mock_request.return_value = {}

result = sites_api.set_scan_template(site_id, template_id)

Check warning

Code scanning / Prospector (reported by Codacy)

local variable 'result' is assigned to but never used (F841) Warning test

local variable 'result' is assigned to but never used (F841)
engine_id = 5
mock_request.return_value = {}

result = sites_api.set_scan_engine(site_id, engine_id)

Check warning

Code scanning / Prospector (reported by Codacy)

local variable 'result' is assigned to but never used (F841) Warning test

local variable 'result' is assigned to but never used (F841)
@talltechy talltechy marked this pull request as ready for review October 13, 2025 23:20
@codacy-production
Copy link

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+21.01% (target: -1.00%)
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (be598fd) 1204 414 34.39%
Head commit (2b9700a) 1204 (+0) 667 (+253) 55.40% (+21.01%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#105) 0 0 ∅ (not applicable)

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

@talltechy talltechy merged commit 08e18fe into main Oct 13, 2025
25 checks passed
@talltechy talltechy deleted the copilot/improve-test-coverage branch October 13, 2025 23:29
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.

2 participants