Presentation is loading. Please wait.

Presentation is loading. Please wait.

Testing Acknowledgement: Original slides by Jory Denny.

Similar presentations


Presentation on theme: "Testing Acknowledgement: Original slides by Jory Denny."— Presentation transcript:

1 testing Acknowledgement: Original slides by Jory Denny

2 Development (one out of many perspectives)
Solve Implement Write test Write code Repeat Integrate Release

3 Test Driven Development (TDD)
1. Write a test (or multiple tests) Note test should fail first! 2. Write the code Get the test to pass Refactor Clean the code Write more tests

4

5 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 the code base.

6 Practical Example How do we solve this? Compute the sum!

7 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!

8 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

9 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; }

10 Things to remember Always have code that compiles
Test writing is an art that takes practice (and more learning!) Compile and test often!

11 Testing Frameworks Many frameworks exist CppUnit, 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

12 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

13 Unit test example public static boolean testSum() { //setup double[] arr = {0, 1, 1, 2, 3, 5, 8}; double ans = 20; //execute double s = sum(arr); //test return s == ans; }

14 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

15 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 {1, 1, 2, 3, 5} is 2 and the median of the sequence {1, 1, 2, 3, 5, 8} is 2.5

16 TDD - Exercise public static boolean testMedian1() { //setup double[] arr = {1, 1, 2, 3, 5}; double ans = 2; //execute double s = median(arr); //test return s == ans; }

17 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.

18 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 m1() { // Write a test method } public void m2() { // Write another test method @Before public void setUp() throws Exception { // Common objects used by test methods may be set up here

19 Run the Test To run the test from the console, use the following command: java org.junit.runner.JUnitCore mytest.ATest

20 Test ArrayList Listing 44.1 is an example of a test class for testing java.util.ArrayList. @Before public void setUp() throws Exception { } @Test public void testInsertion() { list.add("Beijing"); assertEquals("Beijing", list.get(0)); list.add("Shanghai"); list.add("Hongkong"); assertEquals("Hongkong", list.get(list.size() - 1)); public void testDeletion() { list.clear(); assertTrue(list.isEmpty()); list.add("A"); list.add("B"); list.add("C"); list.remove("B"); assertEquals(2, list.size());


Download ppt "Testing Acknowledgement: Original slides by Jory Denny."

Similar presentations


Ads by Google