2. What is Unit Testing ?
Example.
When to use Unit Testing ?
How to write Unit Test ?
FrameWorks Examples.
3. 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
maintainable.
4. 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.
MathOperation
operation = new
MathOperation();
operation.Addition(3,4)
value = 7 should be
expected for a
successful Test.
public class MathOperation
{
public int Addition(int a, int b)
{
if(a==null || b==null)
{
System.Out.Println(“Null value
occured”)
return -1;
}
else
{
return a+b;
}
}
}
5. 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
public interface.
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
code works.
4. Unit Tests should be written at each stage of development of
your code.
so that continuous check of your code is maintained and saves
you efforts during the final release of the software.
6. The Tests should be Through.
1. You should only be satisfied with your unit
tests when
you’re confident that your unit tests thoroughly
verify that
your code works as we expect it to work.
2. Unit tests should exercise the expected as well
as the
unexpected conditions in your code.
3. Special attention should be paid to areas of
code that are
particularly likely to break.
7. 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
server urls.
3. Unit tests must be written in either a sandbox org
or in a developer org.
8. 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
of documentation.
2. Each Test should be independent of the other
Test.