DOC PREVIEW
UMD CMSC 433 - Running many tests with Test Suites

This preview shows page 1-2-3-4 out of 12 pages.

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

Unformatted text preview:

Running many tests with Test Suitespublic classLogRecordTestextends TestCase {…public static Test suite() {TestSuitesuite = new TestSuite();suite.addTest(new LogRecordTest("equals1") {protected void runTest() { testEquals1();}});suite.addTest( new LogRecordTest(“equals2") {protected void runTest() { testEquals2();}}); return suite; }} Test Suites (cont’d)•If you follow certain constraints (discussed later), you can create test suites more easily:public static Test suite() { TestSuitesuite = new TestSuite(); suite.addTest(newLogRecordTest("testEquals1"));suite.addTest(newLogRecordTest("testEquals2"));return suite; } •Or simply:public static Test suite() {return new TestSuite(LogRecordTest.class); }Test Runner•To execute test suite, pick a class: –For graphical display •junit.awtui.TestRunnerTestCaseClassor•junit.swingui.TestRunnerTestCaseClass–For textual display•junit.textui.TestRunnerTestCaseClass•Or run from within your own code:public static void main(String args[]) {junit.textui.TestRunner.run(suite()); }Using Junitwith DrJava•At the top of the file, include: –import junit.framework.TestCase;•The main class of the file: –must be public, extend TestCase, and have a constructor of the form: •publicclassname(String name) { super(name);•Tests run automatically–must be publicand notstatic, return void, take no arguments, and have a name beginning with test–can use suite()as well•Verify results using–void assertTrue(String, boolean), void assertEquals(String, int, int), and void fail(String)•Set up tests using•protected void setUp()Exampleimport junit.framework.*;import java.io.*;public class LogRecordTestextends TestCase {protected String event1, event2;LogRecordtmp1, tmp2, tmp3;public LogRecordTest(String name) { super (name); }protected void setUp() {event1 = "event string1"; event2 = "event string2";tmp1 = new LogRecord(event1);tmp2 = new LogRecord(event2);tmp3 = new LogRecord(event2);}public void testEquals1() {assertTrue(tmp1.equals(tmp1)); }public void testEquals2() {assertTrue(!tmp1.equals(tmp2)); }Example, cont’dpublic void testEquals3() {assertTrue(!tmp3.equals(tmp2)); }public void testPubConstructor1() {assertTrue(tmp1.getEvent() == event1);assertTrue(tmp1.getTimestamp().compareTo(new java.util.Date()) <= 0);} public void testCompareTo1() { assertTrue(tmp1.compareTo(tmp1) == 0);assertTrue(tmp1.compareTo(tmp2) < 0);assertTrue(tmp2.compareTo(tmp1) > 0);assertTrue(tmp2.compareTo(tmp3) < 0);assertTrue(tmp3.compareTo(tmp2) > 0);assertTrue(tmp1.compareTo(tmp3) < 0);assertTrue(tmp3.compareTo(tmp1) > 0);}Example, cont’dpublic void testFormatFromFormat1() {StringWriters = new StringWriter();tmp1.format(new PrintWriter(s));LogRecordtmp4 = tmp1.fromFormat(new BufferedReader(new StringReader(s.toString())));assertTrue(tmp1.toString().equals(tmp4.toString()));}public static Test suite() { return new TestSuite(LogRecordTest.class); }public static void main(String args[]) { junit.textui.TestRunner.run(suite());}}How to come up with tests?•Strive to write tests that completely “cover” the code we’re testing•Structural coverage testing (i.e. white box):–based on control flow of the program–it can be a reasonable and objective criterion–It can be (partially) automated•But–no assurance of software qualityStructural Coverage Testing• Adequacy criteria –If significant parts of program structure are not tested, testing is surely inadequate•Control flow coverage criteria–Statement (node, basic block) coverage–Branch (edge) coverage–Condition coverage•Attempted compromise between the impossible and the inadequateStatement CoverageOne test datum (N=1, A[0]=-7, X=9) is enough to guarantee statement coverage of function selectFaults in handling positive values of A[i] would not be revealedintselect(intA[], intN, intX) {inti=0;while (i<N and A[i] <X) {if (A[i]<0) A[i] = -A[i];i++;}return(1);}i++i<N and A[i] <XA[i]<0A[i] = -A[i];return(1)TrueFalseTrueFalsei=0Branch Coveragei=0i<N and A[i] <XA[i]<0A[i] = -A[i];return(1)TrueFalseTrueFalseWe must add a test datum (N=1, A[0]=7, X=9) to cover branch False of the if statement. Faults in handling positive values of A[i] would be revealed. Faults in exiting the loop with condition A[i] <X would not be revealedintselect(intA[], intN, intX) {inti=0;while (i<N and A[i] <X) {if (A[i]<0) A[i] = -A[i];i++;}return(1);}i++Condition Coveragei=0i<N and A[i] <XA[i]<0A[i] = -A[i];return(1)TrueFalseTrueFalseBoth conditions (i<N), (A[i]<X) must be false and true for different tests. In this case, we must add tests that cause the while loop to exit for a value greater than X. Faults that arise after several iterationsof the loop would not be revealed. intselect(intA[], intN, intX) {inti=0;while (i<N and A[i] <X) {if (A[i]<0) A[i] = -A[i];i++;}return(1);}i++DebuggingDebugging•My program doesn’t work: why?•Some part of the program has a “bug;” narrow down the possible locations of the bug–Figure out which parts of the program work–Test the rest–Iterate•How to figure out which parts work?–Testing!Starting to Debug• What are the symptoms of the misbehavior?–Input/output–Stack trace (from thrown exception)•At what point did the program fail?•Reason backwards: what could have led to this failure?•What invariants should have been preserved?•Test the invariants, narrow down the problemChecking that Invariants Hold•Print statements–Print out expected invariants•Automatic debugger–Allows you to step through the program interactively–Verify expected invariants–Use as part of testingDr. Java Interactions Pane•Can evaluate Java expressions interactively–Can bind variables, execute expressions/statements•Benefits–Make sure that methods work as expected–Test invariants by constructing expressions not in program text–Combines with interactive debuggerDr. Java’s Automatic Debugger•Set execution breakpoints•Step through execution–into, over, and outof method calls•Examine the stack•Examine variable contents•Set watchpoints–Notified when variable contents changeUsing the Debugger•Start Dr. Java with the debug libraries:–java -classpath/usr/local/drjava/drjava-20020814.jar:/usr/local/j2sdk1.4.0/lib/tools.jaredu.rice.cs.drjava.DrJava•Creates debugging menu–Select debug mode to on•Turns on debug panel with state information•Set break point(s) in Java source•Run the programTips•Make the bug reproducible–If it’s


View Full Document

UMD CMSC 433 - Running many tests with Test Suites

Documents in this Course
Trace 1

Trace 1

62 pages

Reflection

Reflection

137 pages

Testing

Testing

25 pages

Paradigms

Paradigms

10 pages

Testing

Testing

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Trace 1

Trace 1

46 pages

Jini

Jini

4 pages

Final

Final

15 pages

Java RMI

Java RMI

13 pages

Testing

Testing

16 pages

Load more
Download Running many tests with Test Suites
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 Running many tests with Test Suites 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 Running many tests with Test Suites 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?