Slides adapted from Alex Mariakakis, with material from Krysta Yousoufian, Mike Ernst, and Kellen Donohue Section 4: Graphs and Testing.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

1. Define the concept of assertions. 1 Explain the use of assertions. 2 Create Java program using assertions. 3 Run Java program using assertions. 4 2.
Practice Session 5 Java: Packages Collection Classes Iterators Generics Design by Contract Test Driven Development JUnit.
If (justMetYou) { crazy = true; cout
J-Unit Framework.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
try { Assert(Life. Real); Assert(Life
Slides adapted from Alex Mariakakis, with material from Krysta Yousoufian, Mike Ernst, Kellen Donohue Section 5: HW6 and Interfaces.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
JUnit, Revisited 17-Apr-17.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Intoduction to Unit Testing Using JUnit to structure Unit Testing SE-2030 Dr. Rob Hasker 1 Based on material by Dr. Mark L. Hornick.
1.  Writing snippets of code that try to use methods (functions) from your program.  Each snippet should test one (and only one) function......by calling.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
A programmer’s wife tells him, “Would you mind going to the store and picking up a loaf of bread. Also, if they have eggs, get a dozen.” The programmer.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
1 CSE 331 Unit Testing with JUnit slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
SLIDES ADAPTED FROM ALEX MARIAKAKIS, WITH MATERIAL FROM KRYSTA YOUSOUFIAN, MIKE ERNST, KELLEN DONOHUE Section 5: HW6 and Interfaces Justin Bare and Deric.
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
Introduction to Exceptions in Java CS201, SW Development Methods.
A programmer’s roommate tells him, “Would you mind going to the store and picking up a loaf of bread. Also, if they have eggs, get a dozen.” The programmer.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
CSE 143 Lecture 14: testing.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Section 4: Graphs and Testing
Unit Testing.
They both stop working when you open Windows!
Logger, Assert and Invariants
CSE 374 Programming Concepts & Tools
Testing UW CSE 160 Spring 2018.
Section 5: HW6 and Interfaces
More Selections BIS1523 – Lecture 9.
Section 5: HW6 and Interfaces
Conditions and Ifs BIS1523 – Lecture 8.
Testing UW CSE 160 Winter 2016.
Credit to Eclipse Documentation
Introduction to JUnit IT323 – Software Engineering II
The programmer returns with 12 loaves of bread.
Section 3 Graphs & Testing
Section 5: HW6 and Interfaces
Section 4: Graphs and Testing
CSC 143 Java Linked Lists.
CSE 143 Lecture 5 More ArrayIntList:
Section 5: HW6 and Interfaces
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Exceptions 10-May-19.
Exceptions 5-Jul-19.
Presentation transcript:

Slides adapted from Alex Mariakakis, with material from Krysta Yousoufian, Mike Ernst, and Kellen Donohue Section 4: Graphs and Testing

Agenda Graphs Java assertions Internal vs. external testing Representation invariants in real code

Graphs A B CD E

A B CD E Nodes

Graphs A B CD E Edges

Graphs A B CD E Children of A

Graphs A B CD E Parents of D

Graphs A B CD E Path from A to C

Graphs A B CD E Shortest path from A to C?

Graphs A B CD E Shortest path from A to B?

Practical Graphs Petr Simecek’s FB Mining

Practical (?) Graphs Chris Webb’s Eurovision Voting

Java Asserts Demo!

Enabling Java Asserts Right click the.java file you are running Go to “Run As” → “Run Configurations” Click on the “Arguments” tab Enter "-ea" under “VM arguments”

Assertions vs. Exceptions Assertions should check for things that should never happen Exceptions should check for things that might happen “Exceptions address the robustness of your code, while assertions address its correctness” public class LitterBox { ArrayList kittens; public Kitten getKitten(int n) { assert(n >= 0); return kittens(n); } public class LitterBox { ArrayList kittens; public Kitten getKitten(int n) { try { return kittens(n); } catch(Exception e) { }

Java Asserts assert(someValue); where someValue is a non-obvious boolean someValue should always be true unless something is broken Asserts do not run unless specifically enabled Our autograder will enable them, so remember to enable asserts!

Internal vs. External Testing Internal: JUnit How you decide to abstract the object Checked with implementation tests If it’s something you’re testing about your implementation that might not be true for everyone’s, it’s internal. External: test script How the client uses the object Checked with specification tests If it’s something that should be true for anybody’s implementation from the same spec, it’s external.

A JUnit Test Class A method is flagged as a JUnit test methods run when JUnit runs import org.junit.*; import static org.junit.Assert.*; public class TestSuite public void TestName1() {... }

JUnit Value Checking assertEquals, assertNull, assertNotSame, etc… These are not the same as Java assert Verify that a value matches expectations: assertEquals(42, meaningOfLife()); assertTrue(list.isEmpty()); If the value isn’t what it should be, the test fails Test immediately terminates Other tests in the test class are still run as normal Results show details of failed tests

JUnit Value Checking AssertionCase for failure assertTrue( test ) the boolean test is false assertFalse( test ) the boolean test is true assertEquals( expected, actual ) the values are not equal assertSame( expected, actual ) the values are not the same (by ==) assertNotSame( expected, actual ) the values are the same (by ==) assertNull( value ) the given value is not null assertNotNull( value ) the given value is null And others: Each method can also be passed a string to display if it fails: assertEquals("message", expected, actual)

Checking for Exceptions Verify that a method throws an exception when it should Test passes if specified exception is thrown, fails otherwise Only time it’s OK to write a test without a form of public void testGetEmptyList() { List list = new ArrayList (); list.get(0); }

Setup and Teardown Methods to run before/after each test case method is public void name() {... public void name() {... } Methods to run once before/after the entire test class public static void name() {... public static void name() {... }

Setup and Teardown public class Example { List public void initialize() { empty = new ArrayList(); public void size() {... public void remove() {... }

Don’t Repeat Yourself Can declare fields for frequently-used values or constants private static final String DEFAULT_NAME = “MickeyMouse”; private static final User DEFAULT_USER = new User(“lazowska”, “Ed”, “Lazowska”); Can write helper methods, etc. private void eq(RatNum ratNum, String rep) { assertEquals(rep, ratNum.toString()); } private BinaryTree getTree(int[] items) { // construct BinaryTree and add each element in items }

#1: Be descriptive When a test fails, JUnit tells you: Name of test method Message passed into failed assertion Expected and actual values of failed assertion The more descriptive this information is, the easier it is to diagnose failures Level of goodnessExample Good testAddDaysWithinMonth() Not so good testAddDays1(), testAddDays2() Bad test1(), test2() Overkill TestAddDaysOneDayAndThenFiveDaysStartingOn JanuaryTwentySeventhAndMakeSureItRollsBack ToJanuaryAfterRollingToFebruary()

#1: Be descriptive Take advantage of message, expected, and actual values No need to repeat expected/actual values or info in test name Use the right assert for the occasion: assertEquals(expected, actual) instead of assertTrue(expected.equals(actual))

Let’s put it all together! public class DateTest {... // Test addDays when it causes a rollover between public void testAddDaysWrapToNextMonth() { Date actual = new Date(2050, 2, 15); actual.addDays(14); Date expected = new Date(2050, 3, 1); assertEquals("date after +14 days", expected, actual); }

public class DateTest {... // Test addDays when it causes a rollover between public void testAddDaysWrapToNextMonth() { Date actual = new Date(2050, 2, 15); actual.addDays(14); Date expected = new Date(2050, 3, 1); assertEquals("date after +14 days", expected, actual); } Let’s put it all together! Tells JUnit that this method is a test to run

Let’s put it all together! public class DateTest {... // Test addDays when it causes a rollover between public void testAddDaysWrapToNextMonth() { Date actual = new Date(2050, 2, 15); actual.addDays(14); Date expected = new Date(2050, 3, 1); assertEquals("date after +14 days", expected, actual); } Descriptive method name

Let’s put it all together! public class DateTest {... // Test addDays when it causes a rollover between public void testAddDaysWrapToNextMonth() { Date actual = new Date(2050, 2, 15); actual.addDays(14); Date expected = new Date(2050, 3, 1); assertEquals("date after +14 days", expected, actual); } Use assertion to check expected results

Let’s put it all together! public class DateTest {... // Test addDays when it causes a rollover between public void testAddDaysWrapToNextMonth() { Date actual = new Date(2050, 2, 15); actual.addDays(14); Date expected = new Date(2050, 3, 1); assertEquals("date after +14 days", expected, actual); } Message gives details about the test in case of failure

#2: Keep tests small Ideally, test one thing at a time “Thing” usually means one method under one input condition Not always possible – but if you test x() using y(), try to test y() in isolation in another test Low-granularity tests help you isolate bugs Tell you exactly what failed and what didn’t Only a few (likely one) assert statements per test Test halts after first failed assertion Don’t know whether later assertions would have failed

#3: Be thorough Consider each equivalence class Items in a collection: none, one, many Consider common input categories Math.abs() : negative, zero, positive values Consider boundary cases Inputs on the boundary between equivalence classes Person.isMinor() : age 18 Consider edge cases -1, 0, 1, empty list, arr.length, arr.length-1 Consider error cases Empty list, null object

Other Guidelines Test all methods Constructors are exception to the rule Keep tests simple Minimize if/else, loops, switch, etc. Don’t want to debug your tests! Tests should always have at least one assert Unless testing that an exception is thrown Testing that an exception is not thrown is unnecessary assertTrue(true) doesn’t count! Tests should be isolated Not dependent on side effects of other tests Should be able to run in any order

JUnit Summary Tests need failure atomicity so we know exactly what failed Each test should have a descriptive name Assert methods should have clear messages to know what failed Write many small tests, not one big test Test for expected errors / exceptions Choose a descriptive assert method, not always assertTrue Choose representative test cases from equivalent input classes Avoid complex logic in test methods if possible Use to reduce redundancy between tests

External Testing This is for your client (us!) HW5 and on, class specifications are no longer provided; everyone’s might be different So how do we test your code?

Test Script Language Text file with one command listed per line First word is always the command name Remaining words are arguments Commands will be translated to some method(s) in your code

Test Script Language # Create a graph CreateGraph graph1 # Add a pair of nodes AddNode graph1 n1 AddNode graph1 n2 # Add an edge AddEdge graph1 n1 n2 e1 # Print the nodes in the graph and the outgoing edges from n1 ListNodes graph1 ListChildren graph1 n1 n1n2

Test Script Language CreateGraph A AddNode A n1 AddNode A n2 CreateGraph B ListNodes B AddNode A n3 AddEdge A n3 n1 e31 AddNode B n1 AddNode B n2 AddEdge B n2 n1 e21 AddEdge A n1 n3 e13 AddEdge A n1 n2 e12 ListNodes A ListChildren A n1 ListChildren B n2 n1n2 n3 n1n2

CheckRep: Linked List public void insert(element e) { checkRep(); // Do things! checkRep(); }

CheckRep private void checkRep() { // Checks invariants: ordered linked list check list not null check list elements are in ascending order check list first.prev is null... } quick! depends

CheckRep private void checkRep() { // Checks invariants: ordered linked list check list not null if(Thisprogram.DEBUG) check list elements order check list first.prev is null... }

CheckRep When should DEBUG = true? While debugging When should DEBUG = false? When submitting code Or, whenever speed is important Performance tests (not for 331!) HW6 grading timeouts While coding, sometimes (you don’t want to wait thirty minutes to test every time you change code)