2

Assume you have 2 classes, A and B, each with 10 methods of equal length. You write 2 test classes, TestA and TestB, each running all 10 methods and producing 100% coverage.

However, when run from TestB, class B calls 2 methods of class A.

So when you run TestA and then look at coverage, class A is covered at 100%. When you run TestB and then look at coverage, class B is covered at 100%, and class A is covered at 20%.

Is there any way to prevent recalculation of dependent class coverage when testing? Is there any way to sequence tests into a specific order? Is there any way to prevent Apex from mistakenly reducing the coverage on a class when it is used indirectly while testing another class? Is there any other workaround for this issue?

6

That's not how unit tests work. The lines tested in each test will be cumulatively covered. You can view the results for an individual test (assuming you turn off "Store Only Aggregate Test Data"), but the coverage will still be correct. I wrote a mock up in my developer org that proves it works correctly. However, if you're having problems, I recommend going to Setup > Develop > Apex Test Execution > View Test History > Clear Test Data, then run all your tests again. Rarely, the server will get the coverage wrong, but clearing the history and running your tests should set things right again.

  • Clearing the history appears to have fixed the problem. It may also be that the problem was specific to the Mavensmate coverage report; I didn't look in developer console to see if it was doing the same thing. – aging hippie Sep 11 '15 at 19:48
  • @aginghippie Mavensmate and Force.com IDE both rely on the Metadata API. It only returns coverage for tests executed. You need to actually check the stored code coverage on the class using the Tooling API or the UI. – sfdcfox Sep 11 '15 at 20:51
  • Marking this as answer, because clearing the history was the solution. – aging hippie Sep 11 '15 at 22:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.