[SalesForce] Code Coverage reports 0% when 100% of the class is covered with tests

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?

Best Answer

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.

Related Topic