function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RoyMooreRoyMoore 

Trailhead - Getting Started with Apex Unit Tests - TemperatureConverter example...

At the end of the TemperatureConverter example, it says...

"After you run tests, code coverage is automatically generated for the Apex classes and triggers in the org. You can check the code coverage percentage in the Tests tab of the Developer Console. In this example, the class you’ve just tested, the TemperatureConverter class, has 100% coverage as shown in this image."

And it shows an image of 100% 3/3.

However, my code coverage still says 0% & 0/3 even though I was able to successfully replicate the example and had a good TestRun

Any clues as to why that might be? 
Best Answer chosen by RoyMoore
James LoghryJames Loghry
Try the following:
  • Go to Setup->Develop->Apex Test Execution
  • Click the "View Test History" link (not button)
  • Click the "Clear Test History" button.
  • Re run the tests and view the code coverage.
Sometimes the code coverage calculation is buggy, and this helps fix it.

However, it looks like your code should be working fine.  Have you tried checking your Trailhead challenge to see if you passed?

All Answers

James LoghryJames Loghry
Roy,

Can you please post the test class for the TemperatureConverter example here?  While doing so, please use the code format (< >) button.  Thanks!
RoyMooreRoyMoore
I just replicated the next example of TaskUtil and the same thing happened.  The TestRun was successful, but the code coverage still says 0%.
RoyMooreRoyMoore
Here's the code, but it is identical (copied) from trailhead and everything rtruns as expected, my code coverage is just not changing.
 
public class TemperatureConverter {
    // Takes a Fahrenheit temperature and returns the Celsius equivalent.
    public static Decimal FahrenheitToCelsius(Decimal fh) {
        Decimal cs = (fh - 32) * 5/9;
        return cs.setScale(2);
    }
}
 
@isTest
private class TemperatureConverterTest {

    @isTest static void testWarmTemp() {
        Decimal celsius = TemperatureConverter.FahrenheitToCelsius(70);
        System.assertEquals(21.11,celsius);
    }
    
    @isTest static void testFreezingPoint() {
        Decimal celsius = TemperatureConverter.FahrenheitToCelsius(32);
        System.assertEquals(0,celsius);
    }

    @isTest static void testBoilingPoint() {
        Decimal celsius = TemperatureConverter.FahrenheitToCelsius(212);        
        System.assertEquals(100,celsius,'Boiling point temperature is not expected.');
    } 
    
    @isTest static void testNegativeTemp() {
        Decimal celsius = TemperatureConverter.FahrenheitToCelsius(-10);
        System.assertEquals(-23.33,celsius);
    }
      
}

 
Arun Deepan LJArun Deepan LJ
Hey,
The above code does cover the class, and test coverage will be 100%. Just click on compile test class and then click the estimate organization's code coverage. Hope it will show the exact code coverage
James LoghryJames Loghry
Try the following:
  • Go to Setup->Develop->Apex Test Execution
  • Click the "View Test History" link (not button)
  • Click the "Clear Test History" button.
  • Re run the tests and view the code coverage.
Sometimes the code coverage calculation is buggy, and this helps fix it.

However, it looks like your code should be working fine.  Have you tried checking your Trailhead challenge to see if you passed?
This was selected as the best answer
RoyMooreRoyMoore
Hey James,

I tried what you said, but still no love.

User-added image

As for the challenge, I haven't gotten there yet.  this is just the pre challenge educational stuff, but I would like to see it working as intended before moving on.

Roy
RoyMooreRoyMoore
Hey James,

I cleared the test data from the developer console and that did the trick.  Thanks for pointing me in the right direction.
James LoghryJames Loghry
Roy, 

There are a few different ways of running unit tests.  Looks like you're running them from the dev console currently, but you could also
  1. Open the test class (Setup->Develop->Apex Classes), and click the "Run Test" button.
  2. Install MavensMate (a Sublime Text 3 plugin that developers use) and use the Execute Apex Tests feature.
I would recommend trying one of those options to see if they report code coverage correctly.  Your test class looks good to me and it looks like you should receive 100% (or near) code coverage
Jeff DouglasJeff Douglas
Roy,

There's an issue with the develper console right now not updating the code coverage correctly when running individual tests. We are trying to get the instructions changed in our next release.

In the mean time, I think if you do Test -> Run All that should fix the issue.

Thanks
Jeff Douglas
Trailhead Developer Advocate
Jack D PondJack D Pond
This one stumped me too when using Developer Console since I had been testing successfully for years until I found this.
Winter '16 - In Developer Console, running tests synchronously does not generate code coverage (https://success.salesforce.com/issues_view?id=a1p30000000eMoyAAE)

Apparently there is a problem with Developer Console in Winter '16 failing to update the code coverage.  You can avoid this problem by going into developer console and enabling "Always Run Asynchronously".  Alternatively, you can (from settings)
"Develop->Apex Classes->TestVerifyDate->Run Test".

Then check coverage on the class ("VerifyDate").  Hope this helps, and select "Answer" if it solved your problem.

 Enable Asynchronous testing
Jess BurghJess Burgh
Help! I'm currently working on the example portion of this unti. And I saw a problem for line 5. "Unexpected token: 'return'. If someone could point me in the right direction, it would be appreciated. User-added image