Sfdx force:source:deploy throws “INVALID_OPERATION: testLevel of NoTestRun cannot be used in production organizations”

code-coveragedeploymentsalesforcedxsalesforcedx-cli

I am trying to deploy config only changes (only a permission set change) and while running:

sfdx force:source:deploy -x manifest/package.xml -u Prod

I get the error:

INVALID_OPERATION: testLevel of NoTestRun cannot be used in production organizations

Whereas when I convert the metadata from the Source format to Mdapi format and then try to run:

sfdx force:mdapi:deploy --deploydir mdapi --targetusername Prod

It runs successfully without asking me to run tests.

Why this difference in the behavior between source:deploy and mdapi:deploy? Is there something I am missing?

Best Answer

With both force:mdapi:deploy and force:source:deploy, there is a parameter to specify the test level (-l or --testlevel).

Looking through the documentation, it seems like both should be using the "NoTestRun" level (since you're not including apex or triggers) though it is possible that there is a discrepancy between how the mdapi command is run and how the source command is run. This behavior suggests that either:

  • The force:mdapi:deploy command is actually running with the "RunLocalTests" test level
  • The force:source:deploy command is incorrectly applying a check for the test level when no apex/triggers are being deployed

The metadata api also changed for api >= v34.0. Prior to v34.0, local tests were run when deploying a permissionSet (same with a handful of other metadata types).

There is also a github issue on the sfdc cli project that seems to be almost word for word a match for your issue, which contains a nifty workaround. Simply use the "RunSpecificTests" test level, and provide a test name that does not exist as a test class (it seems like using an Apex class name works, but that's not something I'd suggest doing) in your target org.

Something like sfdx force:source:deploy -m PermissionSet:myPermSetName -u Production -l RunSpecifiedTests -r notATest