Interface and Implementation Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

Interfaces Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
1 CH2: What the Digerati Know Other people can teach you computer applications or you can figure them out for yourself © Copyright L. Snyder, 2004, modified.
Floating Point Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Unit Testing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Computer Science 111 Fundamentals of Programming I Files.
1 CS 106, Winter 2009 Class 4, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
Recursion Introduction to Computing Science and Programming I.
SM3121 Software Technology Mark Green School of Creative Media.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
Capture-Replay Mocks Scandinavian Developer Conference 4 th April 2011 Geoff Bache.
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Nanotech Example Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved.
@DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise
Syllabus Management System. The Problem There is need for a management system for syllabi that: Provides a simple and effective user interface Allows.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
Chapter 7 Formatted input and output. 7.1 introduction Tax: This result is correct; but it would be better Maybe as $13, Make formatting.
The Operating System ICS3M.  The operating system (OS) provides a consistent environment for other software programs to execute commands.  It gives.
Building Extensible Desktop Applications with Zope 3 Nathan R. Yergler Software Engineer Creative Commons Licensed under Creative Commons Attribution 2.5.
Pipes and Filters Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Files and Directories Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Labs 3: Bi-Grams. Step 1: Get Started Login: – Username: nombre\cc5212 – Password on board – C:/Program.
Software Design: Principles, Process, and Concepts Getting Started with Design.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Inheritance Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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.
Software Development. Software Development Loop Design  Programmers need a solid foundation before they start coding anything  Understand the task.
Text Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Files Tutor: You will need ….
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Word Counter HW Copyright © 2012 Pearson Education, Inc.
Introduction to Computer Programming - Project 1 Intro to Digital Technology.
Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer.
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
MEMORY is part of the Central Processing Unit, or CPU, where data and information are stored. There are two main types of memory in a computer – RAM.
Lecture 3 – MapReduce: Implementation CSE 490h – Introduction to Distributed Computing, Spring 2009 Except as otherwise noted, the content of this presentation.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4
Python Aliasing Copyright © Software Carpentry 2010
Software Development.
Examining Two Pieces of Data
Comments on the “Three Piles” Problem
Program Design Invasion Percolation: Testing
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
Programming Basics Web Programming.
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
Program Design Invasion Percolation: Neighbors
Easy Hacking LibreOffice
CH#3 Software Designing (Object Oriented Design)
What is an Operating System?
Cohesion and Coupling Chapter 5, Pfleeger 01/01/10.
Number and String Operations
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
Chapter 3: The Efficiency of Algorithms
Fundamentals of Programming I Files
Learning to Program in Python
Testability Software testability refers to the ease with which software can be made to demonstrate its faults through (typically execution-based) testing.
Fundamentals of Data Structures
Version Control Basic Operation Copyright © Software Carpentry 2010
ICT Functional Skills Input, Output & Storage Hardware and Software
Program Design Invasion Percolation: The Grid
MapReduce: Simplified Data Processing on Large Clusters
Presentation transcript:

Interface and Implementation Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Testing

Interface and Implementation One of the most important ideas in computing is the difference between interface and implementation

TestingInterface and Implementation One of the most important ideas in computing is the difference between interface and implementation Interface: how something interacts with the world

TestingInterface and Implementation One of the most important ideas in computing is the difference between interface and implementation Interface: how something interacts with the world Implementation: how it does what it does

TestingInterface and Implementation One of the most important ideas in computing is the difference between interface and implementation Interface: how something interacts with the world Implementation: how it does what it does def integrate(func, x1, x2):...math goes here... return result

TestingInterface and Implementation One of the most important ideas in computing is the difference between interface and implementation Interface: how something interacts with the world Implementation: how it does what it does def integrate(func, x1, x2):...math goes here... return result Interface: (f, x 1, x 2 ) -> integral

TestingInterface and Implementation One of the most important ideas in computing is the difference between interface and implementation Interface: how something interacts with the world Implementation: how it does what it does def integrate(func, x1, x2):...math goes here... return result Interface: (f, x 1, x 2 ) -> integral Implementation: we don't (have to) care

TestingInterface and Implementation Often use this idea to simplify unit testing

TestingInterface and Implementation Often use this idea to simplify unit testing Want to test components in program one by one

TestingInterface and Implementation Often use this idea to simplify unit testing Want to test components in program one by one But components depend on each other

TestingInterface and Implementation Often use this idea to simplify unit testing Want to test components in program one by one But components depend on each other How to isolate the component under test from other components?

TestingInterface and Implementation Often use this idea to simplify unit testing Want to test components in program one by one But components depend on each other How to isolate the component under test from other components? Replace the other components with things that have the same interfaces, but simpler implementations

TestingInterface and Implementation Often use this idea to simplify unit testing Want to test components in program one by one But components depend on each other How to isolate the component under test from other components? Replace the other components with things that have the same interfaces, but simpler implementations Sometimes requires refactoring

TestingInterface and Implementation Often use this idea to simplify unit testing Want to test components in program one by one But components depend on each other How to isolate the component under test from other components? Replace the other components with things that have the same interfaces, but simpler implementations Sometimes requires refactoring Or some up-front design

TestingInterface and Implementation Back to those fields in Saskatchewan...

TestingInterface and Implementation Test function that reads a photo from file

TestingInterface and Implementation Test function that reads a photo from file def read_photo(filename): result = set() reader = open(filename, 'r') …fill result with rectangles in file… reader.close() return result

TestingInterface and Implementation Test function that reads a photo from file def read_photo(filename): result = set() reader = open(filename, 'r') …fill result with rectangles in file… reader.close() return result def test_photo_containing_only_unit(): assert read_photo('unit.pht') == { ((0, 0), (1, 1)) }

TestingInterface and Implementation Experience teaches that this is a bad idea

TestingInterface and Implementation Experience teaches that this is a bad idea 1. External files can easily be misplaced

TestingInterface and Implementation Experience teaches that this is a bad idea 1. External files can easily be misplaced 2. Hard to understand test if fixture stored elsewhere

TestingInterface and Implementation Experience teaches that this is a bad idea 1. External files can easily be misplaced 2. Hard to understand test if fixture stored elsewhere 3. File I/O is much slower than memory operations

TestingInterface and Implementation Experience teaches that this is a bad idea 1. External files can easily be misplaced 2. Hard to understand test if fixture stored elsewhere 3. File I/O is much slower than memory operations The longer tests take to run, the less often they will be run

TestingInterface and Implementation Experience teaches that this is a bad idea 1. External files can easily be misplaced 2. Hard to understand test if fixture stored elsewhere 3. File I/O is much slower than memory operations The longer tests take to run, the less often they will be run And the more often developers will have to backtrack to find and fix bugs

TestingInterface and Implementation Original function def count_rect(filename): reader = open(filename, 'r') count = 0 for line in reader: count += 1 reader.close() return count

TestingInterface and Implementation Original function One rectangle per line, no comments or blank lines def count_rect(filename): reader = open(filename, 'r') count = 0 for line in reader: count += 1 reader.close() return count

TestingInterface and Implementation Original function One rectangle per line, no comments or blank lines Real counter would be more sophisticated def count_rect(filename): reader = open(filename, 'r') count = 0 for line in reader: count += 1 reader.close() return count

TestingInterface and Implementation Refactored def count_rect_in(reader): count = 0 for line in reader: count += 1 return count def count_rect(filename): reader = open(filename, 'r') result = count_rect_in(reader) reader.close() return result

TestingInterface and Implementation Refactored Does the work, but does not open the file def count_rect_in(reader): count = 0 for line in reader: count += 1 return count def count_rect(filename): reader = open(filename, 'r') result = count_rect_in(reader) reader.close() return result

TestingInterface and Implementation Refactored Opens the file def count_rect_in(reader): count = 0 for line in reader: count += 1 return count def count_rect(filename): reader = open(filename, 'r') result = count_rect_in(reader) reader.close() return result

TestingInterface and Implementation Refactored Opens the file Keeps name of original function def count_rect_in(reader): count = 0 for line in reader: count += 1 return count def count_rect(filename): reader = open(filename, 'r') result = count_rect_in(reader) reader.close() return result

TestingInterface and Implementation Now write tests

TestingInterface and Implementation Now write tests from StringIO import StringIO Data = ''' ''' def test_num_rect(): reader = StringIO(Data) assert count_rect(reader) == 3

TestingInterface and Implementation Now write tests A "file" that tests can be run on from StringIO import StringIO Data = ''' ''' def test_num_rect(): reader = StringIO(Data) assert count_rect(reader) == 3

TestingInterface and Implementation Now write tests Acts like a file, but uses a string in memory for storage from StringIO import StringIO Data = ''' ''' def test_num_rect(): reader = StringIO(Data) assert count_rect(reader) == 3

TestingInterface and Implementation Now write tests Doesn't know it isn't reading from a real file from StringIO import StringIO Data = ''' ''' def test_num_rect(): reader = StringIO(Data) assert count_rect(reader) == 3

TestingInterface and Implementation Use the same method to test output

TestingInterface and Implementation Use the same method to test output Write to a StringIO

TestingInterface and Implementation Use the same method to test output Write to a StringIO Use getvalue to get and check its final contents

TestingInterface and Implementation def test_write_unit_only(): fixture = { ((0, 0), (1, 1)) } writer = StringIO() photo_write(fixture, writer) result = writer.getvalue() assert result == ' \n' Use the same method to test output Write to a StringIO Use getvalue to get and check its final contents

TestingInterface and Implementation Use the same method to test output Write to a StringIO Use getvalue to get and check its final contents Doesn't know it isn't reading from a real file def test_write_unit_only(): fixture = { ((0, 0), (1, 1)) } writer = StringIO() photo_write(fixture, writer) result = writer.getvalue() assert result == ' \n'

