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
AbAb 

code coverage | test class | query + for

Hello,

I have written a test class, for code coverage.
the code coverage is not proper for a fucntion.
it accepts no paramenters.
but it stop inside for loop.
i am making query inside for loop and it doesnt not enter this loop,whe i execute the query it is passing

User-added image

Any suggestion
 
Best Answer chosen by Ab
Amit Chaudhary 8Amit Chaudhary 8
Hi Sandrine,

Testing Best Practices
  • At least 75% of your Apex code must be covered by unit tests, and all of those tests must complete successfully.
 
  • When deploying Apex to a production organization, each unit test in your organization namespace is executed by default.
  • Calls to System.debug are not counted as part of Apex code coverage.
  • Test methods and test classes are not counted as part of Apex code coverage.
  • While only 75% of your Apex code must be covered by tests, your focus shouldn't be on the percentage of code that is covered. Instead, you should make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single records. This should lead to 75% or more of your code being covered by unit tests.
  • Every trigger must have some test coverage.
  • All classes and triggers must compile successfully.
  •  

1) Always avoid to use SeeAllData = true
2) Create your test data in your test class. Never depend on your organization data
3) Always try to use assert in test classes
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Let us know if this will help you



 

All Answers

Srinivas SSrinivas S
Please verify that you have created test data for the accounts in your test class.
If no records are available, it won't enter into the for loop.

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
AbAb
i made a test in query editor and it gives me reqults
Alexander TsitsuraAlexander Tsitsura
Hi Sandrine,

Maybe you need to create test data(Accounts or others) inside test method.
 
static testMethod void test1() {
    // create test data
    Account[] accs = new Account[] {};
    for (int i=0; i<200; i++) {
        Account acc = new Account(Name = 'Test ' + i);
        accs.add(acc);
    }
    insert accs;

    // do logic
    Test.startTest();
    ...    
    Test.endTest();

    // verified logic: asserts
    ... 
}



Thanks, Alex
Alexander TsitsuraAlexander Tsitsura
Your test without SeeAllData=True is isolated and can not see real data, you need to create this data inside test method or in @testSetup method.
Rohit K SethiRohit K Sethi
Hi Sandrine,

In code you quering on account and before calling this function you need to create account in test class as "Alexander Tsitsura" define but created account fields values must be  matching with query.

Thanks.
Amit Chaudhary 8Amit Chaudhary 8
Hi Sandrine,

Testing Best Practices
  • At least 75% of your Apex code must be covered by unit tests, and all of those tests must complete successfully.
 
  • When deploying Apex to a production organization, each unit test in your organization namespace is executed by default.
  • Calls to System.debug are not counted as part of Apex code coverage.
  • Test methods and test classes are not counted as part of Apex code coverage.
  • While only 75% of your Apex code must be covered by tests, your focus shouldn't be on the percentage of code that is covered. Instead, you should make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single records. This should lead to 75% or more of your code being covered by unit tests.
  • Every trigger must have some test coverage.
  • All classes and triggers must compile successfully.
  •  

1) Always avoid to use SeeAllData = true
2) Create your test data in your test class. Never depend on your organization data
3) Always try to use assert in test classes
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Let us know if this will help you



 
This was selected as the best answer