• No results found

Test Driven Development

N/A
N/A
Protected

Academic year: 2021

Share "Test Driven Development"

Copied!
17
0
0

Loading.... (view fulltext now)

Full text

(1)

Construx

®

Software Development Best Practices

Test Driven

Development

http://www.construx.com

© 1999, 2006 Construx Software Builders Inc. All Rights Reserved.

Construx

®

Software Development Best Practices

Test Driven Development,

What is it and Why?

(2)

“Software Development Best Practices” 3 Construx

Traditional View of Testing

Acceptance Testing System Testing Integration and Component Testing Unit Testing “System Objectives” Requirements Design Coding

“Software Development Best Practices” 4 Construx

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

(3)

“Software Development Best Practices” 5 Construx

Improved View of Testing

Integration and Component Test Planning Unit Test Planning System Test Planning Acceptance Test Planning Acceptance Test Execution System Test Execution Integration and Component Test Execution Unit Test Execution “System Objectives” Requirements Design Coding

“Software Development Best Practices” 6 Construx

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”

(4)

“Software Development Best Practices” 7 Construx

Test Driven Development’s View

Unit Test Fails

System Test Passes Coding Requirements Unit Test Planning System Test Planning System Test Fails

Unit Test Passes

“Software Development Best Practices” 8 Construx

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

(5)

“Software Development Best Practices” 9 Construx

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” 10 Construx

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

(6)

“Software Development Best Practices” 11 Construx

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

Construx

®

Software Development Best Practices

QA-level

(7)

“Software Development Best Practices” 13 Construx

A Use Case Description Template

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.

Exceptions

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) Priority

Identify any nontypical execution scenarios that still constitute successful completion of use case. These are different ways that postconditions could still be satisfied

Alternative courses

Identify what must be true on completion of use case to complete successfully Postconditions

Identify what must be true at the start of use case to complete successfully Preconditions

List any other specific (i.e., nonfunctional) requirements that apply to use case Special

requirements

Describe typical execution scenario, if important Normal course

Identify any assumptions behind specification of use case Assumptions

Give an overall description of intent of use case Description

Identify which actors can access use case Actor(s)

Use case name here Use case # nnn

“Software Development Best Practices” 14 Construx

Example Use Case

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.

Postconditions

Highest

Priority

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.

Assumptions

Must be completed in under 60 seconds.

Special requirements

One or more minimum airport connection times isn’t satisfied. Traveler is on the FBI watch list.

Exceptions

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).

Alternative courses

Reservation request is made and completed.

Normal course

Each specified flight segment exists. There is room available in fare/class for each flight. All applicable advance-purchase/stay requirements are satisfied.

Preconditions

Make a reservation on one or more requested flight segments in name of specified traveler.

Description

Travel Agent, Traveler, Airline Ticket Agent

Actor(s)

Make Flight Reservation(s)

(8)

“Software Development Best Practices” 15 Construx

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” 16 Construx

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

(9)

“Software Development Best Practices” 17 Construx

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”

Construx

®

Software Development Best Practices

Developer-level

(10)

“Software Development Best Practices” 19 Construx

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” 20 Construx

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

(11)

“Software Development Best Practices” 21 Construx

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” 22 Construx

JUnit Design

TestResult Test run():TestResult TestSuite run():TestResult addTest(Test) TestCase run():TestResult setUp() runTest() tearDown() MyTestCase
(12)

“Software Development Best Practices” 23 Construx

Asserts in JUnit

™

assertEquals()

™

assertFalse()

™

assertTrue()

™

assertSame()

™

assertNotSame()

™

assertNull()

“Software Development Best Practices” 24 Construx

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})); }

}

(13)

“Software Development Best Practices” 25 Construx

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” 26 Construx

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
(14)

“Software Development Best Practices” 27 Construx

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” 28 Construx

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++)

(15)

“Software Development Best Practices” 29 Construx

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” 30 Construx

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

(16)

Construx

®

Software Development Best Practices

Transitioning to

Test Driven Development

“Software Development Best Practices” 32 Construx

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

(17)

“Software Development Best Practices” 33 Construx

Contact Information

[email protected] www.construx.com (425) 636-0100 ™ Consulting ™ Seminars [email protected] www.construx.com

Construx

References

Related documents

NOW IS THE TIME FOR HEROES! MUTANTS & MASTERMINDS A G R E E N RONIN PRODUCTION Design & Development Steve Kenson Cover Art Ramón Pérez Editing Jon Leitheusser Executive Producer

ó9ê¶Ø/ô9Õ~Ú;çuցè9ÚÕAÙ%Ú;ïˆ×¼ê£ð~Ù%Øu鼨7ÕÇÖwêŸÚ åaååaååaååaåHååaåHååHåaååaååaååaåaå õ ä/å¬ò9å~ä

[r]

[r]

Ö %HÑ Ø ÓUÓ1ÜåÖlðÒç1ÖÝ1ÝLÜ éçoæ ç!ÑÓ1Ô Ó1éÐÖRÓ1ܹԂälÑ ç!ÐÜsî·éçfÑ ØóÑ

[r]

(ii) The ratio of visual signal level to coherent disturbances which are frequency- coincident with the visual carrier shall not be less than 47 decibels for coherent channel

opportunity to tell us about any accolades or funding you have already received, as well as share online links that provide more information about you. . Have you been