October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved.

Slides:



Advertisements
Similar presentations
Software Testing. Quality is Hard to Pin Down Concise, clear definition is elusive Not easily quantifiable Many things to many people You'll know it when.
Advertisements

Test-First Programming. The tests should drive you to write the code, the reason you write code is to get a test to succeed, and you should only write.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
A Brief Introduction to Test- Driven Development Shawn M. Jones.
Why Use Test Driven Development (TDD)?.  Why the need to change to TDD.  Talk about what TDD is.  Talk about the expectations of TDD.
Smalltalk Connections 1 Test Driven Development (TDD) Presented by Victor Goldberg, Ph.D.
PS4: Test Driven Development Based on Test Driven Development by Example By Kent Beck.
© The McGraw-Hill Companies, 2006 Chapter 9 Software quality.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
October 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved.
TDD,BDD and Unit Testing in Ruby
Apply Sub Procedures/Methods and User Defined Functions
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.
Networked File System CS Introduction to Operating Systems.
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
Chasing those perfect Hash functions… by DANIEL “3ICE” BEREZVAI ELTE.3ICE.HU/ /ALG2/GY/ALG2 GY 2 HF.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Testing Michael Ernst CSE 140 University of Washington.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
HOMEWORK REVIEW This is an if else statement layout if (condition) { code to be executed if condition is true; } else { code to be executed if condition.
Phoenix Software Projects Larry Beaty © 2007 Larry Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
From Quality Control to Quality Assurance…and Beyond Alan Page Microsoft.
Chapter 22 Developer testing Peter J. Lane. Testing can be difficult for developers to follow  Testing’s goal runs counter to the goals of the other.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Self-Documenting Code Chapter 32. Kinds of Comments  Repeat of code  Explanation of code  Marker in code  Summary of code  Description of code’s.
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
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,
Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
CSC 480 Software Engineering Test Planning. Test Cases and Test Plans A test case is an explicit set of instructions designed to detect a particular class.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
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.
(1) Test Driven Development Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
31/01/ Selection If selection construct.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Getting ready. Why C? Design Features – Efficiency (C programs tend to be compact and to run quickly.) – Portability (C programs written on one system.
Test Driven Development Introduction Issued date: 8/29/2007 Author: Nguyen Phuc Hai.
AVCE ICT – Unit 7 - Programming Session 12 - Debugging.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Google C++ Testing Framework Part 2: Assertion. Concepts A test case contains one or many tests. ◦ You should group your tests into test cases that reflect.
Evolution of C and C++ n C was developed by Dennis Ritchie at Bell Labs (early 1970s) as a systems programming language n C later evolved into a general-purpose.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Automated Testing with PHPUnit. How do you know your code works?
Software Engineering Lecture 11 Software Testing Presenter: Josef Hallberg 1.
FOP: Multi-Screen Apps
User-Written Functions
Logger, Assert and Invariants
Testing All Input is Evil pt 2.
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
Testing UW CSE 160 Winter 2017.
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
Test-driven development (TDD)
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
Getting serious about “going fast” on the TigerSHARC
EEC-492/693/793 iPhone Application Development
Testing.
Chapter 3 Debugging Section 3.4
Testing.
Unit Testing.
Lecture 6 - Recursion.
Presentation transcript:

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 1 Test Driven Development Demo Using COMUnit to drive development of Visual Basic macros in a spreadsheet

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 2 Framework (a.k.a. Test Harness) All TDD test frameworks (xUnit) include a simple tool to run the tests and report the errors. The framework typically allows individual tests to be run, or groups of tests to be run. The framework typically shows details about failures. The framework typically shows a green/red status bar. (red=test failures)

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 3 The test execution User Interface

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 4 Elements of the test execution User Interface

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 5 Elements of the test execution User Interface

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 6 Elements of the test execution User Interface

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 7 Elements of the test execution User Interface

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 8 The function to be tested Demo testing part of a standard deviation function in the original spreadsheet code The calculation is done in two steps, according to a standard deviation formula often used in programs that keep “running sums” as they traverse over data: –Add up the samples and the sum of samples –“Finish” the calculation according to this formula:

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 9 Getting started Load the spreadsheet that contains the test functions provided by COMUnit. Remember the TDD cycle: –Write a test –Add the test name to the list of tests to run –Compile (fails on missing function) –Write stub for the function to be tested –Run tests, test fails –Implement function to be tested –Run tests again, test passes

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 10 Public Sub testFinishStdDevWith3DataPoints(oTestResult As TestResult) Dim result As Double ' the data points are 7,8,9 result = FinishStdDev(3, , ) oTestResult.AssertEqualsDouble 1, result, 0.1 End Sub The first Test function

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 11 Adding test name to list of tests Public Property Get ITestContainer_TestCaseNames() As Variant() ' TODO: add the names of your test methods as a parameter into the Array() function ITestContainer_TestCaseNames = Array( _ "testFinishStdDevWith3DataPoints" _ ) End Property

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 12 “Compile” fails

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 13 Write stub function Doesn’t actually compute the return value yet Function FinishStdDev(N, sumOfSamples, sumOfSquares) FinishStdDev = 0 End Function

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 14 Run tests, test fails

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 15 Implement function to be tested Here’s the function from the original Sphygmochron spreadsheet. I’m leaving a few lines commented out because we don’t need them yet. (I’m “implementing” the smallest possible bit of the code to make the test pass.) Function FinishStdDev(N, sumOfSamples, sumOfSquares) temp = ((N * sumOfSquares) - (sumOfSamples ^ 2)) / (N * (N - 1)) 'If temp < 0 Then ' temp = temp * -1 'End If FinishStdDev = Sqr(temp) End Function

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 16 Run tests, test now passes

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 17 Write another test Public Sub testFinishStdDevWith1DataPoint(oTestResult As TestResult) Dim result as Double ' the data point is 7 result = FinishStdDev(1, 7, 49) oTestResult.AssertEqualsDouble 0, result, 0.0 End Sub

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 18 Run new test, it fails

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 19 Write code that makes the test run Function FinishStdDev(N, sumOfSamples, sumOfSquares) If (N = 1) Then FinishStdDev = 0 Else temp = ((N * sumOfSquares) - (sumOfSamples ^ 2)) / (N * (N - 1)) 'If temp < 0 Then ' temp = temp * -1 'End If FinishStdDev = Sqr(temp) End If End Function

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 20 Run (all) tests, new test passes

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 21 Example error checking test Public Sub testFinishStdDevWithBadInputNegativeIntermediateResult(oTestResult As TestResult) Dim result As Double On Error GoTo ErrorCheck ' the data points are 1,2,3, the sum of the squares should have been 14, not 5 result = FinishStdDev(3, 6, 5) oTestResult.AddFailure ("Expected 'invalid input' error, but didn't get it") Exit Sub ErrorCheck: oTestResult.AssertEqualsError Err, oTestResult.AssertEqualsString "FinishStdDev", Err.Source oTestResult.AssertEqualsString "Invalid Input", Err.Description End Sub

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 22 Run tests, error checking test fails

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 23 All failures were in the new test

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 24 Adding error checking The original code in the Sphygmochron had that “If Temp < 0” code to prevent the square-root operation from aborting on negative numbers However, a negative number could never occur there, and it prevented the original programmer from detecting bad input such as in our current test –We have found two errors (or at least weaknesses) in the original code

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 25 Write error checking code Function FinishStdDev(N, sumOfSamples, sumOfSquares) If (N = 1) Then FinishStdDev = 0 Else temp = ((N * sumOfSquares) - (sumOfSamples ^ 2)) / (N * (N - 1)) 'If temp < 0 Then ' temp = temp * -1 'End If If (temp < 0) Then Err.Raise 50000, "FinishStdDev", "Invalid Input" End If FinishStdDev = Sqr(temp) End If End Function

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 26 Run tests, error checking test passes

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 27 Write a new test This test should have worked, but I screwed it up. It was “too hard” to write. The first argument, the number of data points, should have been 5. Public Sub testFinishStdDevWith5DataPoints(oTestResult As TestResult) Dim result As Double ' the data points are 2, 4, 6, 8, 10 result = FinishStdDev(3, 30, 2 * * * * * 10) oTestResult.AssertEqualsDouble , result, End Sub

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 28 Run tests, newest test fails

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 29 Fix the new test This test should have worked, but I screwed it up. It was “too hard” to write. The first argument, the number of data points, should have been 5. Public Sub testFinishStdDevWith5DataPoints(oTestResult As TestResult) Dim result As Double ' the data points are 2, 4, 6, 8, 10 result = FinishStdDev(5, 30, 2 * * * * * 10) oTestResult.AssertEqualsDouble , result, End Sub

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 30 Run tests, now it passes

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 31 Refactor to simplify tests Now that all the tests are running, it’s OK to refactor a little bit to make it easier to write and maintain the tests I write a utility function to help prepare the arguments to the function being tested Function utilCallFinishStdDev(inputdata As Variant) Dim N, sumOfSamples, sumOfSquares Dim i N = UBound(inputdata) + 1 sumOfSamples = 0 sumOfSquares = 0 For i = 0 To UBound(inputdata) sumOfSamples = sumOfSamples + inputdata(i) sumOfSquares = sumOfSquares + inputdata(i) * inputdata(i) Next i utilCallFinishStdDev = FinishStdDev(N, sumOfSamples, sumOfSquares) End Function

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 32 Refactor to simplify tests I use the utility function in all the tests possible I no longer need the comment telling me what the data points are, to count the points, nor compute either of the sums This test is simpler and much easier to get right Public Sub testFinishStdDevWith5DataPoints(oTestResult As TestResult) Dim result As Double ' the data points are 2, 4, 6, 8, 10 result = FinishStdDev(5, 30, 2 * * * * * 10) result = utilCallFinishStdDev(Array(2, 4, 6, 8, 10)) oTestResult.AssertEqualsDouble , result, End Sub

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 33 Run tests, it still passes

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 34 Another simplification Move the “assertion” down into the utility function Sub utilCallFinishStdDev(inputdata As Variant, expectedResult As Double, oTestResult As TestResult) Dim N, sumOfSamples, sumOfSquares Dim i Dim result As Double N = UBound(inputdata) + 1 sumOfSamples = 0 sumOfSquares = 0 For i = 0 To UBound(inputdata) sumOfSamples = sumOfSamples + inputdata(i) sumOfSquares = sumOfSquares + inputdata(i) * inputdata(i) Next i result = FinishStdDev(N, sumOfSamples, sumOfSquares) oTestResult.AssertEqualsDouble expectedResult, result, End Sub

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 35 Another simplification The test itself gets very simple. In fact, all of the “positive” tests for the FinishStdDev function become one line long. Public Sub testFinishStdDevWith5DataPoints(oTestResult As TestResult) Dim result As Double result = utilCallFinishStdDev(Array(2, 4, 6, 8, 10)) oTestResult.AssertEqualsDouble , result, End Sub Public Sub testFinishStdDevWith5DataPoints(oTestResult As TestResult) utilCallFinishStdDev Array(2, 4, 6, 8, 10), , oTestResult End Sub

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 36 Run tests, it still passes

October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved. 37 Wrap-up I took 4 lines of code from the original Sphygmochron code and made it a testable function I found a divide-by-zero error, an apparently unnecessary and poorly-coded “absolute value” function, and a missed opportunity for detecting bad input data, all in what we thought were 4 correct lines of code Refactoring tests gave me a very easy (1-line) mechanism to use to write future tests for this function If any future programmer changes the function, my tests prevent from breaking functionality that I know is important today TDD enabled me to do this by encouraging thinking about valuable test cases before implementing the parts of the function