CS 2110 Software Design Principles II Based on slides originally by Juan Altmayer Pizzorno port25.com.

Slides:



Advertisements
Similar presentations
CompSci 230 Software Design and Construction Software Quality 2014S2 Black box testing.
Advertisements

Agile Planning Dealing with Reality. Reality Basic agile principle – don’t expect static plans to hold, be flexible and expect changes.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
A Brief Introduction to Test- Driven Development Shawn M. Jones.
Written by: Dr. JJ Shepherd
Recursion, pt. 2: Thinking it Through. What is Recursion? Recursion is the idea of solving a problem in terms of solving a smaller instance of the same.
PS4: Test Driven Development Based on Test Driven Development by Example By Kent Beck.
Cell phones off Name signs out – congrats Sean! CSE 116 Introduction to Computer Science for Majors II1.
Test-Driven Development and Refactoring Project 3 Lecture 1 CPSC 315 – Programming Studio Fall 2009.
JUnit, Revisited 17-Apr-17.
20-Jun-15 XP Again. Test-Driven Development Advantages of writing tests first: Clarifies what the methods are supposed to do Methods are testable Methods.
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 C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
Test-Driven Development “Test first, develop later!” –OCUnit.
TDD OVERVIEW OF TEST DRIVEN DEVELOPMENT by Paul M. code of the damned. com.
CIT 590 Unit testing. Agenda Debugging attempt 2 (because I am stubborn) What is unit testing Why? Unit testing framework in Python.
By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.
TDD,BDD and Unit Testing in Ruby
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Test Driven Development TDD. Testing ”Testing can never demonstrate the absence of errors in software, only their presence” Edsger W. Dijkstra (but it.
Test Driven Development An approach to writing better code Jimmy Zimmerman Intel Corporation.
CS 2110 Software Design Principles II Based on slides originally by Juan Altmayer Pizzorno port25.com 1.
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.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Unit Testing 101 Black Box v. White Box. Definition of V&V Verification - is the product correct Validation - is it the correct product.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
A tool for test-driven development
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Test-Driven Development Eduard Miric ă. The problem.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
JUnit Don Braffitt Updated: 10-Jun-2011.
Refactoring - 1 CS494: Intro. to Refactoring Readings: –Refactoring for everyone: How and why to use Eclipse's automated refactoring features. By David.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Error Handling Tonga Institute of Higher Education.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Written by: Dr. JJ Shepherd
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Test Driven Development Introduction Issued date: 8/29/2007 Author: Nguyen Phuc Hai.
Unit Testing with FlexUnit
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Week 6 MondayTuesdayWednesdayThursdayFriday Testing III Reading due Group meetings Testing IVSection ZFR due ZFR demos Progress report due Readings out.
Shlomo Hershkop Basics overview. Shlomo Hershkop Basic Review - Overview Practice coding Practice coding finger guessing game finger guessing.
TDD Unit tests from a slightly different point of view Katie Dwyer.
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
Dept of Computer Science University of Maryland College Park
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
TDD Overview CS 4501/6501 Software Testing
Test Driven Lasse Koskela Chapter 2: Beginning TDD
Test-driven development (TDD)
Test Driven Lasse Koskela Chapter 2: Beginning TDD
CSE 303 Concepts and Tools for Software Development
TDD & ATDD 1/15/2019.
Testing Acknowledgement: Original slides by Jory Denny.
CSE 143 Lecture 5 More ArrayIntList:
Test-Driven Development
Junit Tests.
Testing Slides adopted from John Jannotti, Brown University
Presentation transcript:

CS 2110 Software Design Principles II Based on slides originally by Juan Altmayer Pizzorno port25.com

Overview  Last week: Design Concepts & Principles Refactoring  Today: Test-Driven Development TDD + JUnit by Example  We use JUnit testing to evaluate your homework assignments…

Tests can be great! “In my country of Kazakhstan testing is very nice! Make many tests please!

Testing can be great!  Many people  Write code without being sure it will work  Press run and pray  If it fails, they change something random  This  Never works  And ruins your Friday evening social plans  Test-Driven Development saves the day!

The Example  A collection class SmallSet containing up to N objects (hence “small”) typical operations: we’ll implement add(), size() add adds item contains item in the set? size # items

Test Driven Development  We’ll go about in small iterations 1. add a test 2. run all tests and watch the new one fail 3. make a small change 4. run all tests and see them all succeed 5. refactor (as needed)  We’ll use JUnit

JUnit  What do JUnit tests look like? SmallSet.java package edu.cornell.cs.cs2110; public class SmallSet {... } SmallSetTest.java package edu.cornell.cs.cs2110; import org.junit.Test; import static org.junit.Assert.*; public class SmallSetTest public void testFoo() { SmallSet s = new SmallSet();... assertTrue(...); public void testBar() {... }

A List of Tests  We start by thinking about how to test, not how to implement size=0 on empty set size=N after adding N distinct elements adding element already in set doesn’t change it throw exception if adding too many...  Each test verifies a certain “feature”

A First Test  We pick a feature and test it:  This doesn’t compile: size() is undefined  But that’s all right: we’ve started designing the interface by using it SmallSet class SmallSet {} SmallSetTest class SmallSetTest public void testEmptySetSize() { SmallSet s = new SmallSet(); assertEquals(0, s.size()); } }

Red Bar  A test can be defined before the code is written  Running the test yields a red bar indicating failure:  If we add the size function and re-run the the test, it works! SmallSet class SmallSet { public int size() { return 42; }

Green Bar  What’s the simplest way to make a test pass?  “Fake it till you make it”  Re-running yields the legendary JUnit Green Bar:  We could now refactor, but we choose to move on with the next feature instead SmallSet class SmallSet { public int size() { return 0; }

Adding Items  To implement adding items, we first test for it:  add() is undefined, so to run the test we define it: SmallSetTest class SmallSetTest public void public void testAddOne() { SmallSet s = new SmallSet(); s.add(new Object()); assertEquals(1, s.size()); } } SmallSet public int size()... public void add(Object o) {}

Adding Items  The test now fails as expected:  It seems obvious we need to count the number of items:  And we get a green bar: SmallSet private int _size = 0; public int size() { return 0; return _size; } public void add(Object o) { ++_size; }

Adding Something Again  So what if we added an item already in the set?  As expected, the test fails... SmallSetTest class SmallSetTest public void public void public void testAddAlreadyInSet() { SmallSet s = new SmallSet(); Object o = new Object(); s.add(o); assertEquals(1, s.size()); } }

Remember that Item?...  We need to remember which items are in the set...  All tests pass, so we can refactor that loop... SmallSet private int _size = 0; public static final int MAX = 10; private Object _items[] = new Object[MAX];... public void add(Object o) { for (int i=0; i < MAX; i++) { if (_items[i] == o) { return; } _items[_size] = o; ++_size; }

Refactoring  (...loop) which doesn’t “speak to us” as it could...  All tests still pass, so we didn’t break it! SmallSet (before) public void add(Object o) { for (int i=0; i < MAX; i++) { if (_items[i] == o) { return; } _items[_size] = o; ++_size; } SmallSet (after) private boolean inSet(Object o) { for (int i=0; i < MAX; i++) { if (_items[i] == o) { return true; } return false; } public void add(Object o) { if (!inSet(o)) { _items[_size] = o; ++_size; }

Too Many  What if we try to add more than SmallSet can hold?  The test fails with an error: ArrayIndexOutOfBoundsException  We know why this occurred, but it should bother us: “ArrayIndex” isn’t a sensible error for a “set” public void testAddTooMany() { SmallSet s = new SmallSet(); for (int i=0; i < SmallSet.MAX; i++) { s.add(new Object()); } s.add(new Object()); }

Size Matters  We first have add() check the size, ... re-run the tests, check for green, define our own exception... ... re-run the tests, check for green, and... SmallSet public void add(Object o) { if (!inSet(o) && _size < MAX) { _items[_size] = o; ++_size; } SmallSetFullException public class SmallSetFullException extends Error {}

Testing for Exceptions ... finally test for our exception:  The test fails as expected, so now we fix it... public void testAddTooMany() { SmallSet s = new SmallSet(); for (int i=0; i < SmallSet.MAX; i++) { s.add(new Object()); } try { s.add(new Object()); fail(“SmallSetFullException expected”); } catch (SmallSetFullException e) {} }

Testing for Exceptions ... so now we modify add() to throw:  All tests now pass, so we’re done: SmallSet public void add(Object o) { if (!inSet(o) && _size < MAX) { if (_size >= MAX) { throw new SmallSetFullException(); } _items[_size] = o; ++_size; }

Review  Started with a “to do” list of tests / features could have been expanded as we thought of more tests / features  Added features in small iterations  “a feature without a test doesn’t exist”

Is testing obligatory?  Yes and no…  When you write code in professional settings with teammates, definitely! In such settings, failing to test your code just means you are inflicting errors you could have caught on teammates! At Google, people get fired for this sort of thing!  So… in industry… test or perish!  But what if code is just “for yourself”?  Testing can still help you debug, and if you go to the trouble of doing the test, JUnit helps you “keep it” for re- use later.  But obviously no need to go crazy in this case

Fixing a Bug  What if after releasing we found a bug? Famous last words: “It works!”

A bug can reveal a missing test  … but can also reveal that the specification was faulty in the first place, or incomplete  Code “evolves” and some changing conditions can trigger buggy behavior  This isn’t your fault or the client’s fault but finger pointing is common  Great testing dramatically reduces bug rates  And can make fixing bugs way easier  But can’t solve everything: Paradise isn’t attainable in the software industry

Reasons for TDD  By writing the tests first, we test the tests design the interface by using it ensure the code is testable ensure good test coverage  By looking for the simplest way to make tests pass, the code becomes “as simple as possible, but no simpler” may be simpler than you thought!

Not the Whole Story  There’s a lot more worth knowing about TDD What to test / not to test e.g.: external libraries? How to refactor tests Fixtures Mock Objects Crash Test Dummies... Beck, Kent: Test-Driven Development: By Example

Even so…  The best code written by professionals will still have some rate of bugs  They reflect design oversights  Evolutionary change in requirements  Incompatibilities between modules developed by different people  So never believe that software will be flawless  Our goal in cs2110 is to do as well as possible  In later cs courses we’ll study “fault tolerance”!