function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SpinozaQSpinozaQ 

Code Comments and Blank Lines Adversely Affecting Test Coverage

I have been refactoring a large application recently and have began creating tests for the major components of the application today. I have been executing a single test through the Apex Test Excecution feature. Upon looking at the code coverage for a basic class, I found that many code comments, and blank lines ( only white space. ) were marked in red, indicating they are not covered. This is clearly an error, comments should always remain white. I verified this was adversely affecting code coverage by removing the comments, and re-running the test. The coverage increased by 2 percentage points.

Here is a screen shot of the code coverage stats showing comments and blank lines in red.  I believe the parsing issue may somewhat involve the multi-line SOQL query. When I place the query on one line some of the issues are corrected.

 

Code Coverage APEX Test Run

 

 

Best Answer chosen by Admin (Salesforce Developers) 
gaisergaiser

When you see comments highlighted as lines not covered by unit tests this usually means that it is time to clear test history.

 

Setup->Develop->Apex Test Execution->"View Test History" and there click "Clear Test Results".

If you now re-run your unit tests then coverage results should be more sane.

All Answers

DrBix65DrBix65

I've seen that before myself, and it sucks.  One thing that I noticed on some of our older code was that the unit tests and the code were different versions.  So a piece of Apex code may have been version 20.0 and the unit test would be 23.0.  You might want to try making sure both the code and the unit test are the same version.  It did have an effect on our code, but as usual, YMMV.

crop1645crop1645

Also -- if you use Eclipse's Force.com IDE, it does the correct job of showing the lines where no code coverage exists - I've never seen comments ever listed as 'uncovered' lines

gaisergaiser

When you see comments highlighted as lines not covered by unit tests this usually means that it is time to clear test history.

 

Setup->Develop->Apex Test Execution->"View Test History" and there click "Clear Test Results".

If you now re-run your unit tests then coverage results should be more sane.

This was selected as the best answer
BenPBenP

This did it for me.  Was driving me bananas!