Testing is a key and critical component to successful long term software development process. Salesforce.com strongly recommends using a test-driven development process which occurs at the same time as code development. Salesforce has very strong set of documentation. When I was learning salesforce unit testing, I realize that it is difficult to understand where to start read. Therefore, I summarized the unit testing for salesforce beginners to understand the basic aspects of unit testing.
There are few things to consider before you deploy or upload the code or package;
There are few things to consider before you deploy or upload the code or package;
- 75% of your Apex code must be covered by unit tests
- All the tests must complete successfully
- Every trigger has some test coverage (1%)
- All classes and triggers must compile successfully
When you are writing a test class, you have to write test for Single Action, Bulk Action, Positive Behavior, Negative Behavior, and Restricted User.
- Single Action :Test to verify that a single record produces the correct, expected result.
- Bulk Action : Test not only the single record case, but the bulk cases as well
- Positive Behavior : Verify that the expected behavior occurs through every expected permutation
- Negative Behavior : Verify that the error messages are correctly produced
- Restricted User :Test whether a user with restricted access to the sObjects used in your code sees the expected behavior
When you create a test method,
- Use static
- Use testMethod keyword
- Use void return type
- No any arguments
- No data changes performed in a test method
- Don't send emails
- Cannot be used to test Web service callout because web services are asynchronous and tests are synchronous.
- The API for asynchronous test runs is a Beta release (Winter '12)
- For More : Force.com Apex code Developer's Guide Page 153
The key methods to use in your unit tests are the system.assert() methods. There are three types of system.assert() methods.
- System.assert(condition)
- System.assertEquals(x,y)
- System.assertNotEquals(x,y)
For the security review, every test method must have at least one system.assert() method. We need use assert methods not only for the pass the security review but also as a best practice. It will be help us to keep track the failures of Apex classes or triggers.
Before you write test classes, understand the best practices. There is a good post in following link to understand the best Practice for Writing Test Classes http://www.gokubi.com/archives/testing-best-practices
References,
http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
http://gokubi.com/archives/testing-best-practices
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm
http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm
References,
http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
http://gokubi.com/archives/testing-best-practices
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm
http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm
No comments:
Post a Comment