3

I have not needed to make a test class yet. I have written triggers and a small class here or there.

My question is, at what point will I have to 'fess up and make a test class? Do I just wait until the deployment monster yells at me? What's the deal or criteria?

I recently needed to make a VF page and controller (which many of the competent people on this list have helped me with), it didn't make it to production yet. Will I have to do the test class thing or can I weasel out of it somehow?

8

I am dividing this answer into three section so please go through them and try to understand importance of every section.

Why Test:

Testing is key to the success of your application, particularly if your application is to be deployed to customers. If you validate that your application works as expected, that there are no unexpected behaviors, your customers are going to trust you more.

Take a simple examples to clear why testing is important.

Requirement : Write a vf page/controller which save the record and code redirect to detail page of that record.

Expected Output : After Save it should redirect to detail page.

So if you have developed such requirement there are two ways of testing such application.

  • One is through the Salesforce user interface, important, but merely testing through the user interface will not catch all of the use cases for your application.

  • Second Write test methods to ensure functionality and verify using System.assert methods.

What to test:

Salesforce recommends that you write tests for the following:

  • Single action

    Test to verify that a single record produces the correct, expected result.

  • Bulk actions

    Any Apex code, whether a trigger, a class or an extension, may be invoked for 1 to 200 records. You must test not only the single record case, but the bulk cases as well.

  • Positive behavior

    Test to verify that the expected behavior occurs through every expected permutation, that is, that the user filled out everything correctly and did not go past the limits.

  • Negative behavior

    There are likely limits to your applications, such as not being able to add a future date, not being able to specify a negative amount, and so on. You must test for the negative case and verify that the error messages are correctly produced as well as for the positive, within the limits cases.

  • Restricted user

    Test whether a user with restricted access to the sObjects used in your code sees the expected behavior. That is, whether they can run the code or receive error messages.

When to write test cases:

In software development life cycle testing comes into the picture when we complete the development. It can be either fresh development or extended development. That is the right time to write test cases for your functionality and prior to delivering your functionality for your QA team (If you have) to test, reason being if you have tested your functionality with test methods prior to QA you are minimizing defects.

Code coverage: Salesforce enforce 75% code coverage limit but if you have written proper test cases (positiv/negative/bulk) there is no need to worry about the code coverage you will automatically cover and code will be ready to deploy without any defects.

Some reference to get started :

Getting started with Apex Test

Testing best practices

Testing Example

3

To add to @Himanshu overall excellent answer, I'd like to expand on what he said about "When to write test cases". When he spoke of "software development life cycle testing" It's my sense (and hope) that he was referring to what we call Test Driven Development. This is where we design tests for the functionality we want our code to meet as he described in "What to test". However, we do this during the development stage both prior to and as we're writing our code.

This allows us to verify that our code is meeting our functional requirements as we develop it. It's something that can be agile driven since additional test methods can easily be added as design requirements change. As an added bonus, when our code is finished, we should have a test class that's completed and ready to go. This is my preferred way to write and develop new code.

2

The principle is that you should always create tests that verify the behavior of any code you create. Every time you create or modify a trigger or a class, you should ensure there are apex tests that verify the behavior. A test is your code's best friend ;)

  • Thanks but how come I can 'get away with it' without writing test code? I thought that it was mandatory but I see that, so far, I haven't been forced to do so. What's the minimum requirement? – Apex N-u-b Mar 27 '16 at 8:40
  • @ApexN-u-b Salesforce only checks that your code is covered when you deploy into a production environment. If I recall, you can manually set an option somewhere to have tests run when deploying between sandboxes. The minimum requirements are 75% of each Apex class must be covered (test classes are exempt), and more than 0% of each trigger. Additionally, the overall coverage of your Apex + triggers must be at least 75%. (This is covered by the Testing Best Practices link in Himanshu's answer) – Derek F Mar 27 '16 at 12:51
  • @DerekF - 75% is not mandatory for classes. The mandatory part is 75% overall coverage. Deploying classes without test coverage will reduce overall coverage and at some point making it impossible to deploy anything to the server. Honestly if I had a developer that did this, they would be terminated immediately. – Eric Mar 27 '16 at 14:48
  • @Eric That's what I get for reading too fast, I suppose. I inherited an org close to that 75% overall threshold, so that's probably how I conflated my imaginary individual class coverage requirement with the overall coverage requirement. – Derek F Mar 27 '16 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.