120 likes | 332 Views
BlueJ Tester. By Pepper. What is Tester?. Tester makes it easy for you to run test cases through your method that returns a value. You code the test cases You run the one test It reports all failures with expected vs actual. Why use tester. Unit testing
E N D
BlueJ Tester By Pepper
What is Tester? • Tester makes it easy for you to run test cases through your method that returns a value. • You code the test cases • You run the one test • It reports all failures with expected vs actual
Why use tester • Unit testing • When you can count on a function working, it is makes debugging the main program easier • It is easier to fix a smaller amount of code • Regression testing • Guard against changes breaking code • Alternatives: • Call method frequently typing in values (if value can be entered through BlueJ interface) • Code tests in main method (and verify results and print notices for failure)
Test a Static Method’s Return • Right click on class box – create test class • Double click green unit test box to edit • Remove extends junit.framework.TestCase import tester.*; public class MyPgmTest {
Test a Static Method’s Return • Fill in testSomething method • 3 parts: • Command : t.checkExpect • Method call: MyPgm.addIt(3) • Method return: 6 • t.checkExpect( MyPgm.addIt(3) , 6 ); • Make many tests • t.checkExpect(MyPgm.addIt(3),6); • t.checkExpect(MyPgm.addIt(5),5);
Complete Test Program import tester.*; public class MyPgmTest { public static void testEverything () { Tester.run (new MyPgmTest()); } public void testSomething (Tester t) { t.checkExpect (MyPgm.addIt(2),5); t.checkExpect (MyPgm.addIt(2),4); } }
Run Tests • Right click on test block and compile • Right click on test block and run testEverything() • See the results of each test in the terminal window • Note: You can add other methods if you like
Testing Double Value Matching • How to represent 1.1? • The problem: between 1/8 and 1/16 • 1.10011001100110011001101 × 2-4 • .000110011001100110011001101
More information for doubles • More on float and doubles: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html • Explains how values are held • Explains rounding rules • No need to learn how, just know to expect it slightly off with tolerance .001
Inexact - Checking Doubles • Check doubles by only insisting on .001 tolerance. • 4 parts: • Command : t.checkInexact • Method call: MyPgm.addIt(3) • Method return: 6 • Tolerance: .001 • t.checkInexact( MyPgm.addIt(3) , 6, .001 );
Summary • Use tester for unit and regression tests of methods with return values • Doubles (and float) are inexact storage of fractions, so expect tolerance needed • How to set up tests for exact and inexact • How to run tests • How to see results