Unit Testing Slide 1

advertisement
Unit Testing
Slide 1
Unit Testing Options
Use N-Unit
l
•
Using J-Unit
l
•
l
In a microsoft environment .NET… you can use
their supplied N-Unit testing to test your classes
A free download and integratable with most IDE’s
is J-Unit which you can use with Java for unit
testing. There are some other unit testing
programs for C++, Smalltalk, Eiffel, Ada, etc…
They do cost.
Writing your own Unit Testing
Slide 2
Unit Testing Options
l
l
Write your own unit testing program.
This allows you to understand unit testing
better.
Slide 3
Creating a Unit Test Class
Step 1 Create a Unit Test Class
Given the class you wish to test
YourClassName.
Build a class called
YourClassNameUnitTest that is a sub-class of
YourClassName
Your class
Unit Test
class
Slide 4
Creating a Unit Test Class HOW
Step 1 – Create Unit Test Class -- HOW
Simply copy your class into another class called
YourClassNameUnitTest
Make it a a sub-class of YourClassName
Your class
Unit Test
class
In the constructor (s)
remove the code and
call the super-class constructor method
In the other methods – rewrite them in the form shown in step 2
Slide 5
Rewriting Methods
Step 2 – Rewriting Methods
For all methods that are void--- simply call the super method OR
eliminate them if they are not needed for other methods.
These void methods should not be changing any
class variables but simply doing things that do not
change the state of the object.
For all methods that return a value.
Set up test(s) to assure the correct value is returned.
Slide 6
Rewriting Methods Example
Your class
Unit Test
class
Method addit (int a, int b):int
return a + b;
Method addit (int a, int b):int
int a = 4;
int b = 5;
int answer = super (a,b);
if answer <> 9
print Error in addit;
print expected, “9”;
print received, answer;
else
print OK;
Slide 7
Creating a Unit Test Executable
Step 3 – Create a Unit Test Executable Class
Your class
Unit test
EXEC class
Unit test
class
Slide 8
Creating a Unit Test Executable
Step 3 – Create a Unit Test Executable – How
Decide what methods you wish to test.
Remember– you will use this for the entire duration
of this class …it may change over the years but
this test can be run to make sure that changes to this
c;ass or other classes in the system do not adversely
impact this class…..
It helps to keep down the ripple effect of
changes
Slide 9
Creating a Unit Test Executable
Step 3 – Create a Unit Test Executable – How
In the main executable routine
Make an instance of the Unit Test Class
Call each method in the Unit Test Class
that you wish to test
Remember, each unit test method may execute many
different tests with different parameters.
Slide 10
Download