+ Start a Discussion
Ramssf70Ramssf70 

give me some tips for writing a test class easily

Hi Folks,

Give me some tips for writing test class easily and give me some basic exmaples.

Regards
Ramakrishna
DeepthiDeepthi (Salesforce Developers) 
Hi Ramakrishna,
A test class simply refers to the input required to confirm if instructions and methods used in developing an application or program are working correctly in delivering the desired output. 
Important considerations:
  1. Use the @isTest annotation.
    The test class starts its execution from the "testMethod". 
  2. Cover as many lines as possible. 
  3. At least 75% of your Apex code must be covered by unit tests, and all of those tests must complete successfully.
  4. Use System.assert methods to prove that code behaves properly.
  • Set up test data:
    Create the necessary data in test classes, so the tests do not have to rely on data in a particular organization.
    Create all test data before calling the Test.startTest method.
    Since tests don't commit, you won't need to delete any data.
    Write comments stating not only what is supposed to be tested, but the assumptions the tester made about the data, the expected outcome, and so on.
    Test the classes in your application individually. Never test your entire application in a single test.
Please follow the link for examples and more information on Test classes:
https://developer.salesforce.com/forums/?id=906F0000000BSo5IAG
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm   (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm  )
http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/  (http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/ )

Hope this helps you! Mark this post as Best Answer if it addresses your requirement.
Thanks,
Deepthi
Akshay_DhimanAkshay_Dhiman
Hi  Ramssf70,

I hope this code will help you lot to understand and write test class.

About Test Class:

The Apex testing framework enables you to write and execute tests for your Apex classes and triggers on the Force.com platform. Apex unit tests ensure high quality for your Apex code and let you meet requirements for deploying Apex.

Apex Code:
public with sharing class ContactOneCheck
 {
  public static void checkContact()
  {
  		list<account> aclist= new list<account>();
  		Integer n=0;
	aclist=[select Id, Name, NumberOfEmployees from account limit 10]; // --->Query to get 10 Records of Account from Org
  		list<contact> conlist= new list<contact>();
  		for(account ac:aclist)
{
  		contact con=new contact();
  		con.LastName='Ajay '+n;
  		n++;
  		con.AccountId=ac.Id; //--> Creating LookUp of Account in Contact 
  		conlist.add(con); 
  		}
  	if(conlist.size()>0)
  	insert conlist;
  	System.debug(conlist); // -->Test class does not count 'debug' line
  }   
}

Test Class code for above Apex Code:
@isTest 
public with sharing class ContactOneCheckTestClass
{
    @isTest static void testMethodCaseRecord()
      {
    	Account acc = new Account(); // Creating a  New Account for Test
    	acc.Name = 'Guru Vemuru';
    	acc.NumberOfEmployees = 5;
     	insert acc;
    	system.assert(true,'Account is Inserted');
       	Integer n=0;
   		test.startTest(); //--> Predefined Method for Test Class to Start the Test Execution of Apex Code
    	ContactOneCheck.checkContact(); //--> Calling the Class and their Method for Test.
    	test.stopTest(); //----> Predifined Method to Stop Test Execution 
    }
}

Here the desired output :-
User-added image

Points to remember for Test Classes:-
  • Test class always Start with @isTest.
  • In test class we have to create all the data which getting from query in Apex Code. (For Example : Account Object with its following records Id,Name,NumberOfEmployees).
  • test.startTest() and test.stopTest() must be used in Test Class.

For More Information go through these links Reference:​
  • https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
  • https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
  • http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
  • http://webkul.com/blog/test-classes-in-apex-salesforce/

Regard,
Akshay.
If this answers your query please mark this question as a solved so that it can be filtered out from  unsolved questions.
     
      NILESH RAJ 20NILESH RAJ 20
      Each test class follows a similar pattern:
      Test class in Salesforce starts with @isTest annotation which shows that this particular apex class is a test class.
      Setup of test data. This includes the creation of any data needed by your class. 
      Starting the test by calling Test.startTest() to reset the governor limits.
      Calling your class/method
      Stopping the test by calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish.
      Asserting that your changes have worked
      ​If you have made any change o the record, you need to query for the updates
      Run System.assert, System.assertNotEquals,System.assertEquals to verify that you got the correct data.

      You can check more details test class in salesforce (https://sfdcwisdom.com/test-class-in-salesforce/)
      Suraj Tripathi 47Suraj Tripathi 47
      Hi Ramssf70,

      Test class starts with @isTest annotation.
      Test.startTest() and Test.stopTest() must be used in Test Class.
      Use System. assert methods to check code work properly or not.
      At least 75% of your Apex code must be covered by unit tests.

      for more information go through this link:
      https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_test.htm

      If you find your Solution then mark this as the best answer. 

      Thank you!

      Regards 
      Suraj Tripathi
      Dixie LaneDixie Lane
      We all know that writing tests can be a really tiring and tedious task. It can take hours to write one test, but what if there was an easier https://www.edugeeksclub.com/ way? What if you could automate this process so you don't have to manually do it every time? Well now there is! This blog post will give you some tips on how to write your own test class easily.