What is Unit Testing ?
When to use Unit Testing ?
How to write Unit Test ?
The primary goal of unit testing is to take the
smallest piece of testable software in the application,
isolate it from the remainder of the code, and
determine whether it behaves exactly as you expect.
Good Definition for Unit Test- (define later)
Unit Test is an automated piece of code that invokes
the method or class being tested and then check
some assumption about the logical behavior of that
Method/Class. Unit Test is almost always written
using a Unit Testing Framework. It can be written
easily & runs quickly.
It is fully automated, trustworthy, readable and
Assume we have a
Addition class that
we’d like to test.
We can now create a
simple test class which
will call this method.
operation = new
value = 7 should be
expected for a
public class MathOperation
public int Addition(int a, int b)
if(a==null || b==null)
1 . It helps you write better Code.
In addition to finding errors in your code, unit testing can help
you during the initial development of your class or module's
Unit tests force you to think about how another developer will
want to use your code while you're writing that code.
2. Writing test cases will save you time later.
3. Unit tests provide immediate feedback on your code.
You don't have to wait until after code in a separate part of the
application is written before you can test and know whether your
4. Unit Tests should be written at each stage of development of
so that continuous check of your code is maintained and saves
you efforts during the final release of the software.
The Tests should be Through.
1. You should only be satisfied with your unit
you’re confident that your unit tests thoroughly
your code works as we expect it to work.
2. Unit tests should exercise the expected as well
unexpected conditions in your code.
3. Special attention should be paid to areas of
code that are
particularly likely to break.
The Test should be Repeatable.
1. Every one of your unit tests should be able to be
run repeatedly and continue to produce the same
results, regardless of the environment in which the
tests are being run.
2. Your unit tests should not rely on hard-coded
3. Unit tests must be written in either a sandbox org
or in a developer org.
The Test should be Independent
1. Your unit tests should only test one aspect of
your code at a time, so that it is easy to understand
the purpose of each unit test. Properly written, and
well-decomposed unit tests are an excellent source
2. Each Test should be independent of the other