Introduction to Software Testing (2nd edition) Chapter 4 TDD Example

Slides:



Advertisements
Similar presentations
Introduction to Software Testing Chapter 1 Model-Driven Test Design Paul Ammann & Jeff Offutt
Advertisements

JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
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.
TDD OVERVIEW OF TEST DRIVEN DEVELOPMENT by Paul M. code of the damned. com.
Software Testing and Maintenance Lecture 4 Graph Coverage for Design Element Paul Ammann & Jeff Offutt Instructor: Hossein Momeni Mazandaran.
Refactoring An Automated Tool for the Tiger Language Leslie A Hensley
Paul Ammann & Jeff Offutt
PROPERTIES OF EXPONENTS
Introduction to Software Testing (2nd edition) Chapter 7.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
JUnit Automated Software Testing Framework Advanced Material Paul Ammann & Jeff Offutt
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Introduction to Software Testing (2nd edition) Chapter 4 Putting Testing First Paul Ammann & Jeff Offutt August.
Some facts about the cal() program February 5, 2015 Paul Ammann, SWE 437 with thanks to Bob Kurtz.
Introduction to Software Testing Model-Driven Test Design and Coverage testing Paul Ammann & Jeff Offutt Update.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
: Probability = chance = odds An event is one or more outcomes of an experiment An outcome is the result of a single trial of an experiment.
Software Development.
Lecture 5: Test-Driven Development Basics
Functions + Overloading + Scope
Paul Ammann & Jeff Offutt
Paul Ammann & Jeff Offutt
EGR 2261 Unit 11 Pointers and Dynamic Variables
Paul Ammann & Jeff Offutt
Paul Ammann & Jeff Offutt
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CS427: Software Engineering I
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
JUnit Automated Software Testing Framework
Introduction to Software Testing Chapter 9.2 Program-based Grammars
Paul Ammann & Jeff Offutt
Paul Ammann & Jeff Offutt
And how they make life easier!
Test-Driven Development
Algorithm Analysis CSE 2011 Winter September 2018.
MATRIX GRANT PROJECT Chapter2 Lesson4 Algebra Order of Operation
Paul Ammann & Jeff Offutt
void Pointers Lesson xx
Introduction to Software Testing Chapter 2 Model-Driven Test Design
JUnit Automated Software Testing Framework
Paul Ammann & Jeff Offutt
43 Order of Operations  ( ) + - X.
Introduction to Software Testing Chapter 9.2 Program-based Grammars
Test Driven Lasse Koskela Chapter 2: Beginning TDD
Paul Ammann & Jeff Offutt
Paul Ammann & Jeff Offutt
Fun facts about the cal() program
Software Testing and Maintenance Modifying Code
Paul Ammann & Jeff Offutt
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Introduction to Software Testing Chapter 3 Test Automation
Introduction to Software Testing Chapter 5.2 Program-based Grammars
Test-driven development (TDD)
Test Driven Lasse Koskela Chapter 9: Acceptance TDD Explained
Paul Ammann & Jeff Offutt
Test Driven Lasse Koskela Chapter 2: Beginning TDD
Test Driven Lasse Koskela Chapter 3: Refactoring in Small Steps
TDD & ATDD 1/15/2019.
OBJECTIVE: The students will simplify expressions by using the 7 laws of exponents.
Introduction to JUnit IT323 – Software Engineering II
Paul Ammann & Jeff Offutt
Paul Ammann & Jeff Offutt
Paul Ammann & Jeff Offutt
Paul Ammann & Jeff Offutt
Refactoring Low Level/High Level.
Paul Ammann & Jeff Offutt
Presentation transcript:

Introduction to Software Testing (2nd edition) Chapter 4 TDD Example Paul Ammann & Jeff Offutt http://www.cs.gmu.edu/~offutt/softwaretest/ September 2018

A Simple Example Using TDD TDD is different and feels awkward at first Even if you don’t decide to fully invest in TDD, trying it can help you think about testing and development in a different way TDD is most useful when faced with complex design decisions This example is based on Calc from chapter 3 No complex design decisions The intent is to help you understand the process Calc is a class to perform simple calculator operations Introduction to Software Testing, Edition 2 (Ch 4) © Ammann & Offutt

Starting Point We start TDD with no code, design, or requirements We choose an initial piece of functional behavior … and write the first test … Introduction to Software Testing, Edition 2 (Ch 4) © Ammann & Offutt

Class will be called “Calc” First Calc Test Class will be called “Calc” import org.junit.Test; import static org.junit.Assert.*; public class CalcTest { @Test public void testAdd() assertTrue (“Calc sum incorrect”, 5 == Calc.add (2, 3)); } Method signature: - Name: add() - 2 int parameters - Returns int This won’t compile (red light!), so let’s write minimal code to make it compile … Introduction to Software Testing, Edition 2 (Ch 4) © Ammann & Offutt

First Implementation of Calc public class Calc { static public int add (int a, int b) return 5; } Compiles Test runs correctly Green light! Why “5”? “Minimal” code to make test work May seem silly at first, but that’s TDD Introduction to Software Testing, Edition 2 (Ch 4) © Ammann & Offutt

Hard-coded return value won’t work Second Calc Test @Test public void testAddB() { assertTrue (“Calc sum incorrect”, 7 == Calc.add (3, 4)); } Hard-coded return value won’t work This will fail (red light!), so we have to change the Calc class … Introduction to Software Testing, Edition 2 (Ch 4) © Ammann & Offutt

Modification of Calc Refactor? public class Calc { static public int add (int a, int b) return a+b; } Compiles Test runs correctly Green light! Refactor? Calc class is probably okay, but the first test could be changed to “testAddA()” for consistency Introduction to Software Testing, Edition 2 (Ch 4) © Ammann & Offutt

First parameter is minuend Third Calc Test @Test public void testSubtract() { assertTrue (“Calc difference incorrect”, 5 == Calc.subtract (10, 5)); } Two decisions: name: subtract() First parameter is minuend Now we implement the subtract() method … Introduction to Software Testing, Edition 2 (Ch 4) © Ammann & Offutt

Continuing with Calc You can continue this process on your own (subtract, multiply, divide, memory, exponents, logs, numeric bases, etc.) TDD is not necessary for a class this simple, but it’s a good way to learn TDD can provide some benefits with more complicated design decisions Introduction to Software Testing, Edition 2 (Ch 4) © Ammann & Offutt

Expected Benefits of TDD Program always works as expected Correct relative to current tests Decisions are made when needed Not weeks or months early Decisions are more likely to be correct End result is more reliable Future modifications … Are cheaper Are easier Are less frequent Introduction to Software Testing, Edition 2 (Ch 4) © Ammann & Offutt

TDD & System Readiness Amount of system available and (appears to) work well System testing Steady state; deployment Lots of problems & rework System tests start passing TDD System integration Lots of early effort; nothing works Time Introduction to Software Testing, Edition 2 (Ch 4) © Ammann & Offutt