Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software...

32
Test-Driven Development For Embedded C++ Programmers #421 By James Grenning and Robert Martin Object Mentor, Inc. Copyright © 2000-2003 by Object Mentor, Inc All Rights Reserved V1.0

Transcript of Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software...

Page 1: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

Test-Driven Development For Embedded C++ Programmers

#421 By James Grenning and Robert Martin

Object Mentor, Inc.

Copyright © 2000-2003 by Object Mentor, Inc All Rights Reserved

V1.0

Page 2: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 2

What is Test Driven Development?

• An iterative technique to develop software • As much (or more) about design as testing

– Encourages design from user’s point of view – Encourages testing classes in isolation – Produces loosely-coupled, highly-cohesive systems

• As much (or more) about documentation as testing • Must be learned and practiced

– If it feels natural at first, you’re probably doing it wrong

Page 3: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 3

Typical Development Cycle

Requirements

Design

Code

Test and Fix Test and FixTest and Fix

Test and FixTest and FixTest and Fix

Test

Test and Fix

Page 4: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 4

Iterative/Evolutionary Development Cycle

Requirements

Design

Code

Test

Page 5: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 5

Typical development cycle – Working Features

Requirements

Code

Wor

king

Fea

ture

s

Design

Page 6: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 6

Iterative/Evolutionary Development Cycle – Working Features

Requirements

Design

Code

Test

Wor

king

Fea

ture

s

Page 7: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 7

Automated Tests Provide a Safety Net

• Once a test passes, it is re-run with every change • Broken tests are not tolerated • Side affect defects are detected immediately • Assumptions are

continually checked

Unit Tests Acceptance Tests

Page 8: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 8

The Test Driven Development Cycle

Write a test for !new capability

Start

Compile

Fix compile !errors

Run the test!And see it fail

Write the code

Run the test!And see it pass

Refactor as needed

Page 9: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 9

Lots of Small Steps

• Shortest distance between two points

B’ !

A B

• Use test-driven to get from A to B in very small, verifiable steps

• You often end up in a better place

Page 10: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 10

Do the Simplest Thing

• Assume simplicity – Consider the simplest thing that could possibly work – Iterate to the needed solution

• When coding: – Build the simplest possible code that will pass the tests – Refactor the code to have the simplest design possible – Eliminate duplication

Page 11: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 11

The Rules of Simple DesignIN PRIORITY ORDER!

1. The code passes all tests

2. There is no duplication

3. The code expresses the programmer’s intention

4. Using the smallest number of classes and methods

!

Higher priority rules must be satisfied first

Page 12: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 12

Automated Tests

• Unit tests – Tests that show the programmer that the code does what is expected – Specifies what the code must do – Provide examples of how to use the code (documentation) – All tests are run every few minutes, with every change !

• Acceptance tests – Tests that show the stake holders that the code delivers the feature – All tests are run at least daily !

• All tests are Automated, you run them with every change

Page 13: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 13

What is Tested?

• Every class (module) has one or more unit tests • Test everything that can possibly break

• If it can’t break, don’t test it – Always a judgment call

int EventLog::GetCapacity() { return capacity; }

EventLogTest EventLog

Page 14: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 14

Testing Frameworks

• Tests must be automated – Otherwise they won’t be run

• Most OO languages have a testing framework, xUnit – JUnit, CppUnit(Lite), PyUnit, NUnit, VBUnit – A simple tool – Collects, organizes and automatically calls your test code

• Graphical test runner – Green bar makes you feel good

• Could be added to build environment

Page 15: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 15

Building Test Classes

• All of the testing frameworks work similarly • Your class inherits from a test framework class,

allowing your test to be plugged into the framework

TestCase

EventLogTesttestLogOne() testLogMany() testOverFlow() testPrint() !

EventLogLogIt() Count() Print()

Page 16: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 16

CppUnitLite

• Free C++ unit test harness • Uses macros to make test definition easy • Can be used to test C code • Tests are written that check binary conditions • Tests are repeatable !

• Download it from www.objectmentor.com

Page 17: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 17

EventLog Example - Demo

EventLog

+ LogIt(const char*, int) + GetCount( ) : int + Print( )

• Create a class that logs events • Each event has a character string and an integer • The log throws out the oldest entry once it exceeds

its capacity • The log can print itself !

• See paper for complete example

Page 18: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 18

Focus on Interface

• The test treats the object being tested like black box • Encourages design to be done from a client point of view

– The test is a user • You confront interface design issues

– What are the parameters? – What is the return type? – What is the behavior? – Who controls object lifetime?

Page 19: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 19

Embedded Test-Driven Developers

• Get code working in a friendlier environment prior to running on the target – Feedback – Efficient

• Decouple the application logic from the specific hardware dependencies

• Feed events into the system, verify the response

Page 20: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 20

Design Impacts

• Test-first design promotes testing a class in isolation – It must be decoupled from other classes

• Produces loosely coupled, highly cohesive systems – The hallmark of a good design – Object Oriented Design Principles and Programming

Languages help

Page 21: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 21

Testing a System of Objects

• Sure, unit tests work fine for a simple class like EventLog. But what about a class that collaborates with other classes?

• Home alarm system example – Front panel with LEDs, push buttons, times square

display – Phone line – The hardware won’t be ready for 3 months (one week

before delivery)

Page 22: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 22

Collaborators

• Most classes being tested need collaborators – e.g., Panel and phone line.

• Sometimes you can test with the real collaborators • Sometimes you can’t or shouldn’t

– The hardware is not ready, or it is slow, or hard to control

– It is difficult to get the response needed from the collaborator

• Impersonate collaborators with a Mock Object

Page 23: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 23

Mock Objects

• Unwanted dependencies can be broken with an interface

HomeGuardTesttestBreakIn() !

HomeGuardwindowIntrusion() !

FrontPaneldisplayMessage() soundAlarm() !

Model12FrontPanelsoundAlarm() !

MockFrontPanelsoundAlarm() !

HomeGuardTesttestBreakIn() !

HomeGuardwindowIntrusion() !

<<interface>> FrontPanel

displayMessage() soundAlarm() !

Page 24: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 24

Demo

• Email me at [email protected] for the example home guard code, or leave me a card with your request.

Page 25: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 25

Learning Test-First Design

• A skill which must be practiced – Initially awkward

• Requires discipline – Peer pressure – “I know how to write the class, but I don’t know how to test it”

• It's an addiction rather than discipline – Kent Beck – Author of

– Extreme Programming Explained – Test Driven Development

Page 26: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 26

Productivity and Predictability

• Defects kill predictability: – Cost of fixing is not predictable – When they materialize is not predictable

• Test-driven is predictable: – Working at a steady pace – Results in fewer bugs – More productive than “debug-later programming”

• Test-driven programmers rarely need the debugger

Page 27: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 27

Objections Heard

• “I know how to just write the code, but I don’t know how to test it.”

• “We have to write twice as much code” • “I have to debug twice as much code.” • “We have a testing department.” • “I can test my code after I write it.” • “That might work on easy software but our

problem is really tough” • “You need the target hardware”

Page 28: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 28

How do Acceptance Tests Fit InAcceptance tests use the application the same way the

hardware does, only they bypass the hardware

Hardware Implementation

Layer<<interface>>

Application Services

Application

+ service1() + service2()

Acceptance Tests<<interface>>

Hardware Control

Hardware Fake-out

Layer

Page 29: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 29

!Network

Core System Logic Depends on Hardware Specifics

Page 30: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 30

Separate Core System Logic from Hardware Specifics

!Network

Page 31: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 31

Test Core System Logic Independent of Hardware Specifics

Tests and Simulations

Tests and Simulations

Page 32: Test-Driven Development For Embedded C++ Programmers+v1.1.ppt.pdf · XP and Embedded Software Development – Class #421 James W Grenning . Title: TDD-Embedded-v1.0 Author: James

XP and Embedded Software Development – Class #421 James W Grenning Copyright © March 2002-2003 All Rights Reserved [email protected] 32

What’s in it for Embedded Developers

• Decouples embedded application from target hardware

• Progress can be made without target hardware • Side effect safety net • Can avoid costly simulators in favor of simpler

Mock Objects • Can avoid many long debus session