Skip to content

[test-improver] Improve tests for mcp/connection package#3316

Merged
lpcox merged 1 commit intomainfrom
test-improver/mcp-connection-testify-de4a3dcb39264d08
Apr 7, 2026
Merged

[test-improver] Improve tests for mcp/connection package#3316
lpcox merged 1 commit intomainfrom
test-improver/mcp-connection-testify-de4a3dcb39264d08

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 7, 2026

Test Improvements: internal/mcp/connection_test.go

File Analyzed

  • Test File: internal/mcp/connection_test.go
  • Package: internal/mcp
  • Lines of Code: 1,020 → 977 (net -43 lines, all noise removed)

Improvements 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):

if err != nil && tt.expectError {
    if tt.errorSubstring != "" && !containsSubstring(err.Error(), tt.errorSubstring) {
        t.Errorf("Expected error to contain '%s', got: %v", tt.errorSubstring, err)
    }
    return
}
if err != nil {
    t.Fatalf("Failed to create connection: %v", err)
}
// ...
if tt.expectError {
    if err == nil {
        t.Error("Expected error but got none")
    } else if tt.errorSubstring != "" && !containsSubstring(err.Error(), tt.errorSubstring) {
        t.Errorf("Expected error to contain '%s', got: %v", tt.errorSubstring, err)
    }
} else {
    if err != nil {
        t.Errorf("Expected no error but got: %v", err)
    }
}

After:

if err != nil {
    require.True(t, tt.expectError, "Unexpected error creating connection: %v", err)
    if tt.errorSubstring != "" {
        assert.Contains(t, err.Error(), tt.errorSubstring, "Error should contain expected substring")
    }
    return
}
// ...
if tt.expectError {
    require.Error(t, err, "Expected an error but got none")
    if tt.errorSubstring != "" {
        assert.Contains(t, err.Error(), tt.errorSubstring, "Error should contain expected substring")
    }
} else {
    assert.NoError(t, err)
}

2. Removed Redundant Helper Functions

  • ✅ Removed containsSubstring(s, substr string) bool — manually reimplemented what assert.Contains already does
  • ✅ Removed stringContains(s, substr string) bool — a hand-rolled substring search used only by containsSubstring

These helpers (17 lines) duplicated functionality already available in testify's assert.Contains.

3. Cleaner Assertion Style in TestConnection_IsHTTP

  • if 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.go file was one of the few test files in the codebase that still used raw t.Errorf/t.Fatalf calls 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 as assert.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

Generated by Test Improver · ● 10.9M ·

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>
@lpcox lpcox marked this pull request as ready for review April 7, 2026 18:58
Copilot AI review requested due to automatic review settings April 7, 2026 18:58
Copy link
Copy Markdown
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 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.Fatalf error-handling branches with assert/require assertions in several tests.
  • Removed redundant string-substring helper functions in favor of assert.Contains.
  • Simplified equality checks in TestConnection_IsHTTP using assert.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

@github github deleted a comment from rdevps Apr 7, 2026
@lpcox lpcox merged commit 0ce7a72 into main Apr 7, 2026
7 checks passed
@lpcox lpcox deleted the test-improver/mcp-connection-testify-de4a3dcb39264d08 branch April 7, 2026 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants