Test Driven Lasse Koskela Chapter 2: Beginning TDD

Slides:



Advertisements
Similar presentations
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
Advertisements

PS4: Test Driven Development Based on Test Driven Development by Example By Kent Beck.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
CS 2110 Software Design Principles II Based on slides originally by Juan Altmayer Pizzorno port25.com.
CS 201 Functions Debzani Deb.
TDD OVERVIEW OF TEST DRIVEN DEVELOPMENT by Paul M. code of the damned. com.
Test Driven Development TDD. Testing ”Testing can never demonstrate the absence of errors in software, only their presence” Edsger W. Dijkstra (but it.
Java: Chapter 1 Computer Systems Computer Programming II.
Effective Java: Generics Last Updated: Spring 2009.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
1 Design and Integration: Part 2. 2 Plus Delta Feedback Reading and lecture repeat Ambiguous questions on quizzes Attendance quizzes Boring white lecture.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Test-Driven Development Eduard Miric ă. The problem.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
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,
TDD Example Test Driven – Koskela Chapter 2. Mail Template Test List 1.Evaluating template “Hello, ${name}” with the value “Reader” for variable “name”
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Unit testing Why test your code when your code can test it for you?
Introduction to Parsing
Refactoring Presentation Yash Pant. Overview 3 Refactoring Types: 1. Replace Error Code with Exception 2. Split Temporary Variable 3. Remove Middle Man.
Summary prepared by Kirk Scott
Essentials of UrbanCode Deploy v6.1 QQ147
User-Written Functions
Chapter 6: The Stack Abstract Data Type
Data Abstraction: The Walls
Computer Programming I
Coding Defensively Coding Defensively
TDD Overview CS 4501/6501 Software Testing
A First Book of ANSI C Fourth Edition
CS139 – Fall 2010 How far we have come
Lesson 2: Building Blocks of Programming
Paul Ammann & Jeff Offutt
Some Tips for Using Eclipse
SWE 332 Last Modified Spring 2010 Paul Ammann
Chapter 12 Exception Handling and Text IO
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Test Driven Lasse Koskela Chapter 9: Acceptance TDD Explained
Introduction to Software Testing (2nd edition) Chapter 4 TDD Example
Unit 6 Working with files. Unit 6 Working with files.
Exceptions CSCE 121 J. Michael Moore
Programming in JavaScript
Part B – Structured Exception Handling
Test Driven Lasse Koskela Chapter 2: Beginning TDD
Effective Java, 3rd Edition Chapter 10: Exceptions
Test-Driven Development
Algorithm Correctness
Test Driven Lasse Koskela Chapter 3: Refactoring in Small Steps
Automation of Testing in the Distributed Common Ground System (Army)
Automation of Testing in the Distributed Common Ground System (Army)
Programming in JavaScript
CSE 143 Lecture 5 More ArrayIntList:
JUnit Reading: various web pages
Special instance methods: toString
CSC 143 Java Errors and Exceptions.
Test-Driven Development
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Code-development strategies
Effective Java, Chapter 9: Exceptions
CS31 Discussion 1D Fall18: week 2
Test-Driven Development
Chapter 11: Exception Handling
Test Driven Lasse Koskela Chapter 3: Refactoring in Small Steps
Reasoning with Types.
Presentation transcript:

Test Driven Lasse Koskela Chapter 2: Beginning TDD Paul Ammann http://cs.gmu.edu/~pammann/

Overview From Requirements to Tests Choosing the First Test Breadth-first, Depth-first Let’s Not Forget to Refactor Adding a Bit of Error Handling Loose Ends on the Test List Experience is a hard teacher because she gives the test first, the lesson afterward. 11/16/2018

From Requirements To Tests: Template System Example Template System as Tasks Template System as Tests Write a regular expression for identifying variables from the template Implement a template parser that uses the regex Implement template engine that provides public API and uses parser internally … Template without any variables renders as is Template with one variable is rendered with variable replace by value Template with multiple variables is rendered with each variable replace by appropriate value … Which Approach Do You Find More Natural? 11/16/2018

What Are Good Tests Made Of? A Good Test Is Atomic Keeps Things Focused A Good Test Is Isolated Doesn’t Depend On Other Tests Lots of Other Desirable Properties Of Tests… 11/16/2018

Programming By Intention Given an Initial Set Of Tests Pick One Goal: Most Progress With Least Effort Next, Write Test Code Wait! Code Won’t Compile! Simply Imagine Code Exists Use Most Natural Expression For Call Benefit of Programming By Intention Focus on What We COULD Have Instead of What We DO Have Incremental API Design From The Client Perspective 11/16/2018

Choosing The First Test Some Detailed Requirements: System replaces variable placeholders like ${firstname} in template with values provided at runtime Attempt to send template with undefined variables raises error System ignores variables that aren’t in the template Some Corresponding Tests Evaluating template “Hello, ${name}” with value Reader results in “Hello, Reader” Evaluating “${greeting}, ${name}” with “Hi” and “Reader” results in “Hi, Reader” Evaluating “Hello, ${name}” with “name” undefined raises MissingValueError 11/16/2018

Writing First Failing Test Evaluating template “Hello, ${name}” with value Reader results in “Hello, Reader” Listings 2.1, 2.2, 2.3 public class TestTemplate { @Test public void oneVariable() throws Exception { Template template = new Template(“Hello, ${name}”); template.set(“name”, “Reader”); assertEquals(“Hello, Reader”, template.evaluate()); } DO Try This At Home 11/16/2018

Code To Make Compiler Happy public class Template { public Template (String templateText) { } public void set (String variable, String value) { public String evaluate() { return null; The Test Fails, Of Course, On This Code (Listing 2.4 = 2.5) Let’s Run It And Make Sure We Get A RED Bar We’re At The RED part of RED-GREEN-REFACTOR 11/16/2018

Making The First Test Pass public class Template { public Template (String templateText) { } public void set (String variable, String value) { public String evaluate() { return “Hello, Reader”; // Couldn’t get easier than this! We’re Looking For The Green Bar – Listing 2.6 We Know This Code Will Change Later – That’s Fine Three Dimensions To Push Out Code: variable, value, template 11/16/2018

Another Test Purpose of 2nd Test (Listing 2.7) is to Drive Out Hard Coding of Variable’s Value Koskela Refers To Technique as Triangulation 11/16/2018

Another Test Revised Code (Listing 2.8) on Page 57 public class Template { private String variableValue; public Template(String templateText) { } public void set(String variable, String value) { this.variableValue = value; public String evaluate() { return "Hello, " + variableValue; 11/16/2018

Another Test Note Revisions To Junit in Listing 2.9 11/16/2018

Breadth-First, Depth-First What To Do With a “Hard” Red Bar? Issue is What To Fake Vs. What To Build “Faking” Is An Accepted Part Of TDD Really, It Means “Deferring A Design Decision” Depth First Means Supply Detailed Functionality Breadth First Means Covering End-To-End Functionality (Even If Some Is Faked) 11/16/2018

Handling Variables as Variables: Listing 2.10 public class Template { private String variableValue; private String templateText; public Template(String templateText) { this.templateText = templateText; } public void set(String variable, String value) { this.variableValue = value; public String evaluate() { return templateText.replaceAll("\\$\\{name\\}", variableValue); 11/16/2018

Multiple Variables Test (page 60) @Test public void multipleVariables() throws Exception { Template template = new Template("${one}, ${two}, ${three}"); template.set("one", "1"); template.set("two", "2"); template.set("three", "3"); assertEquals("1, 2, 3", template.evaluate()); } 11/16/2018

Multiple Variables: Solution (2.11) 11/16/2018

Special Test Case Special Case Test page 62 This test passes for free! public void unknownVariablesAreIgnored() throws Exception { Template template = new Template("Hello, ${name}"); template.set("name", "Reader"); template.set("doesnotexist", "Hi"); assertEquals("Hello, Reader", template.evaluate()); } 11/16/2018

Let’s Not Forget To Refactor Refactoring Applies To Code, and Test Code Compare Listing 2.12 with Refactored Listing 2.13 Note Use of Fixtures 11/16/2018

Listing 2.12: Ugly Template template = new Template("Hello, ${name}"); @Test public void oneVariable() throws Exception { Template template = new Template("Hello, ${name}"); template.set("name", "Reader"); assertEquals("Hello, Reader", template.evaluate()); } @Test public void differentTemplate() throws Exception { template = new Template("Hi, ${name}"); template.set("name", "someone else"); assertEquals("Hi, someone else", template.evaluate()); @Test public void multipleVariables() throws Exception { Template template = new Template("${one}, ${two}, ${three}"); template.set("one", "1"); template.set("two", "2"); template.set("three", "3"); assertEquals("1, 2, 3", template.evaluate()); @Test public void unknownVariablesAreIgnored() throws Exception { template.set("doesnotexist", "Hi"); 11/16/2018

Listing 2.13: Test Code is Still Code! 11/16/2018

Adding A Bit Of Error Handling Adding Exception Test Listing 2.14 Note Different Approaches To Testing Exceptions try/catch block with fail() vs. @Test(expected= …) Note Home Grown Exception Generally not recommended @Test public void missingValueRaisesException() throws Exception { try { new Template("${foo}").evaluate(); fail("evaluate() should throw an exception if " + "a variable was left without a value!"); } catch (MissingValueException expected) { } 11/16/2018

Corresponding Code: Listings 2.15, 2:16 11/16/2018

More Refactoring: Listing 2:17 11/16/2018

Exceptions With Diagnostics: Listing 2:18 import java.util.regex.Pattern; import java.util.regex.Matcher; private void checkForMissingValues(String result) { Matcher m = Pattern.compile("\\$\\{.+\\}").matcher(result); if (m.find()) { throw new MissingValueException("No value for " + m.group()); } 11/16/2018

Loose Ends On The Test List Performance? Add a Test Listing 2.19 (not shown in slides) Finally, A Test That Dooms Current Implementation @Test public void variablesGetProcessedJustOnce() throws Exception { template.set("one", "${one}"); template.set("two", "${three}"); template.set("three", "${two}"); assertTemplateEvaluatesTo("${one}, ${three}, ${two}"); } Next Chapter Addresses This “Major” Change Notion of “Spikes” So Far, “Easy” TDD 11/16/2018