Unit Testing
Unit Testing
Testing Objectives
Tests intended to find errors Errors should be found quickly Good test cases have high p for finding a yet undiscovered error Successful tests cause program failure, i.e. find an undiscovered error. Tests should be mutually exclusive and exhaustive Minimal set of test cases needs to be developed because exhaustive testing not possible
Unit Testing
A unit is typically a method/class/a cluster Testing done to each unit, in isolation, to verify its behavior Typically the unit test will establish some sort of artificial environment and then invoke methods in the unit being tested It then checks the results returned against some known value When the units are assembled we can use the same tests to test the system as a whole
JUnit was written by Erich Gamma (of Design Patterns fame) and Kent Beck (creator of XP methodology) JUnit uses Javas reflection capabilities (Java programs can examine their own code) JUnit features includes: Test fixtures for sharing common test data Assertions for testing expected results Test suites for easily organizing and running tests Graphical and textual test runners JUnit is not yet included in Suns SDK, but an increasing number of IDEs include it BlueJ, JBuilder, Eclipse, DrJava etc.now provide JUnit support
publicComplexTest() { }
This is the default constructor
public class Complex { int real_part; int imaginary_part; public Complex(int r, int i) { real_part=r; Example: imaginary_part=i; } Class public Complex() { real_part=0; imaginary_part=0; } public boolean Equal(Complex c) { boolean result = false; if ((real_part==c.get_r()) && (imaginary_part==c.get_i())) result=true; return result; } public Complex Add(Complex c) { Complex result = new Complex(c.get_r()+real_part,c.get_i()+imaginary_part); return result; } public int get_r() { return real_part;} public int get_i() { return imaginary_part; } }
Complex
This means you dont have to worry about the order in which the tests are run
Explicit
- use of a public static method of TestCase class named suite()
Using Junit
Download latest version (3.8.1) from: www.junit.org Setup classpath to junit.jar Write TestCase classes and organize them (Import junit.framework.*) Include appropriate JUnit TestRunner in main()
Complex Class:
revisited
public static void main(String[] argv) { Complex c1 = new Complex(7,3); Complex c2 = new Complex(12,6); Complex result = c1.Add(new Complex(5,3)); if (result.get_r()==c2.get_r() && result.get_i()==c2.get_i()) System.out.println(Addition test Passed); else
System.out.println(Addition test Failed); if (!c2.Equal(c1)) System.out.println(Equality test I Passed); else System.out.println(Equality test I Failed);
import junit.framework.TestCase; public class MyThirdTestClass extends TestCase { public MyThirdTestClass(String name) { super(name); } public void test1() { ... code here ... } public void test2() { ... code here ... } }
To test methods that change the state of the object, you have to have code that checks the state
Its a good idea in any case to write self-tests for object validity
Conclusions
Unit Testing is the place to identify and remove errors at the earliest Effective Automated tests can be written to test most of the units, if not all Unit Testing Frameworks (UTF), like JUnit are quite useful in writing, executing and organizing unit tests for incremental code development and change management.
JUnit in Eclipse
To create a test class, select File New Other... Java, JUnit, TestCase and enter the name of the class you will test
Fill this in
Running JUnit
Second, use this pulldown menu First, select a Test class
Results
Your results are here