Test-Driven Development



Similar documents
JUnit. Introduction to Unit Testing in Java

Test Driven Development Part III: Continuous Integration Venkat Subramaniam

Upping the game. Improving your software development process

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

Code Qualities and Coding Practices

Learning and Coaching Agile Methods. Görel Hedin Computer Science Lund University, Sweden

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

Java course - IAG0040. Unit testing & Agile Software Development

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

Agile Software Development. Venkat Subramaniam Agile Software Development

CPSC 330 Software Engineering

Deep Agile Blending Scrum and Extreme Programming. Jeff Sutherland Ron Jeffries

Agile QA s Revolutionary Impact on Project Management

Agile So)ware Development

Agile Testing and Extreme Programming

XP and Design. Paulo Caroli & Sudhindra Rao. ThoughtWorks

Introduction and Agenda

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Agile.NET Development Test-driven Development using NUnit

Win at Life. with Unit Testing. by Mark Story

Advanced Test-Driven Development

Development Techniques. CSE301 University of Sunderland Harry R. Erwin, PhD

Tools, Trends and Techniques for Developing Scientific Software

How To Be Successful At An Agile Software Engineering

Effective unit testing with JUnit

Co-Presented by Mr. Bill Rinko-Gay and Dr. Constantin Stanca 9/28/2011

Ingegneria del Software Corso di Laurea in Informatica per il Management. Agile software development

Fail early, fail often, succeed sooner!

Test Driven Development

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

Agile Techniques and Tools. White Paper

CSE 4415 / SWE 5415 Software Testing 2 Fall 2004 Olin Engineering Building, Room 128 Credits: 3.00

Continuous Integration, Delivery and Deployment. Eero Laukkanen T Software Testing and Quality Assurance P

Java. Java. e=mc 2. composition

Test Driven Development with Continuous Integration: A Literature Review

Software Quality Exercise 2

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

Code Quality Assurance. Peter Kofler, Code Cop FH Technikum Wien, February 2010

Agile Testing Intelliware Development Inc. BC

Advanced Software Testing

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

Introduction to Agile Software Development. EECS 690 Agile Software Development

An Introduction to Extreme Programming

Agile processes. Extreme Programming, an agile software development process. Extreme Programming. Risk: The Basic Problem

Test-Driven Development of IEEE 1451 Transducer Services and Application

Kevin Lee Technical Consultant As part of a normal software build and release process

Introduction to extreme Programming (XP)

Distributed Agile Development. Bapiraju Nandury Product Development Manager Bangalore Development Centre

Topics covered. Agile methods Plan-driven and agile development Extreme programming Agile project management Scaling agile methods

Methodology: Agile development of safety critical systems Annex D1.1.d to deliverable D1.1

Extreme Programming, an agile software development process

Agile Development and Testing Practices highlighted by the case studies as being particularly valuable from a software quality perspective

Fit for Change: Steps towards Effective Software Maintenance

Software infrastructure for Java development projects

Software Engineering I (02161)

Evaluating the Efficacy of Test-Driven Development: Industrial Case Studies

Software Construction

Extreme Programming, an agile software development process

TDD, Refactoring and Dependency Injection: Agile s answer to Big Up-Front Architecture (BUFA)

Test Driven Development

Java: Learning to Program with Robots. Chapter 11: Building Quality Software

Build management & Continuous integration. with Maven & Hudson

Scrum: A disciplined approach to product quality and project success.

Agile processes. Extreme Programming, an agile software development process

Continuous Integration: Improving Software Quality and Reducing Risk. Preetam Palwe Aftek Limited

Extreme Programming. Sergey Konovalov and Stefan Misslinger. May 23, 2006

Towards Software Configuration Management for Test-Driven Development

Coding in Industry. David Berry Director of Engineering Qualcomm Cambridge Ltd

Escaping the Legacy Hell International PHP Conference Benjamin & Kore 28. Oct 2013

Strangling. Legacy Code

Image Credit:

Igniting young minds through computer programming

Applied Software Project Management

Unit testing with JUnit and CPPUnit. Krzysztof Pietroszek

Quality Assurance Plan

Comparing Agile Software Processes Based on the Software Development Project Requirements

Java Unit testing with JUnit 4.x in Eclipse

Extreme Programming and Embedded Software Development

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

Best Practices for Java Projects Horst Rechner

Teaming Up for Software Development

Continuous Integration Multi-Stage Builds for Quality Assurance

Unit Testing. and. JUnit

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

Java Software Quality Tools and techniques

Content. Development Tools 2(63)

Evaluating the Efficacy of Test-Driven Development: Industrial Case Studies

Continuous Integration For Real: The Perforce Java Platform. Hamish Reid Perforce Software Inc.

Software Configuration Management Best Practices for Continuous Integration

Your guide to DevOps. Bring developers, IT, and the latest tools together to create a smarter, leaner, more successful coding machine

Agile/Automated Testing

Integrated Error-Detection Techniques: Find More Bugs in Java Applications

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

Chapter 8 Software Testing

How To Write Unit Tests In A Continuous Integration

Software Engineering Techniques

THE THREE ASPECTS OF SOFTWARE QUALITY: FUNCTIONAL, STRUCTURAL, AND PROCESS

Agile Testing: The Agile Test Automation Pyramid

Software Development Tools

Agile and Secure: Can We Be Both?

Transcription:

Test-Driven Development An Introduction Mattias Ståhlberg mattias.stahlberg@enea.com

