Wednesday 29 August 2012

Apex Unit Tests


Writing unit tests for your code is fundamental to developing Apex code. You must have 75% test coverage to be able to deploy your Apex code to your production organization. In addition, the tests counted as part of the test coverage must pass. Testing is key to ensuring the quality of your application. Furthermore, having a set of tests that you can rerun in the future if you have to make changes to your code allows you to catch any potential regressions to the existing code.
Note
This test coverage requirement also applies for creating a package of your application and publishing it on the Force.com​ AppExchange. When performing service upgrades, Salesforce executes Apex unit tests of all organizations to ensure quality and that no existing behavior has been altered for customers.

Test Data Isolation and Transient Nature

By default, Apex test methods don’t have access to pre-existing data in the organization. You must create your own test data for each test method. In this way, tests won’t depend on organization data and won’t fail because of missing data when the data it depends on no longer exists.
Test data isn’t committed to the database and is rolled back when the test execution completes. This means that you don’t have to delete the data that is created in the tests. When the test finishes execution, the data created during test execution won’t be persisted in the organization and won’t be available.
You can create test data either in your test method or you can write utility test classes containing methods for test data creation that can be called by other tests.
There are some objects that tests can still access in the organization. They’re metadata objects and objects used to manage your organization, such as User or Profile.

No comments:

Post a Comment