Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question

6 Answers 6

up vote 1 down vote accepted

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?

share|improve this answer
    
The inner classes are called by other test classes. I'm pretty sure we're getting enough coverage on them, it's just odd to see them show up in the coverage lists. We're going to live with it for now, and consider factoring that code out into a "TestHelper.cls" file where tests can call on it, and perhaps even provide better unit test coverage of the methods it provides. –  tomlogic Sep 10 '12 at 16:45

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.

share|improve this answer
    
It happens from the web interface. I just tried running one test from the IDE and Eclipse locked up and had to be restarted. I don't think I'll try doing it again... –  tomlogic Sep 7 '12 at 19:09

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:

@isTest
private class classMethodsTest(){
    @isTest
    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!

share|improve this answer
1  
When adding another @isTest before the inner, private class: "Only top-level non-exception class types can be marked as tests". –  tomlogic Sep 6 '12 at 22:10
    
tomlogic - yeah this would be in a separate top-level class. Guess I should have made that clear. (8 –  jordan.baucke Sep 6 '12 at 22:10

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:

@isTest
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.

share|improve this answer
    
Ryan, I have standalone classes, marked as @isTest. Inside of that class, there's another public class Foo that other test classes access (helper functions for creating objects). Those inner classes can't be tagged @isTest, and are what's causing test coverage issues. –  tomlogic Sep 6 '12 at 23:09

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 */

        Test.startTest();
        /* 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;

        Test.endTest()
        /* 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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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