100 likes | 230 Views
Unit Testing. Unit Testing Options. Use N-Unit In a microsoft environment .NET… you can use their supplied N-Unit testing to test your classes Using J-Unit
E N D
Unit Testing Options • Use N-Unit • In a microsoft environment .NET… you can use their supplied N-Unit testing to test your classes • Using J-Unit • 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
Unit Testing Options • Write your own unit testing program. • This allows you to understand unit testing better.
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
Creating a Unit Test Class HOW Your class Step 1 – Create Unit Test Class -- HOW Simply copy your class into another class called YourClassNameUnitTest Make it a a sub-class of YourClassName 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 Unit Test class
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.
Rewriting Methods Example Your class Method addit (int a, int b):int return a + b; Unit Test class 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;
Creating a Unit Test Executable Step 3 – Create a Unit Test Executable Class Your class Unit test EXEC class Unit test class
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
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.