CSC 216/001 Lecture 4. Unit Testing Why is it called “unit” testing? When should tests be...

17
CSC 216/001 Lecture 4

Transcript of CSC 216/001 Lecture 4. Unit Testing Why is it called “unit” testing? When should tests be...

Page 1: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

CSC 216/001CSC 216/001

Lecture 4Lecture 4

Page 2: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

Unit TestingUnit 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.

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: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

Testing at the BoundariesTesting at the Boundaries

What does this mean? Example (from the online notes):

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 the online notes):

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: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

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: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

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? (Discuss in groups.)

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? (Discuss in groups.)

Page 6: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

What is JUnit?What is JUnit?

Regression testing framework Written by Erich Gamma and Kent

Beck Used for unit testing in Java Open Source Released under IBM's CPL

Regression testing framework Written by Erich Gamma and Kent

Beck Used for unit testing in Java Open Source Released under IBM's CPL

Page 7: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

Testing » Where Does JUnit Come From?Testing » Where Does JUnit Come From? JUnit’s web site:

http://junit.org/index.htm Eclipse includes JUnit

Eclipse provides new GUI to run JUnit test cases and suites

JDT tools include a plug-in that integrates JUnit into the Java IDE

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

JUnit’s web site: http://junit.org/index.htm

Eclipse includes JUnit Eclipse provides new GUI to run JUnit test

cases and suites JDT tools include a plug-in that integrates

JUnit into the Java IDE Allows you to define regression tests for

your code and run them from the Java IDE.

Page 8: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

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 9: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

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 10: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

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 11: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

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 12: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

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. Assert methods are inherited

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. Assert methods are inherited

Page 13: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

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

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

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

Page 14: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

Testing » Adding Two Tests to JUnit Test CaseTesting » Adding Two Tests to JUnit Test Case

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 void testCompareSucceed() {

assertEquals(0, 0); //this assertion will succeed}public void testCompareFail() { assertEquals(0, 1); //this assertion will fail}

}

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 void testCompareSucceed() {

assertEquals(0, 0); //this assertion will succeed}public void testCompareFail() { assertEquals(0, 1); //this assertion will fail}

}

Page 15: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

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 16: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

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

If you wish to see the tests in TestCase click on the Hierarchy tab

Red indicates a test has failed

You can see which test failed

You can see the call trace leading to the failure

If you wish to see the tests in TestCase click on the Hierarchy tab

Page 17: CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.

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)