Top Banner
Test-Driven Development by John Blanco
21

Test Driven Development

Jan 20, 2015

Download

Technology

John Blanco

An introduction to TDD methodology that dispells some myths about unit testing in general.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Test Driven Development

Test-Driven DevelopmentTest-Driven

Developmentby John Blanco

Page 2: Test Driven Development

The process of designing software by writing a validating client before the code.

What is Test-Driven Development (TDD)?What is Test-Driven Development (TDD)?

Maintaining an evolving set of unit tests that continuously validate the correctness (or not) of your code.

What is Automated Unit Testing?What is Automated Unit Testing?

Page 3: Test Driven Development

Automated Unit Testing != Test-Driven DevelopmentAutomated Unit Testing != Test-Driven Development

TDD is a way of thinking about your code, but it doesn’t require unit testing. That being said, you should write them if you can.

Agile processes adapt to change. Unit tests help to manage change in our code.

Unit Testing is a key component of the Agile MethodologyUnit Testing is a key component of the Agile Methodology

Page 4: Test Driven Development

Dispelling Unit Testing MythsDispelling Unit Testing Myths

Page 5: Test Driven Development

No. Writing unit tests is an investment towards time savings over the lifetime of the project.

Myth #1: Won’t writing unit tests double by development time?Myth #1: Won’t writing unit tests double by development time?

• We won’t need to constantly validate our code each time we make changes.

• Finding the bugs early means far less time fixing them later.

• If our unit tests are good, we won’t need to spend hours or days tracking down bugs in the most fundamental layers of our code.

Page 6: Test Driven Development

No. Unit tests are not parallel to your code like documentation. You will be driving your code with them.

Myth #2: Won’t all my unit tests just get out of date?Myth #2: Won’t all my unit tests just get out of date?

• TDD is a programming paradigm.

• Unit tests are not an artifact of your code. They represent how you write it!

• However, if you don’t appreciate unit testing, they likely will not be worth the time to create.

Page 7: Test Driven Development

No. The client is paying for clean, correct code. Unit testing is the means to that end.

Myth #3: Won’t the client refuse to pay for it?Myth #3: Won’t the client refuse to pay for it?

• Do they pay us to test our code?

• Do they pay us to use design patterns?

• Do they pay us to use UML?

Page 8: Test Driven Development

xUnit is the framework for automating our unit tests.

Implementations exist for C++, C#, Java, PHP, Ruby, Python, Perl, Prolog, LISP, … and Flex!

Created by Erich Gamma, member of the GoF and co-author of Eclipse JDT.

Introducing xUnit!Introducing xUnit!

Page 9: Test Driven Development

FlexUnit is the Flex implementation of xUnit.

FlexUnitFlexUnit

Tests you will be performing on your code: • assertEquals()• assertTrue()• assertNotUndefined()• assertStrictlyEquals()• …

Page 10: Test Driven Development

Don’t worry. Chances are, you’re already writing unit tests!

Whoa! Whoa! Hold On -- This Seems Excessive!Whoa! Whoa! Hold On -- This Seems Excessive!

Page 11: Test Driven Development

<mx:Application creationComplete=“onCreationComplete()”> <mx:Script> private function onCreationComplete():void { // DEVELOPMENT ONLY - REMOVE LATER var htmlMaker:MyHTMLMaker = new MyHTMLMaker();

htmlMaker.startTag(“b”); htmlMaker.addContent(“My bold text!!!”); htmlMaker.endTag();

trace(“RESULT: ” + htmlMaker.toString()); } </mx:Script>

. . . </mx:Application>

Ever Write Code Like This…And Then Delete It Afterwards?Ever Write Code Like This…And Then Delete It Afterwards?

Page 12: Test Driven Development

Fool! The problem is that you’re not anticipating CHANGE. When that happens, you will need to modify and test your code all over again.

What if we kept that test?

Yes! And then once I verify it’s working, I remove it. So?Yes! And then once I verify it’s working, I remove it. So?

Page 13: Test Driven Development

There are two ways we can organize our unit tests:

• Inside the project• Outside the project

My personal preference is outside of the project. Here’s why:

• Keeps our code and unit tests physically separate.• Avoids the double-build conundrum.• Allows us to close the unit test project when desirable.• Allows us to run tests against projects in multiple locations.

Setting Up Your Project For TDD Setting Up Your Project For TDD

http://raptureinvenice.blogspot.com/2008/04/better-way-to-set-up-flexunit.html

Reference:

Page 14: Test Driven Development

• Create a Flex or AIR project. (e.g., twitterme)

• Create a Flex or AIR unit testing project. (e.g., twitterme-tests)

• Add twitterme/src to the source path of twitterme-tests.

• Add a boilerplate TestRunner application to twitterme-tests.

• Add flexunit.swc to twitterme-tests/libs.

Setting Up Your Project For TDD Setting Up Your Project For TDD

http://code.google.com/p/as3flexunitlib/

References:

http://raptureinvenice.blogspot.com/2008/04/better-way-to-set-up-flexunit.html

Page 15: Test Driven Development

Working SessionWorking Session

Page 16: Test Driven Development

Wait! We Need a Change!Wait! We Need a Change!

Page 17: Test Driven Development

What Have We Accomplished?What Have We Accomplished?

Page 18: Test Driven Development

Gain #1: We have a clean, stable piece of codeGain #1: We have a clean, stable piece of code

• We know the client code will be easy to use, because we drove the code based on the client itself!

• We have tested the code as thoroughly as we can using unit tests.

• We have kept the unit tests as an artifact of the coding effort so that we can continually re-run the tests as needed.

• Code usage is already documented for our customers!

Page 19: Test Driven Development

Gain #2: We will feel confident making changes to the codeGain #2: We will feel confident making changes to the code• If we make a change to this code, we can re-run the unit tests to verify if the code is still functionally correct - instantly!

• We don’t have to fear the major changes that will likely be made later. Because what is the one constant in software development?Because what is the one constant in software development?CHANGE.

Page 20: Test Driven Development

dpuint offers some enhancements over FlexUnit that you might find valuable. The key differences are:

• Better asynchronous support• Sequencing tests• Cairngorn tests

Highly recommended if you want to test UI.

A Newer Flex Testing Framework: dpuintA Newer Flex Testing Framework: dpuint

Page 21: Test Driven Development

Questions?Questions?