3

I am trying to do a sfdx force:mdapi:deploy to my production org. I am just migrating one class file, however when I do that I keep getting code coverage failures on custom classes that are already in the production org and not being migrated with this deployment. Is this normal behavior? This is my first deployment and I not sure if I now need to write test classes for those existing classes.

  • How did you make the package.xml file that gets used with force:mdapi:deploy? If You used force:source:convert, then the package.xml file contains everything in your sfdx project (not just the files that were changed) – Derek F Apr 15 at 19:12
  • @DerekF I made my change and did a deploy to the sandbox org. Then I used sfdx force:mdapi:retrieve -r metadata -u prodOrg -k manifest/package.xml. In this package.xml I only specified the Apex class I wanted. I only see that file in the zip – MarkC5000 Apr 15 at 19:23
1

You need to specify which tests to run to get sufficient coverage. Presuming you already have proper coverage, you can simply just do this:

sfdx force:mdapi:deploy -d src -l RunLocalTests -w 60

Where -d is the source for the code you're deploying, -l tells Salesforce which tests to run (the demonstrated option runs all of your org's tests), and -w is the time to wait, in minutes (adjust as necessary).

See force:mdapi:deploy documentation for more options you can choose.

If you specify RunLocalTests, the aggregate code coverage must be 75%; if you choose RunSpecifiedTests, each class that runs in a test must individually meet the 75% requirement. In other words, even though you're deploying just one class you need to make sure all classes touched by any unit tests that are run have at least 75% coverage if you want to only run the one unit test. Otherwise, you'll need to run all tests to get the more lenient 75% aggregate coverage limit.

  • Right now I have 1 test class that has covered my new updated code, but its failing on the existing classes, but I guess that is normal then. Would I be able to get around this if I use change sets or would I still need to write tests for these other classes? I am just wondering how this code was able to be migrated in the first place if there was no coverage. – MarkC5000 Apr 15 at 19:33
  • @MarkC5000 Did you use RunLocalTests? – sfdcfox Apr 15 at 20:01
  • I was able to run a specific test and then I was able to push the code through – MarkC5000 Apr 26 at 16:20
  • I think @MarkC5000 marked this is answer although it was not. I constantly run into such problems WITH RunLocalTests and the overall coverage being above 75%. – Robert Sösemann Jun 7 at 14:42

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.