100% found this document useful (1 vote)
360 views

TDD - Test Driven Development

The document discusses test-driven development (TDD) and its benefits. TDD involves writing tests before code to specify requirements and ensure code meets those requirements. It results in code with fewer bugs, easier to maintain and refactor. Well-written tests also serve as documentation for how code should work. The process of TDD is to write tests, write code to pass tests, then refactor code as needed. An example of testing a credit card submission process is provided.

Uploaded by

dpaluy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
360 views

TDD - Test Driven Development

The document discusses test-driven development (TDD) and its benefits. TDD involves writing tests before code to specify requirements and ensure code meets those requirements. It results in code with fewer bugs, easier to maintain and refactor. Well-written tests also serve as documentation for how code should work. The process of TDD is to write tests, write code to pass tests, then refactor code as needed. An example of testing a credit card submission process is provided.

Uploaded by

dpaluy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

TDD Test Driven Development

David Paluy June 15, 2008

Agenda
WHY

HOW + Example

Test Driven Development

You dont have to do TDD,


You should do TDD.

When do we need to write tests?

If my code works perfectly, why should I write tests for it?

Realization 1:
Testing breeds confidence

Confidence

Modular code Many changes Refactoring

NOTE: If there are no tests, you should be worried making changes!

When do we need to write tests?

So, if the code works perfectly, and Im never going to change it, then why should I write tests?

Realization 2:
If you write tests before you write your code, you end up writing better code.

If I write my test first:

It forces me to think before I code Code is more focused and clear I know when Im finished, because all the tests pass

The TDD process

Write specifications Write tests Write code

The TDD process


RED

GREEN

REFACTOR

The TDD process


Write the tests

Write code until tests pass

Try to optimize the code

When do we need to write tests?

So, if the code works perfectly, and Im never going to change it, and the design is already flawless, then why should I write tests?

Realization 3:
Well written tests are just as good, if not better than documentation.
NOTE: The real example is a better explanation than documents or comments.

Yoga Instructions
1.

2.
3. 4. 5.

Kneel down Put your hands flat in the ground Lift your feet off the ground and balance on your hands Put your knees ahead of your hands Stretch out your back so your body is perpendicular to the ground

Yoga Instructions

Yoga Instructions

When do we need to write tests?


So, if the code works perfectly, and Im never going to change it, and the design is already flawless and it is totally readable and understandable, then why should I write tests?

The moral

Learning Test Driven Development

The Goal

Software Test Hierarchy


Black Box
Integrated Tests

White Box
Unit Tests

Acceptance Tests

Endurance Tests

Stress Tests

nUnit C# jUnit Java bMock C++

FIT Framework for Integrated Tests

Example
Need an action which: submits a credit card Specifications:

The user can submit his credit card The server attempts to charge the credit card On failure, display an error message On failure, render a credit card form again On success, store the transaction On success, redirect to the receipt page

Example: Credit Card Submition


TEST(CREDIT_CARD_SUBMITION) { MY_CREDIT_CARD = 1234 5678 1234 5678; TEST_NO_THROW(status = server.submitCreditCard(MY_CREDIT_CARD) ); ASSERT.EQUAL(STATUS.SUBMITTED, status); ASSERT.EQUAL(MY_CREDIT_CARD, server.getCreditCard()); }

Example: Charge Credit Card


TEST(CHARGE_CREDIT_CARD) { MY_CREDIT_CARD = 1234 5678 1234 5678;

TEST_NO_THROW(status = server.chargeCreditCard(MY_CREDIT_CARD) );
ASSERT.EQUAL(STATUS.CHARGED, status);

Example: Charge Credit Card


TEST(CHARGE_CREDIT_CARD) { MY_CREDIT_CARD = 1234 5678 1234 5678;

RECORD_MOCK(status = STATUS.CHARGED, server.chargeCreditCard(MY_CREDIT_CARD));


TEST_NO_THROW(status = server.chargeCreditCard(MY_CREDIT_CARD) ); ASSERT.EQUAL(STATUS.CHARGED, status); }

Test Driven Development

Inspect + Adapt

Test Driven Development

Apply Inspect

Adapt

Test Driven Development


Apply Inspect Adapt

If you havent done it first, you dont know what you are talking about!

You might also like