Test Driven Development



Similar documents
JUnit. Introduction to Unit Testing in Java

Overview. What is software testing? What is unit testing? Why/when to test? What makes a good test? What to test?

+ Introduction to JUnit. IT323 Software Engineering II By: Mashael Al-Duwais

Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE

TESTING WITH JUNIT. Lab 3 : Testing

Unit testing with JUnit and CPPUnit. Krzysztof Pietroszek

Test Driven Development

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Unit Testing. and. JUnit

Unit Testing and JUnit

Approach of Unit testing with the help of JUnit

Table of Contents. LESSON: The JUnit Test Tool...1. Subjects...2. Testing What JUnit Provides...4. JUnit Concepts...5

Introduction to unit testing with Java, Eclipse and Subversion

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

Java course - IAG0040. Unit testing & Agile Software Development

Test-Driven Development

Unit-testing with JML

Effective unit testing with JUnit

JUnit Automated Software Testing Framework. Jeff Offutt. SWE 437 George Mason University Thanks in part to Aynur Abdurazik. What is JUnit?

Extreme Programming and Embedded Software Development

Using JUnit in SAP NetWeaver Developer Studio

Tutorial IV: Unit Test

Test Driven Development Part III: Continuous Integration Venkat Subramaniam

JUnit - A Whole Lot of Testing Going On

Unit Testing JUnit and Clover

Unit Testing with FlexUnit. by John Mason

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

XP & Scrum. extreme Programming. XP Roles, cont!d. XP Roles. Functional Tests. project stays on course. about the stories

Introduction to C Unit Testing (CUnit) Brian Nielsen Arne Skou

Testing Tools and Techniques

Structured Testing in VDM

Agile.NET Development Test-driven Development using NUnit

XP and TDD. Extreme Programming and Test Driven Development. Bertrand Meyer, Manuel Oriol Andreas Leitner. Chair of Software Engineering ETH Zurich

Tutorial 7 Unit Test and Web service deployment

PHPUnit Manual. Sebastian Bergmann

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

Software Engineering. Top-Down Design. Bottom-Up Design. Software Process. Top-Down vs. Bottom-Up 13/02/2012

Chapter 1: Web Services Testing and soapui

webmunit for WebMethods Integration Server

Announcement. SOFT1902 Software Development Tools. Today s Lecture. Version Control. Multiple iterations. What is Version Control

Rigorous Software Development CSCI-GA

CS 451 Software Engineering Winter 2009

JDemo - Lightweight Exploratory Developer Testing

Data Structures. Algorithm Performance and Big O Analysis

Unit Testing with JUnit and CppUnit

Testing, Debugging, and Verification

CSE 326: Data Structures. Java Generics & JUnit. Section notes, 4/10/08 slides by Hal Perkins

Software Testing & Analysis (F22ST3): Static Analysis Techniques 2. Andrew Ireland

10 Deadly Sins of Software Estimation

15-214: Principles of Software Construction 8 th March 2012

PHPUnit Manual. Sebastian Bergmann

Mid- and Back Office System for Travel Agencies

Unit Testing & JUnit

Software Quality Exercise 2

Case studies: Outline. Requirement Engineering. Case Study: Automated Banking System. UML and Case Studies ITNP090 - Object Oriented Software Design

Using Use Cases on Agile Projects

Security Testing Web Applications throughout Automated Software Tests

National University of Ireland, Maynooth MAYNOOTH, CO. KILDARE, IRELAND. Testing Guidelines for Student Projects

