+ Start a Discussion

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.

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/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.
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;
  		con.AccountId=ac.Id; //--> Creating LookUp of Account in Contact 
  	insert conlist;
  	System.debug(conlist); // -->Test class does not count 'debug' line

Test Class code for above Apex Code:
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/

If this answers your query please mark this question as a solved so that it can be filtered out from  unsolved questions.
      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/)