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
Wayne_ClarkWayne_Clark 

Code Coverage

Code Coverage: 39%

Your overall code coverage is currently 39%. To deploy code to production, you must have at least 75%.

 

We have about 10 Apex classes that are reading 0% code coverage.

 

I run the test on one of the them and the code coverage shows 100%.

 

I then run a test on another class that reads 0% and the code coverage shows 100%.

 

However, the coverage on the first class I ran now shows 0%.

 

I have about 10 classes showing 0%, but when I run individual test on them, the are above 75%.  Some of these classes include the test code inside the class.  But, there are a few of them where I'm not able to run the test on them, I'm not even sure how these made it to production without being tested.

 

How do I make all the classes read the correct code coverage?  I'm thinking I need to seperate out the test coverage and make a seperate test class for the ones that include the test code, but I wanted to get some feedback before I tackle this.

 

Any feedback is greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

So once my unit test run finished it showed 77% coverage with a bunch of failures. That was a bit of a surprise as I use this dev org to try stuff out and don't always write tests. Digging a bit deeper, the unit tests for several managed packages that I'd installed were running and that was why the figure was high. When I clicked to calculate my orgs coverage, that came up with 57%, which seems a lot more likely, so I'm surmising that actually calculates the coverage for the classes that I have written rather than installed.

 

Do you have any packages installed that could be skewing the results upwards?

All Answers

bob_buzzardbob_buzzard

How are you running the tests?  If from the force.com IDE, then the summary that it gives you is the code coverage based on the tests that you ran.  Hence when you run tests for a single class, most other classes drop to zero unless your code uses them.

 

You can get the big picture by right clicking on the src "folder" of your project and choosing the "run all tests menu option", or by running all tests from the UI.

 

If you are getting full coverage in isolation but not when running all tests, that sounds like you have tests failing somewhere.  From the sound of it you are trying to deploy, so it may be that there is data in your production org that is interfering with the test.

 

I have seen cases where code has been deployed through change sets without unit test coverage, but I think (hope) that has been closed off now.

 

For those that don't have tests inside the class - is there a separate test class that exercises them?  Not that it really matters that much if you use the run all tests mechanism - that runs everything in your org that is marked as a testmethod.

MiddhaMiddha

Whenever you execute the tests in a class it only shows you the code coverage of the classes/triggers executed by your test code showing all others as 0 test coverage.

 

To deploy code you need an average of 75% all the class code covered, You can get this average of all the classes from within Salesforce. Login to your salesforce org, go to Setup>> Develop >> Apex Classes. Click on "Run all tests" button. 

 

You'll get a consolodated report on test coverage on all the classes you have.

Wayne_ClarkWayne_Clark

Thanks for the feedback.

 

I noticed this issue when I went to deploy some code from the Force.com IDE a couple months ago.  It wouldnt let me deploy because I didn't have coverage on a bunch of classes.  However, I didn't worry too much about it then because we didn't need the code I created anyway.

 

Now, yesterday I was trying to use the Force.com IDE to create about 50 fields on a custom object, but when I went to save the fields I created to the server, it wouldn't let me. 

 

I just clicked on the "Run all tests" button and it shows me a the report with only 64% code coverage on all the tests.  I can see some of the classes that  have 0% coverage and some that have over 75%. 

 

We may be able to remove some of those classes, but I'm guessing I'm going to have to write some test classes for the ones that we still need but need to increase the coverage for.

 

That Apex Code Testing and Best Practice webinar is coming just in time.

Wayne_ClarkWayne_Clark

After looking more into the methods with failures, they all have the same error message I noticed yesterday in the Force.com IDE:

 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Job_End_Date__c, Job_Start_Date__c]: [Job_End_Date__c, Job_Start_Date__c]

 

Could it be that all these codes are just missing the required field?

bob_buzzardbob_buzzard

Highly likely.  One of the easiest ways to break unit tests is to make fields required (either through the field or validation rules).

 

It would also explain how code is on production without unit test coverage - the configuratio has shifted underneath them and broken them.

Wayne_ClarkWayne_Clark

OK, so I edited the Job_Start_Date__c and the Job_End_Date__c and unchecked the required field checkbox, and only made it required on the page layout level.

 

I then clicked the "Run All Tests" button and it went up to 75% and I thought I was all good. 

 

I then clicked the "calculate your organizations code coverage" link and it said only 33% is covered. 

 

I then tried to create a field on a custom object through the Force.com IDE and the error message showed up again saying only 33% is covered.

 

I'm confused now, why did one way show me that 75% was covered and the other way shows only 33%?

bob_buzzardbob_buzzard

That's a very good question. On my dev org, it shows 100% even though I'm nowhere close to that figure. It also comes back very quickly so I'm guessing that it doesn't actually run the tests.  I'm now running a full test set and will post up what happens when that has completed.

 

 

bob_buzzardbob_buzzard

So once my unit test run finished it showed 77% coverage with a bunch of failures. That was a bit of a surprise as I use this dev org to try stuff out and don't always write tests. Digging a bit deeper, the unit tests for several managed packages that I'd installed were running and that was why the figure was high. When I clicked to calculate my orgs coverage, that came up with 57%, which seems a lot more likely, so I'm surmising that actually calculates the coverage for the classes that I have written rather than installed.

 

Do you have any packages installed that could be skewing the results upwards?

This was selected as the best answer
Wayne_ClarkWayne_Clark

Yeah, we have a few packages installed.

 

I'm noticing now that all the classes that read 0% are the ones without the test class included inside the class.  I'm also noticing that these classes have exact copies (of the names at least), but one of the copies are managed, so I can't see the code.  I'm guessing I can do without the copy that is unmanaged with 0% code coverage.

 

Whats the best way to go about seeing if these classes are even being used, if they're marked inactive, would I be able to increase the overall coverage?

 

Thanks again.

bob_buzzardbob_buzzard

If you navigate to the class, you can click the Show Dependencies button, which should give you the information.  If they are deactivated I wouldn't expect them to require unit test coverage, but I've not actually tried that myself.

Wayne_ClarkWayne_Clark

In the Dependency Information, would it be safe to assume that if there are no check marks in any of the columns (upsert, insert, merge, etc.) that the class is not being used anywhere?

 

 

bob_buzzardbob_buzzard

Unfortunately that doesn't appear to be the case - I have a controller that is backing a component and it doesn't show as having any dependencies.  I'm not sure what would happen if I tried to delete it though.

 

It may be better to choose all metadata components in your force.com ide and search that way.