TestingInterface and Implementation Use the same method to test output Write to a StringIO Use getvalue to get and check its final contents Get everything written to the StringIO as a string def test_write_unit_only(): fixture = { ((0, 0), (1, 1)) } writer = StringIO() photo_write(fixture, writer) result = writer.getvalue() assert result == ' \n'

TestingInterface and Implementation One more task

TestingInterface and Implementation def photo_write(photo, writer): contents = list(photo) contents.sort() for rect in contents: print >> writer, rect[0][0], rect[0][1], rect[1][0], rect[1][1] One more task

TestingInterface and Implementation One more task Why do the extra work of sorting? def photo_write(photo, writer): contents = list(photo) contents.sort() for rect in contents: print >> writer, rect[0][0], rect[0][1], rect[1][0], rect[1][1]

TestingInterface and Implementation One more task Why do the extra work of sorting? def photo_write(photo, writer): contents = list(photo) contents.sort() for rect in contents: print >> writer, rect[0][0], rect[0][1], rect[1][0], rect[1][1]

TestingInterface and Implementation def photo_write(photo, writer): for rect in photo: print >> writer, rect[0][0], rect[0][1], rect[1][0], rect[1][1] This version is simpler and faster

TestingInterface and Implementation This version is simpler and faster But there is no way to predict its output! def photo_write(photo, writer): for rect in photo: print >> writer, rect[0][0], rect[0][1], rect[1][0], rect[1][1]

TestingInterface and Implementation

TestingInterface and Implementation two_fields = { ((0, 0), (1, 1)), ((1, 0), (2, 1)) } photo_write(two_fields, …)

TestingInterface and Implementation two_fields = { ((0, 0), (1, 1)), ((1, 0), (2, 1)) } photo_write(two_fields, …)

TestingInterface and Implementation two_fields = { ((0, 0), (1, 1)), ((1, 0), (2, 1)) } photo_write(two_fields, …)

TestingInterface and Implementation two_fields = { ((0, 0), (1, 1)), ((1, 0), (2, 1)) } photo_write(two_fields, …) ≠

TestingInterface and Implementation two_fields = { ((0, 0), (1, 1)), ((1, 0), (2, 1)) } photo_write(two_fields, …) ≠ Sets are unordered

TestingInterface and Implementation two_fields = { ((0, 0), (1, 1)), ((1, 0), (2, 1)) } photo_write(two_fields, …) ≠ Sets are unordered Set elements are stored in an arbitrary order

TestingInterface and Implementation two_fields = { ((0, 0), (1, 1)), ((1, 0), (2, 1)) } photo_write(two_fields, …) ≠ Sets are unordered Set elements are stored in an arbitrary order We can't test if we can't predict the result

TestingInterface and Implementation Our existing tests are inconsistent

TestingInterface and Implementation # From input test Data = ''' ''' Our existing tests are inconsistent

TestingInterface and Implementation # From input test Data = ''' ''' # From output test def test_write_unit_only(): fixture = { ((0, 0), (1, 1)) } … assert result == ' \n' Our existing tests are inconsistent

TestingInterface and Implementation Our existing tests are inconsistent # From input test Data = ''' ''' # From output test def test_write_unit_only(): fixture = { ((0, 0), (1, 1)) } … assert result == ' \n'

TestingInterface and Implementation Our existing tests are inconsistent Do photo files have a newline at the end of the last line or not? # From input test Data = ''' ''' # From output test def test_write_unit_only(): fixture = { ((0, 0), (1, 1)) } … assert result == ' \n'

TestingInterface and Implementation Our existing tests are inconsistent Do photo files have a newline at the end of the last line or not? Either answer is better than "maybe" # From input test Data = ''' ''' # From output test def test_write_unit_only(): fixture = { ((0, 0), (1, 1)) } … assert result == ' \n'

TestingInterface and Implementation Have to design for test

TestingInterface and Implementation Have to design for test Depend on interface, not implementation

TestingInterface and Implementation Have to design for test Depend on interface, not implementation – So it's easy to replace other components for testing

TestingInterface and Implementation Have to design for test Depend on interface, not implementation – So it's easy to replace other components for testing – And tests don't have to be rewritten over and over

TestingInterface and Implementation Have to design for test Depend on interface, not implementation – So it's easy to replace other components for testing – And tests don't have to be rewritten over and over Isolate interactions with outside world

TestingInterface and Implementation Have to design for test Depend on interface, not implementation – So it's easy to replace other components for testing – And tests don't have to be rewritten over and over Isolate interactions with outside world – Like opening files

TestingInterface and Implementation Have to design for test Depend on interface, not implementation – So it's easy to replace other components for testing – And tests don't have to be rewritten over and over Isolate interactions with outside world – Like opening files Make things you are going to examine deterministic

August 2010 created by Greg Wilson Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.