Debugging sucks. Testing rocks.

Contents 1. What is unit testing? 2. What is test-driven development? 3. Example 4. Writing Testable Code 5. Continuous Integration 6. Recommended reading 7. Conclusions

What is a Unit Test? A unit is a class or a set of related functions Smallest part that makes sense to test Tests that the unit does what the programmer intended Tests the unit s public interface Written contract that the production code must satisfy Requires an automatic unit testing framework

Java Example with JUnit @Test public void shouldcalculateaveragespeed() { Track track = new Track(); track.addpoint(0, 0, 0); track.addpoint(0, 1, 1); track.addpoint(1, 1, 2); track.addpoint(1, 0, 3); track.addpoint(0, 0, 4); assertequals(1, track.averagespeed()); } @Test(expected=ArithmeticException.class) public void averagespeedshouldthrowexceptiononzerodistance() { Track track = new Track(); track.averagespeed(); }

Advantages of Unit-Tested Code Gives courage to modify and improve (refactor) the code Knowledge that the code works Problems solved early in the development phase Documents the code Little or no need for debugging Reduced need for costly manual testing Saves time and money

Properties of Good Unit Tests Descriptive name Readable Focused Isolated Automatic Repeatable Fast

Contents 1. What is unit testing? 2. What is test-driven development? 3. Test coverage 4. Visualizing test results 5. Excuses for not testing 6. How to succeed 7. Common pitfalls 8. Recommended reading 9. Conclusion

What is Test-Driven Development? Test-driven is more about design than about testing A design method that renders clean, well-tested code Short iterations where unit tests, either for improvements or new functionality, are written first

What is Test-Driven Development? (contd.) Can also be applied to other types of tests (e.g. acceptance tests) Does not imply no formal testing Kent Beck: Never write a single line of code unless you have a failing automated test Eliminate duplication

Development Cycle 1. Create a new automatic test case 2. Run all tests and watch the new one fail 3. Write production code to make the test pass 4. Run all tests and watch them pass 5. Refactor the code, e.g. eliminate duplicated code 6. Start over Refactor

Advantages of Test-Driven Development Interfaces are tried before they exist more mature interfaces More and better tests improved quality Design and code evolved in small steps less risk, less integration problems Less risk to develop functionality that might be needed Less risk to skip testing when time is short Addictive, stimulating, and great fun Regular and frequent positive feedback

Test-Driven Bug Fixing 1. Create an automatic test that indicates the existence of the bug 2. Locate the bug 3. Fix the bug Ensures that the automatic tests will detect the problem if it should arise again

Contents 1. What is unit testing? 2. What is test-driven development? 3. Example 4. Writing Testable Code 5. Continuous Integration 6. Recommended reading 7. Conclusions

Live Demonstration TDD using Eclipse and JUnit http://www.eclipse.org http://www.junit.org/

Contents 1. What is unit testing? 2. What is test-driven development? 3. Example 4. Writing Testable Code 5. Continuous Integration 6. Recommended reading 7. Conclusions

Writing testable code Use dependency injection Inject interfaces Favor object composition over class inheritance Impossible to chose different class hierarchy at testtime but easy to chose different object composition Favor polymorphism over conditional statements Leads to smaller and more focused classes, which are easier to test

Writing testable code (contd.) Avoid global state and singletons Cannot be replaced at test-time If you really have to have a singleton: Wrap it in a simple adapter Avoid non-private static method calls Cannot be replaced at test-time Can be wrapped in a simple adapter Strive for small and focused classes with clear responsibilities Test first!

Google's guide to testable code http://misko.hevery.com/code-reviewers-guide/ Flaw #1: Constructor does Real Work Flaw #2: Digging into Collaborators Flaw #3: Brittle Global State & Singletons Flaw #4: Class Does Too Much

Contents 1. What is unit testing? 2. What is test-driven development? 3. Example 4. Writing Testable Code 5. Continuous Integration 6. Recommended reading 7. Conclusions

Definition of Continuous Integration A fully automated and reproducible build, including testing, that runs many times a day Martin Fowler (September 2000) A software engineering practice in which isolated changes are immediately tested and reported on when they are added to a larger code base. http://whatis.com (July 2008)

Continuous Integration Benefits Allows each developer to integrate daily thus reducing integration problems Provides rapid feedback on the health of the software If a defect is introduced into the code base, it can be identified and corrected as soon as possible Enables project visibility at all levels

Visualizing Test Results

Try it out! Download Hudson from www.hudson-ci.org $> java jar hudson.war

Contents 1. What is unit testing? 2. What is test-driven development? 3. Example 4. Writing Testable Code 5. Continuous Integration 6. Recommended reading 7. Conclusions

Recommended Reading Kent Beck: Test-Driven Development By Example Andy Hunt, Dave Thomas: Pragmatic Unit Testing in Java with Junit Martin Fowler: Refactoring: Improving the Design of Existing Code http://www.refactoring.com/ http://xunitpatterns.com/ (also as printed book) http://googletesting.blogspot.com/ Testing on the toilet http://misko.hevery.com/code-reviewers-guide/

Contents 1. What is unit testing? 2. What is test-driven development? 3. Example 4. Writing Testable Code 5. Continuous Integration 6. Recommended reading 7. Conclusions

Unit testing Gives courage to refactor and improve Eliminates problems early Requires discipline Saves time and money

Test-driven development Is a design method Gives better unit tests Renders clean code that works Is great fun

Continuous Integration Brings visibility and rapid feedback to the project