Using TDD Making code maintainable, reusable and readable by writing tests.

Slides:



Advertisements
Similar presentations
Designing Reusable Frameworks for Test Automation
Advertisements

xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.
What is Unit Testing? How TDD Works? Tsvyatko Konov Telerik Corporation
Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
SOLID Object Oriented Design Craig Berntson
Chapter 21 Test Driven development. TDD Write the test first Show that test fails Write code to pass the test Run whole suite of tests! This is unit testing.
Test-Driven Development and Refactoring Project 3 Lecture 1 CPSC 315 – Programming Studio Fall 2009.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
XP and Refactoring David Talby. Development Methodologies The Software Crisis – 84% of software projects are not on time – 31% of software projects never.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
Testing HCI Usability Testing. Chronological order of testing Individual program units are built and tested (white-box testing / unit testing) Units are.
Xtreme Programming. Software Life Cycle The activities that take place between the time software program is first conceived and the time it is finally.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
Test-Driven Development “Test first, develop later!” –OCUnit.
Test Driven Development Derived from Dr. Fawcett’s notes Phil Pratt-Szeliga Fall 2009.
CIT 590 Unit testing. Agenda Debugging attempt 2 (because I am stubborn) What is unit testing Why? Unit testing framework in Python.
TDD,BDD and Unit Testing in Ruby
Test Driven Development TDD. Testing ”Testing can never demonstrate the absence of errors in software, only their presence” Edsger W. Dijkstra (but it.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Unit and Functional Testing Your Flex Applications Mike Nimer Dir. Of Engineering nomee.com.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
CSE 219 Computer Science III Testing. Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases:
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
LECTURE 38: REFACTORING CSC 395 – Software Engineering.
Refactoring Improving the structure of existing code Refactoring1.
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Future Media  BBC MMXI TDD at the BBC David Craddock, Jack Palfrey and Tom Canter.
Refactoring1 Improving the structure of existing code.
Refactoring for Testability (or how I learned to stop worrying and love failing tests) Presented by Aaron Evans.
AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Test-Driven Development Eduard Miric ă. The problem.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
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,
1 Presentation Title Test-driven development (TDD) Overview David Wu.
Occasion: Date: Present: Classification: Unit testing frameworks for OpenEdge Comparison between ProUnit and OEUnit. ABL Unit in OpenEdge 11.4 Mihai Pintea.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.
(1) Test Driven Development Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Refactoring1 Improving the structure of existing code.
Test Driven Development Introduction Issued date: 8/29/2007 Author: Nguyen Phuc Hai.
Unit Testing with FlexUnit
1 Test Coverage Coverage can be based on: –source code –object code –model –control flow graph –(extended) finite state machines –data flow graph –requirements.
CS223: Software Engineering Lecture 18: The XP. Recap Introduction to Agile Methodology Customer centric approach Issues of Agile methodology Where to.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
1 Punishment Through Continuous Delivery If it hurts, do it more often…
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Unit Testing - solid fundamentals
Test-driven development
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Software Testing.
Test Driven development
Test Driven development
Test-driven development (TDD)
Testing and Test-Driven Development CSC 4700 Software Engineering
Improving the structure of existing code
CSE 303 Concepts and Tools for Software Development
Unit Testing Good Practices
Test Case Test case Describes an input Description and an expected output Description. Test case ID Section 1: Before execution Section 2: After execution.
EEC-492/693/793 iPhone Application Development
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Refactoring Low Level/High Level.
Test-Driven Development
Refactoring.
Presentation transcript:

Using TDD Making code maintainable, reusable and readable by writing tests

Intro Julian Lyndon-Smith CTO & Co-founder whoGloo & nodeable Using progress since v3.. Specialists in OO design, Code refactoring Mentoring Versioning and Application Lifecycle Management System integration

Introduction to TDD Test Driven Development Test first Code second Different from traditional development methods Takes buy in from all involved Makes you think differently Hard to make the switch

History of TDD 1989 : “fit”, one of the first testing frameworks written 1994 : Extreme Programming (XP) starts appearing 1999 : a number of books on TDD start appearing 2000 : jUnit launched 2004 : proUnit 2010 : OEUnit 2014 : ABLUnit

Traditional development cycle Write code Run testsdeployFix bugs

TDD cycle Write test Run tests Write code Run tests Fix bugs refactor deploy

TDD cycle : Red, green, refactor Red Tests fail Green Tests pass Refactor Make code better to maintain and test

TDD cycle : Red Write a test for a class or method That does not exist A new requirement Test will (should) fail Why should we do this ? Makes you think of what functionality you are testing Makes you write the code required to pass the test only

TDD cycle : Green Now, write just enough code to make the test pass This is the difficult part ! Take this business requirement: A new method must, given an integer input value of 42, return false What is enough code to make the test pass ?

When enough is too much /** A new method must, given an integer input value of 42, return false */ method public logical myMethod(a as int): if a eq 42 then return false. else return true. end method.

WRONG… The requirement only stated what the method should do for an input value of 42. All other values are undetermined No requirement.. No test Otherwise you are writing code that may never be used

When enough is enough /** A new method must, given an integer input value of 42, return false */ method public logical myMethod(a as int): return false. end method.

Requirements are tests The previous example shows that there is at least one more requirement needed Other numbers apart from 42 The developer should liaise with other stakeholders “are you needing different results for other numbers?” Write new tests for new requirements

TDD cycle : Refactor Refactoring code is done to make the code Maintainable Readable Good code quality Your unit tests will help to check that you don’t break functionality DRY

The benefits of TDD Ensures quality code from the very beginning Promotes loosely-coupled code Can provide specifications by tests Give you confidence that your code works

The benefits of TDD Keeps unused code out of your systems Makes you think hard about application design Finds bugs quickly

The cost of bugs - augmentum.com

The cost of bugs – Dilbert

The benefits of TDD Code coverage Regression testing for free Stops recurring bugs Clean API design Reduced debugging Reduced development costs

The benefits of TDD (real world) Development of Maia4 Complete rewrite of Maia Took 3 weeks Including writing unit tests Found 4 bugs in progress …

The benefits of TDD (real world) Return in finally Doesn’t return longchars Recursive delete of a directory fails If path contains a folder starting with “.” Json parser hangs if data contains comments /* */ Static properties and method calls as part of a parameter cause gpf

The benefits of TDD (real world) Only 2 bugs in maia4 found after initial alpha release Extents not generated at all (missing code) Custom properties assigned to db Several bugs found in UI … No unit tests if not oProperty:has("isDbField") then return "".

The downsides of TDD Big time in investment Additional Complexity Harder than you think Selling to management Selling to developers ;) You lose the title of “Hacker” !

