1

I'm not SF expert. I'm helping another team to improve and apply some code standards.

How to check the test coverage before git push? I'm trying to deploy and execute staged Apex Classes tests before the git push using SF CLI.

What I'm trying to achieve is to check only the staged classes' test coverage and if their tests are passed before the code is pushed into the repository. So I'm using lint-staged to find all staged Class Names and their Tests and SF CLI to deploy and run the tests. Based on the results I check if tests are passing and if test coverage is above the threshold.

What I don't understand:

  1. Is this a valid/good idea in general?
  2. What is the right way to do it? ( is --dray-run ok?)

Should I think for --dry-run like it is compiling only the listed classes on the side and then running tests on it?

sf project deploy start --metadata ApexClass:PubSubSomething ApexClass:QueueableSomething ApexClass:AnotherExample --junit --tests PubSubSomethingTest QueueableSomethingTest AnotherExampleTest --dry-run --ignore-conflicts --json --results-dir ./.tests/apex --coverage-formatters=json --target-org ExampleOrg

1 Answer 1

2

There's no easy way to find "all staged Class Names and their Tests." After all, any given class may be covered by just one test (e.g. controller classes), while others might be involved in many tests (e.g. utility classes).

It is customary to simply run all tests when pushing to the server. This ensures that all units are compiling correctly and all unit tests are running. You can use --dry-run to avoid committing changes to a given server, which is how some CI/CD environments are set up.

Remember that any failures in coverage, or any failed tests, will rollback all changes by default in any org. This means that as long as you're running tests without purposefully ignoring warnings and errors, you don't necessarily need --dry-run.

Another common setup is to use Husky (configured by default with the sf project generate command) to run unit tests and/or linters before committing, which may be useful when you're using Scratch Orgs for development.

For CI/CD on the server, you can use pre-commit actions to run tests on a separate org, perhaps a Sandbox or other Scratch Org, to validate that the branch as a whole is still compiling and passing tests correctly.

There's no One Right Way to do it, and there are many articles online that vary in implementation details. The most important thing is to try out various configurations and see what works best for you and/or your team.

1
  • "all staged Class Names and their Tests." means only all class files that are currently staged in git. I don't want to test everything. We have CI/CD for that. It is just to check classes that have been worked on in the last commit/commits. Save some time of falling CI/CD and developer going back and forward fixing stuff. I have set up Husky with lint, prettier, PMD, unit tests, and so on. Commented Jun 29 at 22:51

You must log in to answer this question.

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