Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.

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

Practice Session 5 Java: Packages Collection Classes Iterators Generics Design by Contract Test Driven Development JUnit.
1 Unit Testing with JUnit CS 3331 Fall 2009 Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report, 3(7):37-50, Available.
If (justMetYou) { crazy = true; cout
Slides adapted from Alex Mariakakis, with material from Krysta Yousoufian, Mike Ernst, and Kellen Donohue Section 4: Graphs and Testing.
J-Unit Framework.
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
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.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Utilities (Part 3) Implementing static features 1.
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.
Fall 2007CS 2251 Programming Tools Eclipse JUnit Testing make and ant.
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.
Intoduction to Unit Testing Using JUnit to structure Unit Testing SE-2030 Dr. Rob Hasker 1 Based on material by Dr. Mark L. Hornick.
Test automation / JUnit Building automatically repeatable test suites.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
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.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
SilkTest 2008 R2 SP1: Silk4J Introduction. ConfidentialCopyright © 2008 Borland Software Corporation. 2 What is Silk4J? Silk4J enables you to create functional.
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.
JUnit A framework which provides hooks for easy testing of your Java code, as it's built Note: The examples from these slides can be found in ~kschmidt/public_html/CS265/Labs/Java/Junit.
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
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
SLIDES ADAPTED FROM ALEX MARIAKAKIS, WITH MATERIAL FROM KRYSTA YOUSOUFIAN, MIKE ERNST, KELLEN DONOHUE Section 5: HW6 and Interfaces Justin Bare and Deric.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Slides with material from Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section 3: HW4, ADTs, and more.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
JUnit Testing Why we do this and how we can get better.
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
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 143 Lecture 14: testing.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Section 4: Graphs and Testing
Unit Testing.
Software Construction Lab 10 Unit Testing with JUnit
They both stop working when you open Windows!
Unit Testing with JUnit
Introduction to JUnit CS 4501 / 6501 Software Testing
Section 5: HW6 and Interfaces
Section 5: HW6 and Interfaces
Conditions and Ifs BIS1523 – Lecture 8.
CMPE212 – Stuff… Assn 2 due this Friday. Winter 2018
Credit to Eclipse Documentation
Introduction to JUnit CS 4501 / 6501 Software Testing
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
CSE 143 Lecture 5 More ArrayIntList:
Section 5: HW6 and Interfaces
Exceptions 25-Apr-19.
JUnit Reading: various web pages
Exceptions 22-Apr-19.
TCSS 360, Spring 2005 Lecture Notes
Exceptions 5-Jul-19.
Junit Tests.
Presentation transcript:

Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section 4: Graphs and Testing

AGENDA ✕ Graphs ✕ JUnit Testing ✕ Test Script Language ✕ JavaDoc

GRAPHS A B CD E Nodes and Edges

GRAPHS A B CD E Children of A

GRAPHS A B C D E Parents of D

GRAPHS A B CD E Paths from A to C:

GRAPHS A B CD E Paths from A to C: A -> C A -> D -> E -> C Shortest path from A to C?

Testing

INTERNAL VS. EXTERNAL TESTING ✕ Internal : JUnit  How you decide to implement the object  Checked with implementation tests ✕ External: test script  Your API and specifications  Testing against the specification  Checked with specification tests

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() {... }

USING JUNIT ASSERTIONS ✕ Verifies that a value matches expectations ✕ assertEquals(42, meaningOfLife()); ✕ assertTrue(list.isEmpty()); ✕ If the assert fails:  Test immediately terminates  Other tests in the test class are still run as normal  Results show “details” of failed tests (We’ll get to this later)

USING JUNIT ASSERTIONS 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: ✕ 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); }

“But don’t I need to create a list before checking if I’ve successfully added to it?”

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() {... }

Test Writing Etiquette

The Rules 1. Don’t Repeat Yourself ◦Use constants and helper methods 2. Be Descriptive ◦Take advantage of message, expected, and actual values 3. Keep Tests Small ◦Isolate bugs one at a time – Test halts after failed assertion 4. Be Thorough ◦Test big, small, boundaries, exceptions, errors

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); }

How To Create JUnit Test Classes ✕ Right-click hw5.test -> New -> JUnit Test Case ✕ Important: Follow naming guidelines we provide ✕ Demo

JUNIT ASSERTS VS. JAVA ASSERTS ✕ We’ve just been discussing JUnit assertions so far ✕ Java itself has assertions public class LitterBox { ArrayList kittens; public Kitten getKitten(int n) { assert(n >= 0); return kittens(n); }

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) { }

REMINDER: ENABLING ASSERTS IN ECLIPSE To enable asserts: Go to Run -> Run Configurations… -> Arguments tab -> input -ea in VM arguments section Do this for every test file

Expensive CheckReps ✕ Ant Validate and Staff Grading will have assertions enabled ✕ But sometimes a checkRep can be expensive ✕ For example, looking at each node in a Graph with a large number of nodes ✕ This could cause the grading scripts to timeout

Expensive CheckReps ✕ Before your final commit, remove the checking of expensive parts of your checkRep or the checking of your checkRep entirely ✕ Example: boolean flag and structure your checkRep as so: private void checkRep() { cheap-stuff if(DEBUG_FLAG) { // or can have this for entire checkRep expensive-stuff } cheap-stuff...

EXTERNAL TESTS: TEST SCRIPT LANGUAGE

TEST SCRIPT LANGUAGE ✕ Text file with one command listed per line ✕ First word is always the command name ✕ Remaining words are arguments ✕ Commands will correspond to methods in your code

TEST SCRIPT LANGUAGE (ex.test file) # 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

How To Create Specification Tests ✕ Create.test and.expected file pairs under hw5.test ✕ Implement parts of HW5TestDriver +driver connects commands from.test file to your Graph implementation to the output which is matched with.expected file ✕ Run all tests by running SpecificationTests.java +Note: staff will have our own.test and.expected pairs to run with your code +Do not hardcode.test/.expected pairs to pass, but instead make sure the format in hw5 instructions is correctly followed

DEMO: TEST SCRIPT LANGUAGE

JAVADOC API ✕ Now you can generate the JavaDoc API for your code ✕ Instructions in the Editing/Compiling Handout ✕ Demo: Generate JavaDocs