Find your content:

Search form

You are here

Why are my Test Classes (marked @isTest) getting listed with Code Coverage?


For some reason, two (out of ten) of my test classes are showing up in the Class Code Coverage section when I run all Apex tests. They also have a Code Coverage percentage on my Apex class list.

The classes all start with @isTest.

Looking at coverage, I see that Salesforce is marking lines from a subclass of the test class, used as a helper object by the various test methods. It's also flagging lines in static methods used by the test methods.

From this answer to a related question, I had assumed everything inside of my @isTest class would not be included in code coverage metrics.

You can also include non-test utility methods in classes marked as @isTest and (this is the compelling part) they do not need to be covered by tests themselves, and can only be called from inside of test methods! Very useful for data generation methods that are not needed in production code.

More information (and possible cause found): We have Test Classes that use static methods from the Test Classes getting listed with Code Coverage. That would indicate to me that we should potentially have a special "TestHelper" class to hold these methods instead, and that the "TestHelper" class will need to be tested itself.

Attribution to: tomlogic

Possible Suggestion/Solution #1

With one of our test classes, this is a problem in the sandbox. But the identical test class does not have this problem in the production org. Spooky.

Attribution to: Sander de Jong

Possible Suggestion/Solution #2

I just happened to run across this while searching for answers to something else. It sounds as though you may not be not using using "Test.startTest();" and "Test.stopTest();" in between calling your class methods and running the code to verify you're getting the expected results using the portions of your test code behind verifying the code in the Test Class. Other test class code generates the object and fields needed in order to test your class's functionality as opposed to using existing data.

Using psuedo-code, I'd expect a test class to look something like this:

@Test private class My TestMethod {
        create accts ;
        create contacts ;
        do something with accts; 
        do something with contacts ; 
        update accts ;
        update contacts ;
        /* the above could also include adding opportunities or anything else your 
           class included that you needed to test as part of your controller */
        /* run system asserts to verify test data is valid */

        /* if this were an "on update" trigger running as a class nothing would need to
           happen here because the update would cause the trigger to automatically activate */

        write code for calls to the class using the data created above;

        /* check returned values from the class being tested */

        use System.asserts to verify that the class returned the expected results;

Hope this helps anyone who stumbles upon this post.

Attribution to: crmprogdev

Possible Suggestion/Solution #3

It is very simple

 1. Copy the listed test class and paste into notepad.
 2. Delete the listed test class.
 3. Create new test class with **same class name** and **same body**(structure).
 4. Finally Run the test class.

100% it works.Now you test class it not listed with code coverage.

Attribution to: Ramesh S

Possible Suggestion/Solution #4

The code coverage percentages, and the highlighting for code coverage (in my experience at least) is far from perfect.

Maybe try putting them inside of a private class that is marked as @isTest as well:

private class classMethodsTest(){
    static void testMethod1(){
       /* some test code */

That's the pattern I follow to keep things nice and segregated and avoid weird percentage totals!

But I've had some hair pulling out over getting test-code coverage over 75% in some orgs!

Attribution to: jordan.baucke

Possible Suggestion/Solution #5

Are these inner classes or just standalone classes?

When I make a standalone test class (a file with just 1 test class) it looks like this:

private class MyTestClass{  
  public static testmethod void MyTestMethod(){

When defined that like that the code coverage column in the Apex Classes section of setup is just blank. Not 0%, not 100%, just nothing at all.

When I declare a test method inside of an existing class that method does not count against test coverage either. When you run the tests and click on the code coverage % you will see lines of code highlighted blue (for covered) or red (for not covered). Test methods aren't highlighted at all.

Those test methods are defined inside of a normal class like this:

public static testMethod void MyTestMethod(){

If you're using an inner class for your test methods maybe just try breaking them out into either their own class or just as methods within the main class.

Attribution to: Ryan Elkins

Possible Suggestion/Solution #6

Does this happen both when running tests from the IDE/ant and when running tests in the UI?

There's a bug in the IDE/ant case that I'm working on currently.

Attribution to: Rich Unger

Possible Suggestion/Solution #7

If those inner classes are being called during testing, then surely they are contributing positively to test coverage and you wouldn't have any problems?

Are these inner classes being called by any classes other than the one that contains them?

Attribution to: DavidSchach
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content