0

In our CI process, we trigger a job for validation in Jenkins and have an ant target which runs the validation first with checkOnly as true and runs all the tests.

build.xml :

<target name="sf_build" depends="sf_prepare_deployment">
    <echo message="START: SFDC Deployment" />
    <echo message="../${diffDir}/src" />
    <sf:deploy  username="${sf.deploy.username}" password="${sf.deploy.password}"
                serverurl="${sf.deploy.serverurl}" sessionId="${sf.deploy.sessionId}"
                deployRoot="../${diffDir}/src" maxPoll="${sf.maxPoll}"
                pollWaitMillis="${sf.pollWaitMillis}" checkOnly="${sf.checkOnly}"
                runAllTests="${sf.runAllTests}" logType="${sf.logType}"
    />
</target>

Is there a way to extract the test coverage from the org when only validation job has happened ? We would like to fail the build if the code coverage is below a certain threshold.

Is there any rest api or soap api which can give this result ?

0

You can call the checkDeployStatus method to check the DeployResult object. As far as I'm aware, this is still only currently available in the form of a SOAP call. It is also not exposed via the sf:deploy Ant task, so you'd have to patch in that feature or write your own Ant task, or use some other technique entirely (e.g. write your own deployment tool). This is fairly trivial (I've done one or two myself), but you'll need some Java expertise to make this happen.

Alternatively, you can move away from Ant tasks and instead use sfdx. Just execute the command for deployment with a wait timeout, and it can return a coverage report that you can then parse with a command script to determine total coverage and generate errors. This might be a bit easier to do, as you'll only have to deal with a shell script instead of compiling code.

  • Ok so you mean write a jar with com.force.api force-apex-api this dependency and get the DeployResult. and fire this jar from ant. Ok, this I can do, but will it work when checkOnly is true, will salesforce still calculate the code coverage when its just a validation job. – Nagendra Singh Apr 18 at 3:32
  • @NagendraSingh Yes, code coverage can be requested for any type of deploy. Try it out in SFDX before you commit to a more complicated path like writing code. – sfdcfox Apr 18 at 3:37
0

Thanks @sfdcfox

I was able to fetch the details from Soap service :

public static void main(String[] args) {

    ConnectorConfig config = new ConnectorConfig();
    config.setSessionId("ACCESS_TOKEN");
    config.setServiceEndpoint("INSTANCEURL/services/Soap/m/45.0");
    List<String> lstString = new ArrayList<String>();
    try {
        MetadataConnection metadataConnection = new MetadataConnection(config);
        DeployResult deployResult = metadataConnection.checkDeployStatus("0Af7F00001CKZuk", true);
        DeployDetails details = deployResult.getDetails();
        RunTestsResult runTestResult = details.getRunTestResult();
        for (CodeCoverageWarning codeCoverageWarning : runTestResult.getCodeCoverageWarnings()) {
            lstString.add(codeCoverageWarning.getMessage());
        }

        for (String eachMessage : lstString) {
            System.out.println(eachMessage);
        }


        ;
    } catch (ConnectionException e) {
        e.printStackTrace();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.