cs-2852 data structures lecture 7b andrew j. wozniewicz image copyright © 2010 andyjphoto.com

16
CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

Upload: anis-hodges

Post on 17-Jan-2018

216 views

Category:

Documents


0 download

DESCRIPTION

CS-2852 Data Structures, Andrew J. Wozniewicz What is Testing? The purpose of testing is to find bugs. No amount of testing “proves” correctness – impossible to test all scenarios. Process of exercising a program (or its part) under controlled conditions and verifying that the results are as expected. DEFINITION

TRANSCRIPT

Page 1: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852Data StructuresLECTURE 7B

Andrew J. Wozniewicz

Image copyright © 2010 andyjphoto.com

Page 2: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Agenda• Introduction to Testing• Automated Testing– JUnit

• Debugging

Page 3: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

What is Testing?

The purpose of testing is to find bugs.No amount of testing “proves”

correctness – impossible to test all scenarios.

Process of exercising a program (or its part) under controlled conditions and verifying

that the results are as expected.

DEFINITION

Page 4: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Classification of Testing I• Unit testing– Test a single method or class.

• Integration testing– Test a cluster of related classes, subsystems,

etc.• System testing– Test the whole program, as it will be used.

• Acceptance testing– System testing for the benefit of the

“customer”.

Page 5: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Classification of Testing II• Black-Box Testing– Based on public interfaces and external

specs– “Functional testing”

• White-Box Testing– Based on the intimate knowledge of the

implementation.– Exercise all/as many as possible paths of

execution– “Coverage testing”

Page 6: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Testing Ideas• Boundary Conditions– Special cases that might cause the

program to fail.• Test Scaffolding– Stubs, Mockups• Skeleton with minimal implementation.

– Pre- and Postconditions– Driver Programs

Page 7: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Unit Testing• Unit Test – Code written by a developer that tests a

specific functionality.– Used as part of specifying out the system

(pre-code)– Used for regression testing (post-code)

Page 8: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

JUnit Unit Testing Framework

• By Kent Beck and Erich Gamma• JUnit is a simple, open-source

framework to write repeatable tests.• Home Page: www.junit.org• Automatic: JUnit tests do not require

human judgment to interpret.• Easy to set up a multitude of tests and

always run them together.

Page 9: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

JUnit Demo

Page 10: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Writing a Simple JUnit Test• Step 1: Create a test class

import org.junit.*; import static org.junit.Assert.*; import java.util.*;

public class SimpleTest {

}

Page 11: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Writing a Simple JUnit Test• Step 2: Write a test method (annotated

with @Test) that asserts expected results on the object under test:

@Testpublic void testEmptyCollection() { Collection collection = new ArrayList(); assertTrue(collection.isEmpty());}

Page 12: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Writing a Simple JUnit Test• Step 3: Run the test from within :

RunRun As...JUnit Test

Page 13: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

JUnit Annotations@Test public void method()@Before public void method()@After public void method()@BeforeClass public void method()@AfterClass public void method()@Ignore@Test(expected=IllegalArgumentException.class)@Test(timeout=100)

Page 14: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

JUnit Test Methodsfail(String) assertTrue(true); assertsEquals([String message], expected, actual) assertsEquals([String message], expected, actual, tolerance) assertNull([message], object) assertNotNull([message], object) assertSame([String], expected, actual) assertNotSame([String], expected, actual) assertTrue([message], boolean condition)

Equals: Values, Same: References

Page 15: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Summary• Introduction to Testing• Automated Testing– JUnit

• Debugging

Page 16: CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

Questions?

Image copyright © 2010 andyjphoto.com