[test-improver] Improve tests for mcp/connection package#3316
Merged
Conversation
Replace manual t.Errorf/t.Fatalf patterns with proper testify assertions (require.Error, assert.NoError, assert.Contains) for clearer, more idiomatic test failures. Remove redundant containsSubstring and stringContains helper functions that manually duplicated testify's assert.Contains functionality. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR modernizes and simplifies the internal/mcp/connection_test.go test suite by replacing ad-hoc error checks and custom substring helpers with idiomatic Testify assert/require assertions, improving failure clarity and removing duplicated logic.
Changes:
- Replaced manual
t.Errorf/t.Fatalferror-handling branches withassert/requireassertions in several tests. - Removed redundant string-substring helper functions in favor of
assert.Contains. - Simplified equality checks in
TestConnection_IsHTTPusingassert.Equal.
Show a summary per file
| File | Description |
|---|---|
internal/mcp/connection_test.go |
Refactors tests to use consistent Testify assertions and removes redundant helpers for clearer, more maintainable test code. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 0
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.
Test Improvements:
internal/mcp/connection_test.goFile Analyzed
internal/mcp/connection_test.gointernal/mcpImprovements Made
1. Better Testing Patterns (Idiomatic Testify)
Replaced four different manual error-checking patterns with proper testify assertions:
t.Errorf("Failed to read request body: %v", err)→assert.NoError(t, err, "Failed to read request body")t.Fatalf("Failed to create connection: %v", err)→require.True(t, tt.expectError, ...)(correct semantics: fails test immediately if connection creation was unexpected)t.Error("Expected error but got none")→require.Error(t, err, "Expected an error but got none")t.Errorf("Expected error to contain '%s', got: %v", ...)→assert.Contains(t, err.Error(), tt.errorSubstring, ...)Before (in
TestHTTPRequest_ErrorResponses):After:
2. Removed Redundant Helper Functions
containsSubstring(s, substr string) bool— manually reimplemented whatassert.Containsalready doesstringContains(s, substr string) bool— a hand-rolled substring search used only bycontainsSubstringThese helpers (17 lines) duplicated functionality already available in testify's
assert.Contains.3. Cleaner Assertion Style in
TestConnection_IsHTTPif conn.GetHTTPURL() != testServer.URL { t.Errorf(...) }→assert.Equal(t, testServer.URL, conn.GetHTTPURL(), ...)if returnedHeaders[k] != v { t.Errorf(...) }→assert.Equal(t, v, returnedHeaders[k], "Header %s should match", k)Why These Changes?
The
internal/mcp/connection_test.gofile was one of the few test files in the codebase that still used rawt.Errorf/t.Fatalfcalls instead of the testify assertions already imported and used elsewhere in the same file. The manual patterns also included two custom helper functions (containsSubstring,stringContains) that re-implemented string-contains logic already available asassert.Contains.These improvements make test failures produce clearer output (testify shows expected vs. actual values), eliminate dead code, and make the test code consistent with the rest of the codebase.
Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests