[SalesForce] Test Data Factory Methods counting against code coverage

I have created a Test Data Factory helper class to create Test Data for testing my apex code like suggested in the documentation here: https://developer.salesforce.com/docs/atlas.en-us.200.0.apexcode.meta/apexcode/apex_testing_utility_classes.htm

The TestDataFactory methods that don't have any parameters like so:

public testmethod static void CreateProducts()

can be marked with the testmethod keyword and don't count against code coverage. However, if the method has parameters like they have in the documentation example and like below:

public static void CreateBusinesses(Integer NumberOfBusinessesToCreate)

you cannot mark it with the testMethod keyword or @isTest attribute because you get the error "Test methods must have no arguments". Keep in mind, the TestDataFactory class that holds all these methods is decorated with the @isTest attribute as well.

The problem is, my TestDataFactory class is quite large and has many methods, and any of the methods that aren't marked as test methods are being counted as uncovered towards my code coverage (the developer console shows uncovered lines). The methods that are marked as testmethods are being counted as covered.

Also, some people have mentioned it might be an issue with the developer console and maybe it isn't actually counting it, but it is counting it because at first I didn't have any of the methods in the TestDataFactory class marked as testmethod and my code coverage was 65%; then I marked the methods that had no parameters with testmethod and code coverage on the org increased to 70%.

Anyone have any idea how to resolve this issue so that TestDataFactory methods are not counted against code coverage?

Best Answer

I've heard many people asking similar questions with the same issue.

This is a bug with the displayed code coverage but will not be factored into minimum coverage deployment calculations.

This is most likely caused by initially creating/saving your test factory class without the @isTest prefix. If you follow these instructions you can remove the incorrect data. Then when you rerun tests it should not reappear.