Unit tests What is a unit test ? Test of one requirement for one method Isolation other code / tests Other developers Targeted Repeatable Predictable

Achieving good design Writing tests first means that you have to describe what you want to achieve before you write the code In order to keep tests understandable and maintainable, keep them as short as possible. Long tests imply that the unit under test is too large If a component requires too many dependencies, then it is too difficult to test

Code design to help with TDD SOLID Code “smell” Refactoring

SOLID Single responsibility Each method and class should have only one responsibility Open / Close principle Open for extension, closed for modification Inheritance Liskov subsititution principle An object should be replaceable by the super class without breaking the application

SOLID Interface segregation principle Must not rely on interfaces that a client does not need Dependency inversion Code should depend on abstractions, not implementation

Code smell Mistaks: repeated mistaks ;) Duplicate code Big classes, huge methods Comments controversial … Bad names Too many if.. Then or case statements

Code refactoring – rename members method public decimal getValue(a as int,b as int): def var p as dec init no-undo. return (a * a) * b * p. end method.

Code refactoring – rename members method public decimal getCylinderVolume(radius as int, height as int): def var Pi as dec init no-undo. return (radius * radius) * height * Pi. end method.

Code refactoring Extract methods Extract interfaces Multiple implementation Encapsulation of properties Get / set Replace conditionals with polymorphism

Achieving good design Code which is complicated is Bad design Hard to maintain Hard to test Expensive to fix

TDD: Testing “smells” (1) Writing tests after writing code Not writing tests ! Duplicate logic in tests Code apart from asserts / setup logic in tests == bugs in tests (>90% likelyhood)

TDD: Testing “smells” (2) Remove tests Change tests Have test dependent on another test Have multiple asserts per test unless checking multiple properties per object

TDD: Best Practices (1) Increase code coverage Test reviews Manually introduce a bug if all tests pass, there’s a problem with the test Write tests first Make tests isolated

TDD: Best Practices (2) Ensure all unit tests pass. None should fail Integration tests should be in a separate project Test only publics (If possible) SOLID design Use Setup methods / refactor code into “helpers”

TDD: Best Practices (3) Make tests readable rather than maintainable Enforce test isolation Each test should set up and clean up it’s own state Any test should be repeatable Use variables instead of constants to make tests readable

TDD: Best Practices (bad method public void test#1(): def var lv_data as char no-undo. assign lv_data = gecode:getGPS("maitland","southend"). AssertString:IsNotNullOrEmpty(lv_data). end method.

TDD: Best Practices (good method public void get_the_GPS_coordinates_for_a_building_in_a_town(): def var lv_gpsCoord as char no-undo. def var lv_Town as char init "southend" no-undo. def var lv_Building as char init "maitland" no-undo. assign lv_gpsCoord = gecode:getGPS(lv_Building,lv_Town). AssertString:IsNotNullOrEmpty(lv_gpsCoord ). end method.

TDD: Best Practices (4) Tests should run in any order Name tests (add_LessThanZero_throwsException) Name variables / use pre-processor Start using Interfaces to facilitate tests “mocks”

maintainable trustworthy readable

book The art of unit testing Second edition Roy Osherove

Questions / Demos