sfdx force:apex:test:run include coverage for only those tests included in the sfdx command

2.3k views Asked by At

I have an org that has approx 530 test classes .I am currently working on a nodejs project that uses the sfdx commands to run the tests in the org and then generate code coverage report, analyze and classify them so that I can use the classified data to find what apex class lines are covered by which test classes/methods.

Since the amount of test classes are very large and salesforce removes coverage data if the records exceed 10K (which happens because of such a large amount of classes, triggers and test classes), I have tried to implement batches of tests to be run.

lets say the batch consists of Test_1,Test_2,.....,Test_10. The command I am using is given below.

sfdx force:apex:test:run --codecoverage -r json --classnames "Test_1,Test_2,.....,Test_10" -u testOrg --outputdir path/to/folder

The problem here is

  1. command blocks the script until the test are completed and overall coverage is calculated.
  2. after completion testRunId is not returned.
  3. with each subsequent run, previous test coverage data is also included in the output which is unrequired.

Is there anyway to run the command such that coverage data is only provided for the tests included in the batch?

2

There are 2 answers

0
Brachy On

Try to seperate it to multicommands, what you will you gain this way ?

  1. The lock on the script done for small unit each time
  2. The result can be stored and analyzed seperate each class test

3. Previous test coverage data is NOT included in the output. each command calculate its test code coverage

 sfdx force:apex:test:run --codecoverage -r json --classnames "Test_1" -u testOrg --outputdir path/to/folder/Test_1

sfdx force:apex:test:run --codecoverage -r json --classnames "Test_2" -u testOrg --outputdir path/to/folder/Test_2

sfdx force:apex:test:run --codecoverage -r json --classnames "Test_10" -u testOrg --outputdir path/to/folder/Test_10
1
Igor Iosifov On

Don't know if it's still relevant or not, but there is a solution.

You can overwrite the SFDX_MAX_QUERY_LIMIT environment variable's value and retrieve all records that you need.
From https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_dev_cli_env_variables.htm:
SFDX_MAX_QUERY_LIMIT
The maximum number of Salesforce records returned by a CLI command.
Default value is 10,000.
Example:
SFDX_MAX_QUERY_LIMIT=200000