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
ADGADG 

Overall Code Coverage is 0%

I have the following trigger:
trigger HelloWorldTrigger on Book__c (before insert) {
    Book__c[] books = Trigger.new;
    MyHelloWorld.applyDiscount(books);
}
which calls the function in the follwoing class:
public class MyHelloWorld {
    public static void applyDiscount(Book__c[] books) {
        for (Book__c b: books) {
            b.Price__c *= 0.9;
        }
    }
}
but when I am running a test script:
@isTest
private class HelloWorldTestClass {
    static testMethod void validateHelloWorld() {
        Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100);
        System.debug('Price before inserting new book: ' + b.Price__c);
        // Insert book
        insert b;
        // Retrieve the new book
        b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id];
        System.debug('Price after trigger fired: ' + b.Price__c);
        // Test that the trigger correctly updated the price
        System.assertEquals(90, b.Price__c);
    }
}
its giving me a 0% code coverage
User-added image

Can anyone please help me with this?
Best Answer chosen by ADG
bob_buzzardbob_buzzard
I assume you are running your tests through the developer console. If you are, and you haven't checked the box for 'Always Run Asynchronously', there is (yet another) bug in the code coverage calculation that will report zero coverage:

https://success.salesforce.com/issues_view?id=a1p30000000eMoyAAE

Code coverage is something that Salesforce seem to have an enormous amount of difficultly keeping stable, so its always worth checking the known issues.

All Answers

bob_buzzardbob_buzzard
I assume you are running your tests through the developer console. If you are, and you haven't checked the box for 'Always Run Asynchronously', there is (yet another) bug in the code coverage calculation that will report zero coverage:

https://success.salesforce.com/issues_view?id=a1p30000000eMoyAAE

Code coverage is something that Salesforce seem to have an enormous amount of difficultly keeping stable, so its always worth checking the known issues.
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please try to execute your test class from salesforce UI. Open class in salesforce UI and then click on RunTest button then check your code coverage . I hope that will help you

Thanks
Amit Chaudhary
ADGADG
Thanks!! I checked the 'Always Run Asynchronously' and now the coverage is being displayed correctly.
Abhishek_DEOAbhishek_DEO
They are enhancing the developer console in every release and enhancement always introduced bugs with them
bob_buzzardbob_buzzard
This isn't the developer console, this is when running unit tests synchronously so would be the same for any other client. The UI always runs asychronously which is why it works there. 
Abhishek_DEOAbhishek_DEO

Thanks Bob!!

it is clear. My point was that due to continous enhancement, developer console does not work properly. Sometimes code did not save, it hangs ....lots of other issues

Jamie 5mithJamie 5mith
For future people who may have this issue as I did - MavensMate has a great UI test runner that worked for me when DevConsole didn't seem to. From Sublime text's top menu > MavensMate > Unit Testing > has options to open the UI, run tests or quickly show org wide test coverage.
B_BajajB_Bajaj
Hi,

I am still facing the issue. I can see few classes are getting failed and others are running fine still coverage is shown as 0%. Any help will be appreciated. Is there any other reason for this issue?