9

Since summer 19, sfdx force:source:deploy allows us to push source format meta-data into static orgs and production. The Commands are introduced here

However when I check the help of sfdx force:source:deploy, it seems the lowest number of Apex tests that you can run is --testlevel RunLocalTests.

Is it possible to set it to run only specific apex tests when deploying into a production org?

3 Answers 3

21

Now with the latest sfdx update(7.21.0) you can run 'sfdx force:source:deploy' command with specific test classes. Check below command.

sfdx force:source:deploy -p "./PathToMetadata" -l RunSpecifiedTests -r TestClass1,TestClass2 -w 33 --verbose --loglevel fatal

https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_source.htm

1
  • 3
    Additionally, you can run specific methods from the test class, instead of the default "all methods" - RunSpecifiedTests -r TestClass1.method1,TestClass2 Commented Jan 6, 2021 at 18:21
7

force:source:deploy does let you run specific tests by setting --testlevel to RunSpecifiedTests and then specifying the tests with the --runtests argument.

So you could do something like sfdx force:source:deploy --checkonly --sourcepath force-app --targetusername production-org --testlevel RunSpecifiedTests --runtests MyUnitTests to do a validation and run specific tests.

4
  • what is MyUnitTests its a test package name?
    – ShreyasD
    Commented May 25, 2020 at 18:34
  • 1
    No, it's the name of a test class. If you want to run tests from multiple classes you can use a comma separated list instead. Commented May 26, 2020 at 22:06
  • can we make --runtests MyUnitTests this part of command dynamic? Like point to a specific folder. Commented Apr 18, 2022 at 9:55
  • The --runtests parameter only takes a comma separated list of tests, so there's nothing built into the CLI for passing in a folder - but if you have the tests you want to run in a specific folder there's nothing stopping you from using a script to build a comma separated list of test class names from the files in that folder and passing that list to the command. Commented Apr 20, 2022 at 15:32
0

Due to updates in SF CLI toolkit, Adding new format for deployment using SF toolkit.

Syntax: sf project deploy start -d "relative file path" "relative file 2 path" -l RunSpecifiedTests -t test_class_name

Example: sf project deploy start -d "force-app/main/default/classes/Sample.cls" "force-app/main/default/classes/SampleTest.cls" -l RunSpecifiedTests -t SampleTest

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .