1 csc 216 lecture 3. 2 unit testing the most basic kind of testing is called unit testing why is...

25
1 CSC 216 Lecture 3

Upload: edward-potter

Post on 19-Jan-2016

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

1

CSC 216CSC 216

Lecture 3Lecture 3

Page 2: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

2

Unit TestingUnit Testing

The most basic kind of testing is called unit testing Why is it called “unit” testing?

When should tests be written? Before the code for a class is written. After the code for a class is written, but

before other classes are written. After code for all classes is written, but

before the classes are integrated. After the classes are integrated.

The most basic kind of testing is called unit testing Why is it called “unit” testing?

When should tests be written? Before the code for a class is written. After the code for a class is written, but

before other classes are written. After code for all classes is written, but

before the classes are integrated. After the classes are integrated.

Page 3: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

3

Testing at the BoundariesTesting at the Boundaries

What does this mean? Example (from Dr. Jo Perry)

For a method that removes an element from a list Test on a list with 1 element Test on a list that does not contain the element to

be removed Test on a list where the element is at the

beginning/end of the list Test on a list where the element appears multiple

times Test on an empty list

What does this mean? Example (from Dr. Jo Perry)

For a method that removes an element from a list Test on a list with 1 element Test on a list that does not contain the element to

be removed Test on a list where the element is at the

beginning/end of the list Test on a list where the element appears multiple

times Test on an empty list

Page 4: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

4

Testing a Square-Root Routine

Testing a Square-Root Routine

From the text: What kinds of tests were proposed?

Test numbers greater than 1. Test numbers less than 1. Test negative numbers. Test randomly generated numbers. Apply the inverse of the square-

root operation.

From the text: What kinds of tests were proposed?

Test numbers greater than 1. Test numbers less than 1. Test negative numbers. Test randomly generated numbers. Apply the inverse of the square-

root operation.

Page 5: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

5

Testing a StackTesting a Stack

Suppose we have a MyStack class implemented in an integer array. MyStack(int n); void push(int i); int pop(); int top();

Suppose we have a MyStack class implemented in an integer array. MyStack(int n); void push(int i); int pop(); int top();

17

9

33

Page 6: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

6

Testing a StackTesting a Stack

Suppose we have a MyStack class implemented in an integer array. MyStack(int n); void push(int i); int pop(); int top();

Suppose we have a MyStack class implemented in an integer array. MyStack(int n); void push(int i); int pop(); int top();

17

9

15

Page 7: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

7

Testing a StackTesting a Stack

Suppose we have a MyStack class implemented in an integer array. MyStack(int n); void push(int i); int pop(); int top();

Suppose we have a MyStack class implemented in an integer array. MyStack(int n); void push(int i); int pop(); int top();

17

9

Page 8: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

8

Testing a StackTesting a Stack

Suppose we have a MyStack class implemented in an integer array. MyStack(int n); void push(int i); int pop(); int top();

What test cases should we use?

Suppose we have a MyStack class implemented in an integer array. MyStack(int n); void push(int i); int pop(); int top();

What test cases should we use?

17

Page 9: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

9

What is JUnit?What is JUnit?

An open-source regression-testing framework for Java

JUnit’s web site: http://junit.org Eclipse includes JUnit

However, you need to set it up inside each project.

It allows you to define regression tests for your code and run them from the Java IDE.

An open-source regression-testing framework for Java

JUnit’s web site: http://junit.org Eclipse includes JUnit

However, you need to set it up inside each project.

It allows you to define regression tests for your code and run them from the Java IDE.

Page 10: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

10

Testing » JUnit Test CasesTesting » JUnit Test Cases

JUnit Test Cases Test case

Runs multiple tests Implemented as subclass of TestCase Define instance variables that store the state

of the tests in the class Initialize TestCase by overriding setUp method Cleanup after test case is done by overriding tearDown method

The Test framework will invoke the setUp and tearDown methods.

JUnit Test Cases Test case

Runs multiple tests Implemented as subclass of TestCase Define instance variables that store the state

of the tests in the class Initialize TestCase by overriding setUp method Cleanup after test case is done by overriding tearDown method

The Test framework will invoke the setUp and tearDown methods.

Page 11: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

11

Testing » Setting up JUnitTesting » Setting up JUnit

JUnit is in Eclipse by default, but needs to be applied to a project by performing a few actions.

First, right-click on the name of the package.

JUnit is in Eclipse by default, but needs to be applied to a project by performing a few actions.

First, right-click on the name of the package.

Page 12: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

12

From the popup menu, select “Build Path”; then “Configure Build Path”

From the popup menu, select “Build Path”; then “Configure Build Path”

Testing » Setting up JUnit, cont.Testing » Setting up JUnit, cont.

Page 13: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

13

Then select “Add Library” and then “JUnit”.

Then select “Add Library” and then “JUnit”.

Testing » Setting up JUnit, cont.Testing » Setting up JUnit, cont.

Page 14: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

14

Then, when asked which version, just click on “Finish”.

Then, when asked which version, just click on “Finish”.

Testing » Setting up JUnit, cont.Testing » Setting up JUnit, cont.

Page 15: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

15

Testing » Creating JUnit Test Cases in EclipseTesting » Creating JUnit Test Cases in Eclipse

Create a new package to contain your test case classes.

Add the JUnit JAR file to the project’s buildpath.

Create a new package to contain your test case classes.

Add the JUnit JAR file to the project’s buildpath.

Page 16: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

16

Testing » Creating JUnit Test Cases in EclipseTesting » Creating JUnit Test Cases in Eclipse Select your testing package

From the context menu select New » JUnit Test Case. This opens the New JUnit Test Case Wizard.

Fill in the name of your test case in the Name field.

Select the method stubs that you want Eclipse to generate

This will create the corresponding class in your testing package

Select your testing package

From the context menu select New » JUnit Test Case. This opens the New JUnit Test Case Wizard.

Fill in the name of your test case in the Name field.

Select the method stubs that you want Eclipse to generate

This will create the corresponding class in your testing package

Page 17: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

17

Testing » JUnit TestCase TemplateTesting » JUnit TestCase Template

public class NewTestCase extends TestCase {

public static void main(String[] args) {}

public NewTestCase(String arg0) {super(arg0);

}

protected void setUp() throws Exception {super.setUp();

}

protected void tearDown() throws Exception {super.tearDown();

}}

public class NewTestCase extends TestCase {

public static void main(String[] args) {}

public NewTestCase(String arg0) {super(arg0);

}

protected void setUp() throws Exception {super.setUp();

}

protected void tearDown() throws Exception {super.tearDown();

}}

Page 18: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

18

Testing » Adding Tests to Test CasesTesting » Adding Tests to Test Cases Any method in a TestCase class is

considered a test if it begins with the word “test”. You can write many tests (have many

test methods) Each test method should use a

variety of assert… methods to perform tests on the state of its class.

Any method in a TestCase class is considered a test if it begins with the word “test”. You can write many tests (have many

test methods) Each test method should use a

variety of assert… methods to perform tests on the state of its class.

Page 19: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

19

Testing » JUnit Assert MethodsTesting » JUnit Assert Methods Assert methods include:

assertTrue(boolean) assertFalse(boolean) assertNull(object) assertNotNull(object) assertEquals(x,y) assertSame(firstObject, secondObj) assertNotSame(firstObject, secondObj)

Assert methods include: assertTrue(boolean) assertFalse(boolean) assertNull(object) assertNotNull(object) assertEquals(x,y) assertSame(firstObject, secondObj) assertNotSame(firstObject, secondObj)

Page 20: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

20

Testing » Example of a Test CaseTesting » Example of a Test Case

package stackTests;

import junit.framework.TestCase;import stack.MyStack;

public class Tests extends TestCase {

protected void setUp() throws Exception {super.setUp();

}

protected void tearDown() throws Exception {

super.tearDown();}public void testPush() {

MyStack = new MyStack(5); m.push(27); assertEquals(m.pop(), 27);}

}

package stackTests;

import junit.framework.TestCase;import stack.MyStack;

public class Tests extends TestCase {

protected void setUp() throws Exception {super.setUp();

}

protected void tearDown() throws Exception {

super.tearDown();}public void testPush() {

MyStack = new MyStack(5); m.push(27); assertEquals(m.pop(), 27);}

}

Page 21: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

21

Testing » Running JUnit Test CaseTesting » Running JUnit Test Case Select TestCase

class From the Run menu

select Run » Run As » JUnit Test

This will run the tests in your TestCase class along with the setUp and tearDown methods

You will then get a report in the JUnit window

Select TestCase class

From the Run menu select Run » Run As » JUnit Test

This will run the tests in your TestCase class along with the setUp and tearDown methods

You will then get a report in the JUnit window

Page 22: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

22

Testing » JUnit WindowTesting » JUnit Window

Red indicates a test has failed

You can see which test failed

You can see the call trace leading to the failure

Red indicates a test has failed

You can see which test failed

You can see the call trace leading to the failure

Page 23: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

23

Testing » JUnit WindowTesting » JUnit Window

You can see how many tests ran

How many failures occurred

You can see the details of the failure

Errors occur when exceptions are thrown (e.g., when assertions fail)

You can see how many tests ran

How many failures occurred

You can see the details of the failure

Errors occur when exceptions are thrown (e.g., when assertions fail)

Page 24: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

24

Testing » ExerciseTesting » Exercise

Go through the Stack class and add additional unit tests.

The stack class is at http://courses.ncsu.edu

/csc216/lec/001/exercises/stack.java Submit one of your test cases

here.

Go through the Stack class and add additional unit tests.

The stack class is at http://courses.ncsu.edu

/csc216/lec/001/exercises/stack.java Submit one of your test cases

here.

Page 25: 1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be

25

Important termsImportant terms

Black-box testing White-box testing Regression testing Test coverage

Black-box testing White-box testing Regression testing Test coverage