Skip to content

Running Apex Test fails #3465

@devansBOX

Description

@devansBOX

Salesforce CLI Issue Report: Code Coverage with Large Test Suites

Environment Information

Salesforce CLI Version: 2.110.22

Platform: Jenkins CI/CD (Linux container)

API Version: v65.0

Use Case: Running automated tests with code coverage in scratch orgs.

Test Suite Size: 188 test classes with a custom namespace prefix.

Issue: HTTP 400 Error with sf apex run test and Code Coverage

Command Attempted: sf apex run test --suite-names MyTestSuite -c -w 50 -r json -d temp

Error: Error (ERROR_HTTP_400): Bad Request

Analysis: The REST request that failed with an HTTP 400 error was likely:

HTTP Method: GET

Endpoint: https://.salesforce.com/services/data/v65.0/tooling/query?q=SELECT+ApexClassOrTriggerId,ApexClassOrTrigger.Name,NumLinesCovered,NumLinesUncovered+FROM+ApexCodeCoverageAggregate+WHERE+ApexClassOrTriggerId+IN+(<188 class IDs>)

Full Query (reconstructed):

SELECT ApexClassOrTriggerId, ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered
FROM ApexCodeCoverageAggregate
WHERE ApexClassOrTriggerId IN (
'<ID_1>',
'<ID_2>',
'<ID_3>',
... (185 more class IDs)
)

Estimated URL Length:

Base URL + path: ~100 characters

Query parameters: ~150 characters

WHERE IN clause with 188 IDs: Each ID is 18 characters + quotes + comma = 21 characters × 188 = 3,948 characters

Total estimated URL length: ~4,200+ characters

Root Cause: The generated URL exceeds standard HTTP GET request length limits (typically 2,048–8,192 characters). When the Salesforce CLI attempts to fetch code coverage for all 188 test classes in a single GET request, it constructs a URL that is too long, resulting in an HTTP 400 Bad Request error from the server.

Description: When running tests with the -c (code coverage) flag on a large test suite, the command fails with an HTTP 400 error. This occurs after test execution during the code coverage retrieval phase. The command executes successfully without the -c flag.

Requested Enhancements

  1. Fix HTTP 400 Error The CLI should handle large code coverage requests without hitting URL length limits. Potential solutions include:

Using POST requests instead of GET for large parameter lists.

Implementing pagination or batching for coverage data retrieval.

Automatically splitting large requests into smaller chunks.

Providing a --batch-size flag for coverage retrieval.

  1. Improve Error Messages The generic HTTP 400 error provides no details about the root cause. More descriptive error messages would help developers diagnose issues faster, such as:

"URL length limit exceeded. Consider reducing the number of classes or using --test-level RunLocalTests."

"Request parameters too large. Try batching your test execution."

  1. Documentation Update documentation to include:

Information about URL length limitations with code coverage.

Recommended patterns for large codebases (200+ test classes).

Best practices for CI/CD pipelines with managed packages.

Business Impact

This issue prevents automated code coverage reporting in CI/CD pipelines for large Salesforce managed packages. As a result, teams must either:

Skip code coverage in automated builds.

Use manual or alternative approaches.

Split test execution into multiple smaller batches, increasing build time.

For enterprise managed package development with hundreds of test classes, reliable code coverage reporting is critical for release quality gates and compliance requirements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue or pull request that identifies or fixes a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions