1

Currently I do most of my development in VScode using sfdx.

For writing test classes, I need to see which lines are covered in Apex classes I write.

In DX, running commands (see below) to get coverage result takes very long (perhaps more than three minutes when Apex test execution time is less than five seconds).

sfdx force:apex:test:run --classnames AnApexClass --resultformat json --outputdir d:\VScode projects\example\.sfdx\tools\testresults\apex --loglevel error --codecoverage

Therefore I usually use Developer Console for doing this job. In Developer Console, the coverage result usually comes out right after the test methods finish executing. The downside of Developer console is that after each test run, I still need to find the Apex class I want by scrolling down in the "Overall Code Coverage" section. This is also quite time consuming when I need to run the test a couple of times.

I would like to ask if there is any way to make retrieving coverage result faster in DX?

Or, if there is any other tool better than Developer Console or DX?

Thanks.

1
  • 1
    VS Code seems to run tests fast for me in some quick testing i just did. I typically just use sfdx force:apex:test:run -n TestClassName -r human -c. Have you tried clicking on ""Run all Tests" in the Apex test itself in the VS editor to see if that is faster? Also, might be useful to make sure you're on the latest update with sfdx update Commented Mar 18, 2020 at 11:33

2 Answers 2

4

Fortunately, there is an easier way to do this. I often use this while writing apex unit tests.

As mentioned in the comment by Kris, you can run specific or all test class methods from a class using the VS code.

To be able to see the code highlighted, you need to do enable it from the VS code bottom toolbar.

enter image description here

When you have that checked, you will be able to see the code coverage highlight in the VS Code for every test run.

Also, you will be able to see class wise coverage percentage (and line-wise) in the output panel of the VS code. There you can look for your specific classes coverage.

enter image description here


Also, you can invoke all tests, Test Suits, Specific Test classes using the command palette of the VS Code.

Just type 'Invoke Apex Tests' in the command palette, you will see all available test classes and suites to execute.

3
  • 1
    Hi Rahul, thanks for your sharing. Maybe I did not state my question clearly enough. I do know how to get the test result (whether running specific test classes or run all tests) and see line-by-line test coverage in DX. What bothers me is that it usually takes more than 3 minutes to run sfdx test command if --codecoverage is included. How can I make it faster? Commented Mar 19, 2020 at 6:49
  • @ZhuangPaulus Oops, I find no such lag unless the internet is slow, I use it regularly. Maybe your tests are taking that much time to run or it can be an internet issue. Commented Mar 19, 2020 at 6:56
  • 1
    thanks a lot. After changing the internet it no longer takes so long. Really good tool to use. :-) Commented Mar 27, 2020 at 9:04
1

Disable retrieve-test-code-coverage from settings and try this vscode extension. You can get code coverage as in Developer Console in less time.

https://marketplace.visualstudio.com/items?itemName=RaffaelePreziosi.apexgetcoverage

You must log in to answer this question.

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