TESTING Acknowledgement Original slides by Jory Denny 1
TESTING Acknowledgement: Original slides by Jory Denny 1
DEVELOPMENT (ONE OUT OF MANY PERSPECTIVES) 1. Solve 2. Implement 1. Write test 2. Write code 3. Repeat 3. Integrate 4. Release 2
TEST DRIVEN DEVELOPMENT (TDD) 1. Write a test (or multiple tests) • Note test should fail first! Refactor 2. Write the code • Clean the code • Write more tests • Get the test to pass 3
4
PRACTICAL EXAMPLE • Lets practice some TDD on the following example You are tasked with implementing a feature for a company that determines the total amount of time a worker spends working in a week (for his salary for example). He says the number of hours worked each day is already being measured and is stored in an internal array in 5 the code base.
PRACTICAL EXAMPLE • How do we solve this? • Compute the sum! 6
PRACTICAL EXAMPLE • First we write a test public static double sum(double[] arr) { return Double. POSITIVE_INFINITY; //note this clearly does not work and is thus failing } public static void main() { double[] arr = {0, 1, 1, 2, 3, 5, 8}; if(sum(arr) != 20) System. out. println("Test failed? !? !? ! I suck!"); //you don't, its supposed to fail! } 7
PRACTICAL EXAMPLE • Before we continue, lets review • Positives • • Scaffolding, function interface, and test all implemented We know it is good design Tests to tell if the code is correct, before we struggle with debugging many lines of code Negatives • Code isn’t written until later…. . but is that really that bad? NO • In fact, with TDD you code FASTER and more EFFECTIVELY than without it 8
PRACTICAL EXAMPLE • Now the code – and then run the test! public static double sum(double[] arr) { double s = 0; for(double x : arr) s += x; return s; } 9
THINGS TO REMEMBER • Always have code that compiles • Test writing is an art that takes practice (and more learning!) • Compile and test often! 10
TESTING FRAMEWORKS • Many frameworks exist Cpp. Unit, JUnit, etc. • We will be using JUnit • A unit test is a check of one behavior of one “unit” (e. g. , function) of your code 11
TESTING FRAMEWORKS • SETT – unit testing paradigm • Setup – create data for input and predetermine the output • Execute – call the function in question • Test – analyze correctness and determine true/false for test • Teardown – cleanup any data 12
UNIT TEST EXAMPLE public static boolean test. Sum() { //setup double[] arr = {0, 1, 1, 2, 3, 5, 8}; double ans = 20; //execute double s = sum(arr); //test return s == ans; } 13
TDD - EXERCISE • Write a Java function to find the median of an array of integers • Do test driven development, starting with a good unit test(s) • After test is created and checked, code the function 14
TDD - EXERCISE • [Median] In a group of n elements, the median is the middle element, such that the number of elements less than or equal the median is equal to the number of elements larger than or equal the median. • If n is odd, we take the middle element. • If n is even, the median is defined as the average of the two middle elements. • For example, the median of the sequence 5} is 2 {1, 1, 2, 3, 15
TDD - EXERCISE public static boolean test. Median 1() { //setup double[] arr = {1, 1, 2, 3, 5}; double ans = 2; //execute double s = median(arr); //test return s == ans; } 16
JUNIT BASICS JUnit is the de facto framework for testing Java programs. JUnit is a third-party open source library packed in a jar file. The jar file contains a tool called test runner, which is used to run test programs. Suppose you have a class named A. To test this class, you write a test class named ATest. This test class, called a test class, contains the methods you write for testing class A. The test runner executes ATest to generate a test report, as shown in Figure 44. 1. 17
A JUNIT TEST CLASS To use JUnit, create a test class. By convention, if the class to be tested is named A, the test class should be named ATest. A simple template of a test class may look like this: package mytest; import org. junit. *; import static org. junit. Assert. *; public class ATest { @Test public void m 1() { // Write a test method } @Test public void m 2() { // Write another test method } @Before public void set. Up() throws Exception { // Common objects used by test methods may be set up here } } 18
RUN THE TEST To run the test from the console, use the following command: java org. junit. runner. JUnit. Core mytest. ATest 19
TEST ARRAYLIST Listing 44. 1 is an example of a test class for testing java. util. Array. List. @Before public void set. Up() throws Exception { } @Test public void test. Insertion() { list. add("Beijing"); assert. Equals("Beijing", list. get(0)); list. add("Shanghai"); list. add("Hongkong"); assert. Equals("Hongkong", list. get(list. size() - 1)); } @Test public void test. Deletion() { list. clear(); assert. True(list. is. Empty()); list. add("A"); list. add("B"); list. add("C"); list. remove("B"); assert. Equals(2, list. size()); } } 20
- Slides: 20