3

I have written a batch class, AbcBatch. I created a test class AbcBatchTest to cover the class 100%:

static testMethod void shouldNotDoXyz() {

    // setup code
    ...

    // call the batch class
    Test.startTest();
    AbcBatch b = new AbcBatch ();
    Database.executeBatch(b);   
    Test.stopTest();

    // asserts
    ...
}

When I run the tests in the Developer Console, the tests pass, but reports 0% code coverage for the Batch Class. I also verified "Store Only Aggregated Code Coverage" is unchecked under Test Execution Options.

I ran the "Clear Test Data" command and performed a run of all tests via Tests -> Run All, but still the problem persists.

Here's the code coverage button in Developer Console for the class:

enter image description here

I have checked "Disable Parallel Apex Testing" under Test Execution Options as some of my tests won't pass with this option checked. However, I did run my tests with parallel testing enabled to see if that solved the problem, however it did not.

I ran the tests from the /ui/setup/apex/ApexTestQueuePage as well, however I get the same result.

I also cleared test data, and Run All tests forcing asynchronous mode but still the problem persists.

enter image description here

What could be wrong?

7
  • 1
    Did you check Setup > Apex > Apex Test Execution > Options... to make sure that "Store Only Aggregated Code Coverage" is unchecked? Did you clear the test data and then use run all tests?
    – sfdcfox
    Commented Nov 14, 2016 at 18:45
  • 1
    Are you using the "Always Run Asynchronously" option update the test menu in the developer console? If not, try checking it and running the test again. Or try running the tests from /ui/setup/apex/ApexTestQueuePage Commented Nov 14, 2016 at 19:03
  • Have you actually clicked the dropdown? Does it not show any of the classes or methods that should provide coverage?
    – Adrian Larson
    Commented Nov 14, 2016 at 19:15
  • Added picture showing dropdown per Andrian's request. Commented Nov 14, 2016 at 19:18
  • If you add assertions that fail to the last part of you batch class do they fail as expected? E.g. at the end of the execute method add System.assert(!Test.isRunningTest(), 'Expected Boom!'); Commented Nov 14, 2016 at 19:34

2 Answers 2

2

Historically there were two ways to run test cases - Synchronous and Asynchronous. Only the async would actually track the coverage metrics.

In Winter '16 APIs and options were reintroduced to run test in synchronous mode from the Developer Console. This generally provided faster test execution for single test cases. There was a corresponding known issue that the synchronous test runs wouldn't provide coverage - Winter '16 - In Developer Console, running tests synchronously does not generate code coverage.

It might be that this has regressed. I'd suggest explicitly running the tests asynchronosly. You can do this either with the Always Run Asynchronously option under the Test menu in the developer console or via the older UI at /ui/setup/apex/ApexTestQueuePage.

2
  • Thanks Daniel. However, running tests asynchronously still does not update code coverage for my class. Commented Nov 14, 2016 at 19:57
  • 1
    @SwisherSweet OK, it was worth a try. If you add an assertion that deliberately fails to the end of the batch execute method does it occur? What API versions are on your test and batch class? What does the debug log show when the test is running? Commented Nov 14, 2016 at 20:00
0

As it turns out, the batch class reporting 0% code coverage did not pass its tests in the environment reporting 0% code coverage. I got confused because it had been passing in a different org.

Once I corrected the error causing the test failure, cleared test data, and rerun all tests, the class show reports accurate code coverage percentage.

I went a step further and reverted the test class to the point it was failing again, cleared test data, and ran all tests again. To my surprise, it still reported accurate test coverage.

My current working configuration is to run tests synchronously (not asynchronously), and parallel tests are disabled.

It seems that Salesforce might only calculate code coverage for classes with passing tests.

Thank you all for your help in troubleshooting this problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .