• No results found

Test-Driven Development

N/A
N/A
Protected

Academic year: 2021

Share "Test-Driven Development"

Copied!
30
0
0

Loading.... (view fulltext now)

Full text

(1)

Test-Driven Development

An Introduction Mattias Ståhlberg

(2)

Debugging

sucks.

(3)

Contents

1. What is unit testing?

2. What is test-driven development?

3. Example

4. Writing Testable Code

5. Continuous Integration

6. Recommended reading

(4)

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 intendedTests the unit’s public interface

Written contract that the production code must satisfyRequires an automatic unit testing framework

(5)

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

(6)

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 phaseDocuments the code

Little or no need for debugging

Reduced need for costly manual testingSaves time and money

(7)

Properties of Good Unit Tests

Descriptive nameReadableFocusedIsolatedAutomaticRepeatableFast

(8)

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)

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

(10)

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”

(11)

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

(12)

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 shortAddictive, stimulating, and great fun

(13)

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

(14)

Contents

1. What is unit testing?

2. What is test-driven development?

3. Example

4. Writing Testable Code

5. Continuous Integration

6. Recommended reading

(15)

Live Demonstration

TDD using Eclipse and JUnit

­ http://www.eclipse.org

(16)

Contents

1. What is unit testing?

2. What is test-driven development?

3. Example

4. Writing Testable Code 5. Continuous Integration

6. Recommended reading

(17)

Writing testable code

Use dependency injectionInject interfaces

Favor object composition over class inheritance

­ Impossible to chose different class hierarchy at test-time but easy to chose different object composition

Favor polymorphism over conditional statements

­ Leads to smaller and more focused classes, which are easier to test

(18)

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

(19)

Google's guide to testable code

http://misko.hevery.com/code-reviewers-guide/

Flaw #1: Constructor does Real WorkFlaw #2: Digging into Collaborators

Flaw #3: Brittle Global State & SingletonsFlaw #4: Class Does Too Much

(20)

Contents

1. What is unit testing?

2. What is test-driven development?

3. Example

4. Writing Testable Code

5. Continuous Integration 6. Recommended reading

(21)

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

(22)

Continuous Integration – Benefits

Allows each developer to integrate daily thus reducing

integration problems

Provides rapid feedback on the “health” of the softwareIf a defect is introduced into the code base, it can be

identified and corrected as soon as possible

(23)
(24)

Try it out!

Download Hudson from www.hudson-ci.org

(25)

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

(26)

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/

(27)

Contents

1. What is unit testing?

2. What is test-driven development?

3. Example

4. Writing Testable Code

5. Continuous Integration

6. Recommended reading

(28)

Unit testing

Gives courage to refactor and improveEliminates problems early

Requires discipline

(29)

Test-driven development

Is a design methodGives better unit tests

Renders clean code that worksIs great fun

(30)

Continuous Integration

References

Related documents

The custom, policy, and practice of Defendants Colorado Springs and Teller County of encouraging, condoning, tolerating, and ratifying the retaliation, detention, use of

Therefore, the series of workshops were designed to: (1) help students gain a better understanding of the richness of their identities and heritage; (2) motivate students to

As the name implies, this menu allows you to take point data in a shapefile and create gridfiles where each grid cell takes a value calculated based on the points that are

For more information and configuration instructions, see the Forcepoint Next Generation Firewall Product Guide and the Forcepoint Next Generation Firewall

Moss (1994; 2013) menar att en stor andel vårsådda grödor skulle göra renkavle till ett mycket mindre problem men det finns även delvis motsatt uppfatt- ning att vårsådda

If no written notice of any error or correction is received by PDTC within five (5) calendar days from receipt hereof, you shall be deemed to have accepted the accuracy

The second method is to use the Selenium framework through NetBeans IDE by exporting a recording or written script as Java code and run the test in NetBeans instead

The tropospheric origin of GOM measured at the Råö site is further supported by the fact that the highest GOM values were observed in conjunction to the import of air masses from