The junit Unit Tes(ng Tool for Java

Lecture 17: Testing Strategies" Developer Testing"

Chapter 8 Software Testing

Exercises Engenharia de Software (cod & 6633 )

UNIT TESTING. Written by Patrick Kua Oracle Australian Development Centre Oracle Corporation

Writing Self-testing Java Classes with SelfTest

Advanced Test-Driven Development

Chapter 4: Design Principles I: Correctness and Robustness

CASE STUDY. Test Automation for India s Top Publically Listed Travel Portal

System Level Integration and Test Leveraging Software Unit Testing Techniques

RUP. Development Process. Iterative Process (spiral) Waterfall Development Process. Agile Development Process. Well-known development processes

Android Application Testing Guide

How To Write Unit Tests In A Continuous Integration

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Introduction. Motivational Principles. An Introduction to extreme Programming. Jonathan I. Maletic, Ph.D.

Tools for Integration Testing

Test Automation Integration with Test Management QAComplete

Extreme Programming, an agile software development process

Survey of Unit-Testing Frameworks. by John Szakmeister and Tim Woods

Tes$ng Hadoop Applica$ons. Tom Wheeler

SpiraTest / SpiraTeam Automated Unit Testing Integration & User Guide Inflectra Corporation

Automate APIs with Groovy Script

Fail early, fail often, succeed sooner!

Xtreme RUP. Ne t BJECTIVES. Lightening Up the Rational Unified Process. 2/9/2001 Copyright 2001 Net Objectives 1. Agenda

Evaluation of AgitarOne

Volunteer Reimbursement Policy

Transcription:

Software Development Best Practices Test Driven Development http://www.construx.com 1999, 2006 Software Builders Inc. All Rights Reserved. Software Development Best Practices Test Driven Development, What is it and Why?

Traditional View of Testing System Objectives Acceptance Testing Requirements System Testing Design Integration and Component Testing Coding Unit Testing Software Development Best Practices 3 We Pause to Bring You This Important Message... More than the act of testing, the act of designing tests is one of the best [defect] preventers known The thought process that must take place to create useful tests can discover and eliminate problems at every stage of development Boris Beizer Software Development Best Practices 4

Improved View of Testing System Objectives Acceptance Test Planning Acceptance Test Execution Requirements System Test Planning System Test Execution Design Integration and Component Test Planning Integration and Component Test Execution Coding Unit Test Planning Unit Test Execution Software Development Best Practices 5 XP s Test First Create test cases before writing code Business rep writes acceptance tests to demonstrate that user stories are correctly implemented Programmers continually write unit tests which must run flawlessly for development to continue We only write new code when we have a test that doesn t work Reference: [Jeffries01] Software Development Best Practices 6

Test Driven Development s View System Requirements Test Planning System Test Fails System Test Passes Unit Test Planning Unit Test Fails Coding Unit Test Passes Software Development Best Practices 7 Test Driven Development Process repeat select functionality to implement create and/or modify system-level tests repeat developer selects one unit-level module to write and/or modify developer creates and/or modifies unit-level tests repeat developer writes and/or modifies unit-level code until all unit-level tests pass until all system-level tests pass until no more functionality to implement Software Development Best Practices 8

Test A Little, Code A Little You don t need to write all the test cases first 1. You only have to create of one test that the current code won t pass 2. Write code to pass the test 3. Go back to step 1 If it is hard to write a test there may be a design issue Each round of test & code includes refactoring of the previous code Software Development Best Practices 9 Why Test Driven Development? One of the biggest problems in software is requirements ambiguity A direct result of using natural language specifications (e.g., The system shall be fast ) A test case is inherently unambiguous Test cases are unambiguous proxies for requirements Software Development Best Practices 10

Advantages of Test Driven Development Gradually builds an comprehensive suite of (hopefully automated) test cases Run that suite each time the code is compiled All tests must pass except the brand new one(s) Code can be refactored with confidence Saves time during integration and system testing Most tests can be run automatically Many integration errors can be found before system test Software Development Best Practices 11 Software Development Best Practices QA-level Test Driven Development

A Use Case Description Template Use case # nnn Actor(s) Description Preconditions Postconditions Priority Normal course Alternative courses Exceptions Special requirements Assumptions Use case name here Identify which actors can access use case Give an overall description of intent of use case Identify what must be true at the start of use case to complete successfully Identify what must be true on completion of use case to complete successfully State how important use case is relative to all of other use cases in the system (note: this could be interpreted as either execution priority or development priority) Describe typical execution scenario, if important Identify any nontypical execution scenarios that still constitute successful completion of use case. These are different ways that postconditions could still be satisfied Identify any execution scenarios that constitute unsuccessful completion of this use case. These are different ways that, despite the preconditions having been satisfied, the postconditions will not have been achieved. List any other specific (i.e., nonfunctional) requirements that apply to use case Identify any assumptions behind specification of use case Software Development Best Practices 13 Example Use Case Use case # 66 Actor(s) Description Preconditions Postconditions Priority Normal course Alternative courses Exceptions Special requirements Assumptions Make Flight Reservation(s) Travel Agent, Traveler, Airline Ticket Agent Make a reservation on one or more requested flight segments in name of specified traveler. Each specified flight segment exists. There is room available in fare/class for each flight. All applicable advance-purchase/stay requirements are satisfied. Each reservation instance has been created. The reservation confirmation has been given to actor. Space available in each fare/class for each flight has been reduced. Highest Reservation request is made and completed. Actor pays for reservations immediately. Actor preassigns seats. Actor requests special meal. Actor requests extra service like wheelchair or unaccompanied minor service (traveler is under 12 years old). One or more minimum airport connection times isn t satisfied. Traveler is on the FBI watch list. Must be completed in under 60 seconds. This use case only covers making reservations for one person at a time. It also doesn t address related travel services like rental car and hotel reservations. Software Development Best Practices 14

Testing from Use Cases Positive tests Normal course Each alternative course Combinations of alternative courses? Validate the assumptions? Negative tests Violate each precondition Force each exception Software Development Best Practices 15 Example Use Case-based Tests TC1 (Positive, normal course) Reservation on existing flight(s) with room in fare/class and advance purchase/stay requirements met reservation created, confirmation provided, availability reduced TC2 (Positive, alternative course 1) Reservation on existing flight(s) with room in fare/class and advance purchase/stay requirements met & pay immediately TC3 (Positive, alternative course 2) Reservation on existing flight(s) with room in fare/class and advance purchase/stay requirements met & pre-assign seat(s) TC4 (Positive, alternative course 3) Reservation on existing flight(s) with room in fare/class and advance purchase/stay requirements met & special meal TC5 (Positive, alternative course 4) Reservation on existing flight(s) with room in fare/class and advance purchase/stay requirements met & unaccompanied minor TC6 16 (Positive, all other combinations of alternative courses) Reservation on existing flight(s) with room in fare/class and advance purchase/stay requirements met & pay now & pre-assign seat(s) & special meal & unaccompanied minor reservation created, confirmed, availability--, paid, seat assigned, meal rqst d, and UM Software Development Best Practices 16

Example Use Case-based Tests (cont) TC17 (Negative, violate precondition 1) Reservation on non-existing flight(s) Flight doesn t exist TC18 (Negative, violate precondition 2) Reservation on existing flight(s) and advance purchase/stay requirements met but no room in fare/class No room in fare/class TC19 (Negative, violate precondition 3) Reservation on existing flight(s) with room in fare/class but advance purchase/stay requirements not met Advance purchase/stay not met TC20 (Negative, force exception 1) Reservation on existing flights with room in fare/class and advance purchase/stay requirements met but minimum connect time not met Minimum connect time not met TC21 (Negative, force exception 2) Reservation on existing flight(s) with room in fare/class and advance purchase/stay requirements met but traveler on FBI watch list Traveler on FBI Watch List Software Development Best Practices 17 Software Development Best Practices Developer-level Test Driven Development

Example of Test First Development Suppose we need a method that finds the largest number in an array int Largest.largest(int[] list); Given [ 7, 8, 9 ] it should return 9 From Andrew Hunt and David Thomas, Pragmatic Unit Testing: In Java with Junit, 2003 Software Development Best Practices 19 Example Test Cases [ 7, 8, 9 ] 9 [ 8, 9, 7 ] 9 [ 9, 7, 8 ] 9 [ 7, 9, 8, 9 ] 9 [ 1 ] 1 [ -9, -8, -7 ] -7 Order shouldn t matter Multiple occurrences are OK One element is still an array Negative numbers are OK Software Development Best Practices 20

Unit Test Frameworks Automates unit-level testing Write unit tests in the same language you are coding in Many open source, downloadable, free frameworks available JUnit for Java NUnit for C# CppUnit for C++ Links at www.xprogramming.com/software.htm Software Development Best Practices 21 JUnit Design Test run():testresult TestResult TestCase run():testresult setup() runtest() teardown() TestSuite run():testresult addtest(test) MyTestCase Software Development Best Practices 22

Asserts in JUnit assertequals() assertfalse() asserttrue() assertsame() assertnotsame() assertnull() Software Development Best Practices 23 JUnit Test Code (1) import junit.framework.* public class TestLargest extends TestCase { public TestLargest(String name) { super(name); public void testsimple() { assertequals(9, Largest.largest(new int[ ] {7, 8, 9)); What happens when we run this test? Software Development Best Practices 24

Initial Code for the Method public class Largest { public static int largest(int[] list) { int index, max = Integer.MAX_VALUE; for (index = 0; index < list.length-1; index++){ if (list[index] > max) { max = list[index]; return max; Software Development Best Practices 25 What happens when we run the test? There was 1 failure: 1)testSimple(TestLargest)junit.framework.AssertionF ailederror: expected: <9> but was: <2147483647> at TestLargest.testSimple(TestLargest.java:11) Assignment max = Integer.MAX_VALUE should have been more like max=0 Change the code, run the test, now it passes Software Development Best Practices 26

JUnit Test Code (2) import junit.framework.* public class TestLargest extends TestCase { public TestLargest(String name) { super(name); public void testsimple() { assertequals(9, Largest.largest(new int[ ] {7, 8, 9)); public void testorder() { assertequals(9, Largest.largest(new int[] {9, 8, 7)); assertequals(9, Largest.largest(new int[] {7, 9, 8)); Software Development Best Practices 27 What happens when we run the tests? There was 1 failure: 1)testOrder(TestLargest)junit.framework.AssertionF ailederror: expected: <9> but was: <8> at TestLargest.testOrder(TestLargest.java:10) Ignoring last item in the list for (index = 0; index < list.length-1; index++) should be for (index = 0; index < list.length; index++) Change the code, run the tests, now they pass Software Development Best Practices 28

JUnit Test Code (3) import junit.framework.* public class TestLargest extends TestCase { public TestLargest(String name) { super(name); // leaving out tests already shown public void testdups() { assertequals(9, Largest.largest(new int[ ] {9, 7, 9, 8)); public void testone() { assertequals(1, Largest.largest(new int[] {1)); public void testnegative() { int [] neglist = new int[] {-9, -8, -7; assertequals(-7, Largest.largest(negList)); Software Development Best Practices 29 What happens when we run the tests? There was 1 failure: 1)testNegative(TestLargest)junit.framework.Assertio nfailederror: expected: <-7> but was: <0> at TestLargest.testNegative(TestLargest.java:16) 0 is bigger than any negative number, so it will be returned as the largest, want max = Integer.MIN_VALUE Change the code, run the tests, now they pass Software Development Best Practices 30

Software Development Best Practices Transitioning to Test Driven Development Transitioning to Test Driven Development Don t try to write tests for the whole thing! Write tests for the parts you are adding or changing Write tests for parts that are causing you problems Gradually you ll build up a set of tests You may find the code isn t designed to make writing tests easy May have to be refactored or rewritten Software Development Best Practices 32

Contact Information Consulting@construx.com www.construx.com (425) 636-0100 Software Development Best Practices Consulting Seminars sales@construx.com www.construx.com Software Development Best Practices 33