DOC PREVIEW
UMD CMSC 131 - Lecture 13: unit testing

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)Lecture 13:unit testingLast time:1.Method overloading2.thisToday:1.Unit testing and JUnitCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)1The problemProblems:need to be able to make sure all parts are testedneed to know in testing exactly which part was not as expectedneed to be able to keep the tests for modifications made laterUnit testing helps overcome this problems of making sure everything is tested Unit testing: test each class and each part of the class (unit) individually Goal is to eliminate inconsistencies between the API and the actual working of the codeCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)2Unit TestingUnit testing helps overcome this problems of making sure everything is tested in a structured wayUnit testing: test each unit individually (micro level – each method or specifically each interaction described in the API)Goal is to eliminate errors within classesNeeds for unit testingMethod for defining tests = inputs, expected outputsMethod for running testsMethod for reporting resultsOne possibility: write a driver for each classDriver class contains main methodmain method creates objects in class to be tested, calls methods, prints outputsUser checks outputs, determines correctnessGood: easy, no special tools neededBad: tedious, relies on human inspection of outputsAnother approach: JUnit2CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)3JUnitA unit-testing tool for JavaIncludes capabilities for:Test definition, including output checkingTest running (execution)Result reportingSeamless integration with EclipseNoteIn this class we will use JUnit 3.8.1So, when given a choice select JUnit 3CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)4Structure of a JUnit 3.8.1 Test Caseimport junit.framework.TestCase;public class FunnyIntegerSetTest01 extends TestCase {public void testInsert() {FunnyIntegerSet set = new FunnyIntegerSet ();set.insert(3);assertTrue (set != null);}public void testFindClosest() {FunnyIntegerSet set = new FunnyIntegerSet ();set.insert (3);set.insert (6);assertEquals (6, set.findClosest(5));}}Test case nameJUnit libraryNeeded (will see why later in semester)TestsAssertions (result checkers)CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)5A Test Case Is … A Class!assertion checkersassertTrue(expression);If statementis true, keep running test; otherwise, halt test, report “fail”assertFalse(expression);If statement is false, keep running test; otherwise, halt test, report “fail”assertEquals(expression1, expression2);If expression1, expression2 equal, keep running test; otherwise, halt test, report “fail”If test terminates without failing, it passes that test  It continues with all susequent tests regardless of passing or failing the current test3CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)6Hints on TestingGive names to tests that relate to class being testedDevelop some tests before you codeHelps you to clarify what you are supposed to be doingGives you some ready-made tests to run while you codeUse tests to debugHow many tests?Statement coverage: develop tests to make sure each statement in class is executed at least once (including constructors)Decision coverage: develop tests to make each condition (if statement) in program both true and falseYou should at least reach statement coverage in your own


View Full Document

UMD CMSC 131 - Lecture 13: unit testing

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 13: unit testing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 13: unit testing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 13: unit testing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?