SlideShare una empresa de Scribd logo
1 de 12
Descargar para leer sin conexión
Test Driven
Development
    Test-driven development (TDD) is a software development process that
    relies on the repetition of a very short development cycle: first the
    developer writes an (initially failing) automated test case that defines a
    new function, then produces the minimum amount of code to pass that
    test, and finally refactors the new code to acceptable standards
Test Driven Development

Contents
Test Driven Development: ..................................................................................................................... 2
   Removing Dollar Side Effects: ........................................................................................................... 5
   Equality for All: ................................................................................................................................... 6
   Privacy: ............................................................................................................................................... 7
   Franc-ly Speaking: .............................................................................................................................. 8
   Equality for All, Redux: ...................................................................................................................... 9
   Comparing Dollars and Francs:........................................................................................................ 11




                                                                                                                                                  Page 1
Test Driven Development

Test Driven Development:
Test-driven development (TDD) is a software development process that relies on the repetition
of a very short development cycle: first the developer writes an (initially failing) automated test
case that defines a new function, then produces the minimum amount of code to pass that test,
and finally refactors the new code to acceptable standards i.e.,

    1.   Quickly Add A Test
    2.   Run all the tests and see the new test fail
    3.   Change the Code
    4.   Run all the tests and see them all succeed
    5.   Refactor to remove duplication

Let’s discuss with one small example of multiplication:

Public void Multiplication ()
{
        Dollar Five=new Dollar (5);
        Five.times (2);
        AssertEquals (10, Five.amount);
}

If we try running this test, it doesn’t even compile. It’s easy to fix. We have for Compilation
Errors:
     No class Dollar
     No Constructor
     No method times (int)
     No field amount

Now start resolving one at a time:

We can come out of one error by defining a Class Dollar

Class Dollar

If we compile now, we will have only three errors. Now we need a Parameterized Constructor
Dollar (int amount)
{
}

If we compile now, we will have only two errors. Now we need a method times (int multiplier)

Void times (int multiplier)
{
}

If we compile now, we will have only one error. Now we need an amount field

Int amount;

Now we don’t have any compilation errors but if we run this we will see one error noticed that
we are expecting ‘10’ as a result, instead we saw ‘0’.



                                                                                                  Page 2
Test Driven Development

Now are not going to like this solution, but our immediate goal is not to get the perfect answer
but to pass the test.

Let’s make small change in the above code. This could pass our test

Int amount=10;

If we run this modified code, the test will pass however. But this is not a generalized code.

We can pass the test by the following code also

Int amount=5*2;

Here the code will be changed like this

Int amount;
Void times (int multiplier)
{
Amount =5*2;
}

The test still passes.

TDD is not about taking tiny steps, it’s about able to take tiny steps.

From the code we can have one question

Where can we get the value ‘5’? That was passed to the constructor. So we have to save it in the
‘amount’ variable

So the code will be like this

Dollar (int amount)
{
         This.amount=amount;
}

Then we can use this amount variable in times method

Then the code will be like this

Void times (int multiplier)
{
        Amount=amount*2;
}

Here ‘2’ is the multiplier. Then we can replace the value ‘2’ by variable ‘multiplier’

Then the code will be like this



Void times (int multiplier)
{


                                                                                                Page 3
Test Driven Development

Amount=amount*multiplier;
}

In short we replace

“Amount=amount*multiplier” by “amount*=multiplier”

Now the refactored code will be

Void times (int multiplier)
{
Amount*=multiplier;
}

The Complete Code is

Class Dollar
{
        Int amount;
        Dollar (int amount)
        {
                 This.amount=amount;
        }
        Void times (int multiplier)
        {
                 Amount*=multiplier;
        }
}

We can use/call the method like

Void Multiplication
{
       Dollar Five=new dollar (5)
       Five.times (3);
       AssertEquals (15,five.amount);
       Dollar Three=new dollar (3)
       Three.times (3);
       AssertEquals (9,three.amount);

}




                                                        Page 4
Test Driven Development

Removing Dollar Side Effects:
Consider the below case

Void Multiplication
{
       Dollar Five=new dollar (5)
       Five.times (2);
       AssertEquals (10, Five.amount);
       Five.times (3);
       AssertEquals (15, Five.amount);
}

In this case, the second assert statement will because after the first call to times (), five isn’t five
anymore. It’s actually 10. To make the second assert statement pass we need to return a Dollar
object instead of a value.

This can be achieved as follows

Void Multiplication
{
       Dollar Five=new dollar (5)
       Dollar Product = Five.times (2);
       AssertEquals (10, Product.amount);
       Product = Five.times (3);
       AssertEquals (15, Product.amount);
}

The above test will not compile because our times () is returning value. To compile the test we
need to change the times () function as follows:

Dollar times (int multiplier)
{
         Return new Dollar (amount*multiplier);
}




                                                                                                  Page 5
Test Driven Development

Equality for All:
If I have an integer and I add 1 to it, I don’t expect the original integer to change; I expect to use
the new value. Objects usually don’t behave that way.

We can use objects as values, as we are using our Dollar now. The pattern for this is Value Object.
One of the constraints on Value Objects is that the values of the instance variables of the object
never change once they have been set in the constructor.

There is one huge advantage to using value objects; you don’t have to worry about aliasing
problems. Say I have one Check and I set its amount to $5, and then I set another Check’s amount
to the same $5. Some of the worst bugs in my career have come when changing the first Check’s
value inadvertently changed the second Check’s value. This is aliasing.
Public void testEquality ()
{
        AssertTrue (new Dollar (5).equals (new Dollar (5)));
}

Now we have to define equals () method with the fake result to make the code compiled

Public Boolean equals (Object object)
{
        Return true;
}

Now we have to refactor the equals () method.

Public Boolean equals (Object object)
{
        Dollar dollar = (Dollar) object;
        Return amount == dollar.amount;
}

This is the generalized code

This results the correct output for the following code

Public void testEquality ()
{
        AssertTrue (new Dollar (5).equals (new Dollar (5)));
        AssertTrue (new Dollar (5).equals (new Dollar (3)));
}




                                                                                                 Page 6
Test Driven Development

Privacy:

From the above section (Equality for All), the below can we be refactored

Void DollarMultiplication
{
       Dollar Five=new dollar (5)
       Dollar Product = Five.times (2);
       AssertEquals (10, Product.amount);
       Product = Five.times (3);
       AssertEquals (15, Product.amount);
}

We can refactor the above code like

Void DollarMultiplication
{
       Dollar Five=new dollar (5)
       Dollar Product = Five.times (2);
       AssertEquals (New Dollar (10), Product);
       Product = Five.times (3);
       AssertEquals (New Dollar (15), Product);
}

We can even refactor the code by removing the temporary variable ‘Product’



Void DollarMultiplication
{
       Dollar Five=new dollar (5)
       AssertEquals (New Dollar (10), Five.times (2));
       AssertEquals (New Dollar (15), Five.times (2));
}

With these changes, the ‘Amount’ variable is only used by the instances of ‘Dollar’ class. So we
can make the Amount variable as ‘Private’

Private int Amount;




                                                                                            Page 7
Test Driven Development

Franc-ly Speaking:

If we can get the object franc to work the way the object dollar works now, we’ll be closer to
being able to write and run the mixed addition test.

Let’s write the above DollarMultiplication code for FrancMultiplication

Void FrancMultiplication
{
        Franc Five=new dollar (5)
        AssertEquals (New Franc (10), Five.times (2));
        AssertEquals (New Franc (15), Five.times (2));
}

So now we got error again. So we have to follow all the above steps. Then finally we get the
below code

Class Franc
{
        Private int Amount;
        Franc times (int multiplier)
        {
                Return new Franc (Amount*multiplier);
        }

       Public Boolean equals (Object object)
       {
               Franc franc = (Franc) object;
               Return amount == franc.Amount;
       }
}




                                                                                            Page 8
Test Driven Development

Equality for All, Redux:

Now we have the same function equals () for different objects (Dollar, Franc). We need to
generalize the equals () method which should be applicable for any objects.

Actually we got a new test case for the object Franc. But it is similar to the Object Dollar. It is
achieved just by Copying and Pasting the code. Now we have to clean up the code so that we
make use of single function for various objects.

One possibility is to make one of our Class extends the other. Instead, we are going to find a
common super class for the two classes.




Now from the above diagram, we have to create a superclass (i.e., Money), and then we have to
create classes ‘Dollar’ and ‘Franc’ which extends ‘Money’ class.

Class Money
{
}

Now we have to Create Dollar class by extending to Money

Class Dollar extends Money
{
        Private Int Amount;
}

Will it run? No, the test still run but we have to declare the Amount Variable in ‘Money’ class as
protected. Like,

Class Money
{
       Protected int Amount;
}

Now no need of declaring the ‘Amount’ Variable in ‘Dollar’ and ‘Franc’ classes as it this variable it
made available from ‘Money’ Class

Now the class declarations will be

Class Dollar extends Money
{
}

And



                                                                                                Page 9
Test Driven Development

Class Dollar extends Money
{
}

Now we have to work on equals () method in ‘Dollar’ class. Firstly we have to create a temporary
variable of type ‘Money’ instead of ‘Dollar’

Dollar:
Public Boolean equals (Object object)
{
        Money dollar = (Dollar) object;
        Return amount == dollar.amount;
}

Now we have change the cast

Dollar:
Public Boolean equals (Object object)
{
        Money dollar = (Money) object;
        Return amount == dollar.amount;
}

Now we have to change the variable name

Dollar:
Public Boolean equals (Object object)
{
        Money money = (Money) object;
        Return amount == money.amount;
}

Finally we have to move the equals function from Dollar class to Money Class

Money:
Public Boolean equals (Object object)
{
        Money money = (Money) object;
        Return amount == money.amount;
}

The same thing is applicable in Franc Class also. So we have reduced the plenty of code till now.
Now we can use the same equals () method of Money class for both Dollar and Franc Class

Public Void testEquality ()
{
        AssertTrue (new Dollar (5).Equals (new Dollar (5)));
        AssertTrue (new Dollar (5).Equals (new Dollar (6)));
        AssertTrue (new Franc (5).Equals (new Franc (5)));
        AssertTrue (new Franc (5).Equals (new Franc (6)));
}




                                                                                           Page 10
Test Driven Development

Comparing Dollars and Francs:

Now we got another problem. What happens when we compare Franc with Dollar?

Public Void testEquality ()
{
        AssertTrue (new Dollar (5).Equals (new Dollar (5)));
        AssertTrue (new Dollar (5).Equals (new Dollar (6)));
        AssertTrue (new Franc (5).Equals (new Franc (5)));
        AssertTrue (new Franc (5).Equals (new Franc (6)));
        AssertTrue (new Franc (5).Equals (new Dollar (5)));
        AssertTrue (new Dollar (5).Equals (new Franc (5)));
}

It results fail. The result is true when we compare 5 Dollars with 5 Francs, which should not. Now
we have to fix this code in the equals () method of Money class. equals () method needs to check
that whether it is comparing Dollars with Francs and vice versa. We can fix this by comparing
class type of two objects.

It means two Moneys are equal only when the amount and Amount Types are equal.

So, we have to modify the code as follows

Money:
Public Boolean equals (Object object)
{
        Money money = (Money) object;
        Return amount == money.amount && getClass (). Equals (money.getClass ());
}




                                                                                           Page 11

Más contenido relacionado

La actualidad más candente

Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentalsumar78600
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Neeru Mittal
 
Algorithm and Programming (Procedure and Function)
Algorithm and Programming (Procedure and Function)Algorithm and Programming (Procedure and Function)
Algorithm and Programming (Procedure and Function)Adam Mukharil Bachtiar
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#Giorgio Zoppi
 
Learn VbScript -String Functions
Learn VbScript -String FunctionsLearn VbScript -String Functions
Learn VbScript -String FunctionsNilanjan Saha
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
Javascript conditional statements
Javascript conditional statementsJavascript conditional statements
Javascript conditional statementsnobel mujuji
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsDhivyaSubramaniyam
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio Zoppi
 

La actualidad más candente (20)

Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
Function
FunctionFunction
Function
 
Tdd.eng.ver
Tdd.eng.verTdd.eng.ver
Tdd.eng.ver
 
C# Loops
C# LoopsC# Loops
C# Loops
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 
Algorithm and Programming (Procedure and Function)
Algorithm and Programming (Procedure and Function)Algorithm and Programming (Procedure and Function)
Algorithm and Programming (Procedure and Function)
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#
 
Learn VbScript -String Functions
Learn VbScript -String FunctionsLearn VbScript -String Functions
Learn VbScript -String Functions
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Vbscript
VbscriptVbscript
Vbscript
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Javascript conditional statements
Javascript conditional statementsJavascript conditional statements
Javascript conditional statements
 
Python unit 3 and Unit 4
Python unit 3 and Unit 4Python unit 3 and Unit 4
Python unit 3 and Unit 4
 
Clean code
Clean codeClean code
Clean code
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
 
VB Script
VB ScriptVB Script
VB Script
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrency
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 

Destacado

Painttex presentation
Painttex presentationPainttex presentation
Painttex presentationRajesh Baru
 
CUSTMZ 4 CUSTO - Customise your digital magazine
CUSTMZ 4 CUSTO - Customise your digital magazineCUSTMZ 4 CUSTO - Customise your digital magazine
CUSTMZ 4 CUSTO - Customise your digital magazineCUSTMZ
 
CUSTMZ FOR BVIC - Digital magazines for internal communication
CUSTMZ FOR BVIC - Digital magazines for internal communicationCUSTMZ FOR BVIC - Digital magazines for internal communication
CUSTMZ FOR BVIC - Digital magazines for internal communicationCUSTMZ
 
Building and Sustaining a Business Development Culture _ Law Practice Division
Building and Sustaining a Business Development Culture _ Law Practice DivisionBuilding and Sustaining a Business Development Culture _ Law Practice Division
Building and Sustaining a Business Development Culture _ Law Practice DivisionTea Hoffmann
 
VodacoSocial Real-Time Marketing Agency
VodacoSocial Real-Time Marketing AgencyVodacoSocial Real-Time Marketing Agency
VodacoSocial Real-Time Marketing AgencyVodacoSocial
 
Ecer2014-The changes of digital inclusion in rural communities
Ecer2014-The changes of digital inclusion in rural communities Ecer2014-The changes of digital inclusion in rural communities
Ecer2014-The changes of digital inclusion in rural communities luisa aires
 
The cause of american financial crisis
The cause of american financial crisisThe cause of american financial crisis
The cause of american financial crisismanibosca
 
CUSTMZ 4 Vacature
CUSTMZ 4 VacatureCUSTMZ 4 Vacature
CUSTMZ 4 VacatureCUSTMZ
 
The Direct Method in Language Teaching
The Direct Method in Language TeachingThe Direct Method in Language Teaching
The Direct Method in Language TeachingSoner Kalan
 

Destacado (16)

Painttex presentation
Painttex presentationPainttex presentation
Painttex presentation
 
CUSTMZ 4 CUSTO - Customise your digital magazine
CUSTMZ 4 CUSTO - Customise your digital magazineCUSTMZ 4 CUSTO - Customise your digital magazine
CUSTMZ 4 CUSTO - Customise your digital magazine
 
CUSTMZ FOR BVIC - Digital magazines for internal communication
CUSTMZ FOR BVIC - Digital magazines for internal communicationCUSTMZ FOR BVIC - Digital magazines for internal communication
CUSTMZ FOR BVIC - Digital magazines for internal communication
 
Google chrome
Google chromeGoogle chrome
Google chrome
 
Building and Sustaining a Business Development Culture _ Law Practice Division
Building and Sustaining a Business Development Culture _ Law Practice DivisionBuilding and Sustaining a Business Development Culture _ Law Practice Division
Building and Sustaining a Business Development Culture _ Law Practice Division
 
VodacoSocial Real-Time Marketing Agency
VodacoSocial Real-Time Marketing AgencyVodacoSocial Real-Time Marketing Agency
VodacoSocial Real-Time Marketing Agency
 
Ecer2014-The changes of digital inclusion in rural communities
Ecer2014-The changes of digital inclusion in rural communities Ecer2014-The changes of digital inclusion in rural communities
Ecer2014-The changes of digital inclusion in rural communities
 
Screenchot of chrome os
Screenchot of chrome osScreenchot of chrome os
Screenchot of chrome os
 
Bestessays pres
Bestessays presBestessays pres
Bestessays pres
 
Triatló power point
Triatló power pointTriatló power point
Triatló power point
 
The cause of american financial crisis
The cause of american financial crisisThe cause of american financial crisis
The cause of american financial crisis
 
Introduction to cryptography
Introduction to cryptographyIntroduction to cryptography
Introduction to cryptography
 
Verification and validation
Verification and validationVerification and validation
Verification and validation
 
Selenium Handbook
Selenium HandbookSelenium Handbook
Selenium Handbook
 
CUSTMZ 4 Vacature
CUSTMZ 4 VacatureCUSTMZ 4 Vacature
CUSTMZ 4 Vacature
 
The Direct Method in Language Teaching
The Direct Method in Language TeachingThe Direct Method in Language Teaching
The Direct Method in Language Teaching
 

Similar a Test driven development

CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxmonicafrancis71118
 
OverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docxOverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docxgerardkortney
 
Basic c# cheat sheet
Basic c# cheat sheetBasic c# cheat sheet
Basic c# cheat sheetAhmed Elshal
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 
Visual Basic Review - ICA
Visual Basic Review - ICAVisual Basic Review - ICA
Visual Basic Review - ICAemtrajano
 
Javascript breakdown-workbook
Javascript breakdown-workbookJavascript breakdown-workbook
Javascript breakdown-workbookHARUN PEHLIVAN
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for KidsAimee Maree Forsstrom
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structuresMicheal Ogundero
 
CMIS 102 WEEK 5 HANDS-ON LAB
CMIS 102 WEEK 5 HANDS-ON LABCMIS 102 WEEK 5 HANDS-ON LAB
CMIS 102 WEEK 5 HANDS-ON LABHamesKellor
 
Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8Frédéric Delorme
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test communityKerry Buckley
 
Build tic tac toe with javascript (4:11 dc)
Build tic tac toe with javascript (4:11 dc)Build tic tac toe with javascript (4:11 dc)
Build tic tac toe with javascript (4:11 dc)Daniel Friedman
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while LoopJayBhavsar68
 

Similar a Test driven development (20)

CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
 
03b loops
03b   loops03b   loops
03b loops
 
OverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docxOverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docx
 
Basic c# cheat sheet
Basic c# cheat sheetBasic c# cheat sheet
Basic c# cheat sheet
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Visual Basic Review - ICA
Visual Basic Review - ICAVisual Basic Review - ICA
Visual Basic Review - ICA
 
Javascript breakdown-workbook
Javascript breakdown-workbookJavascript breakdown-workbook
Javascript breakdown-workbook
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Vb script tutorial
Vb script tutorialVb script tutorial
Vb script tutorial
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Loops
LoopsLoops
Loops
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structures
 
CMIS 102 WEEK 5 HANDS-ON LAB
CMIS 102 WEEK 5 HANDS-ON LABCMIS 102 WEEK 5 HANDS-ON LAB
CMIS 102 WEEK 5 HANDS-ON LAB
 
Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test community
 
Build tic tac toe with javascript (4:11 dc)
Build tic tac toe with javascript (4:11 dc)Build tic tac toe with javascript (4:11 dc)
Build tic tac toe with javascript (4:11 dc)
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 

Último

APRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfAPRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfRbc Rbcua
 
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu MenzaYouth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menzaictsugar
 
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncr
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / NcrCall Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncr
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncrdollysharma2066
 
Kenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby AfricaKenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby Africaictsugar
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchirictsugar
 
Case study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailCase study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailAriel592675
 
Memorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMMemorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMVoces Mineras
 
Market Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMarket Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMintel Group
 
India Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample ReportIndia Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample ReportMintel Group
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfpollardmorgan
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCRashishs7044
 
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCRashishs7044
 
Islamabad Escorts | Call 03070433345 | Escort Service in Islamabad
Islamabad Escorts | Call 03070433345 | Escort Service in IslamabadIslamabad Escorts | Call 03070433345 | Escort Service in Islamabad
Islamabad Escorts | Call 03070433345 | Escort Service in IslamabadAyesha Khan
 
Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Seta Wicaksana
 
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607dollysharma2066
 
Investment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy CheruiyotInvestment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy Cheruiyotictsugar
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Servicecallgirls2057
 
8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCRashishs7044
 

Último (20)

APRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfAPRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdf
 
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu MenzaYouth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
 
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncr
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / NcrCall Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncr
Call Girls in DELHI Cantt, ( Call Me )-8377877756-Female Escort- In Delhi / Ncr
 
Enjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCR
Enjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCREnjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCR
Enjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCR
 
Kenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby AfricaKenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby Africa
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchir
 
Corporate Profile 47Billion Information Technology
Corporate Profile 47Billion Information TechnologyCorporate Profile 47Billion Information Technology
Corporate Profile 47Billion Information Technology
 
Case study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailCase study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detail
 
Memorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMMemorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQM
 
Market Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMarket Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 Edition
 
India Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample ReportIndia Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample Report
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
 
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
 
Islamabad Escorts | Call 03070433345 | Escort Service in Islamabad
Islamabad Escorts | Call 03070433345 | Escort Service in IslamabadIslamabad Escorts | Call 03070433345 | Escort Service in Islamabad
Islamabad Escorts | Call 03070433345 | Escort Service in Islamabad
 
Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...
 
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
 
Investment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy CheruiyotInvestment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy Cheruiyot
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
 
8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR
 

Test driven development

  • 1. Test Driven Development Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards
  • 2. Test Driven Development Contents Test Driven Development: ..................................................................................................................... 2 Removing Dollar Side Effects: ........................................................................................................... 5 Equality for All: ................................................................................................................................... 6 Privacy: ............................................................................................................................................... 7 Franc-ly Speaking: .............................................................................................................................. 8 Equality for All, Redux: ...................................................................................................................... 9 Comparing Dollars and Francs:........................................................................................................ 11 Page 1
  • 3. Test Driven Development Test Driven Development: Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards i.e., 1. Quickly Add A Test 2. Run all the tests and see the new test fail 3. Change the Code 4. Run all the tests and see them all succeed 5. Refactor to remove duplication Let’s discuss with one small example of multiplication: Public void Multiplication () { Dollar Five=new Dollar (5); Five.times (2); AssertEquals (10, Five.amount); } If we try running this test, it doesn’t even compile. It’s easy to fix. We have for Compilation Errors:  No class Dollar  No Constructor  No method times (int)  No field amount Now start resolving one at a time: We can come out of one error by defining a Class Dollar Class Dollar If we compile now, we will have only three errors. Now we need a Parameterized Constructor Dollar (int amount) { } If we compile now, we will have only two errors. Now we need a method times (int multiplier) Void times (int multiplier) { } If we compile now, we will have only one error. Now we need an amount field Int amount; Now we don’t have any compilation errors but if we run this we will see one error noticed that we are expecting ‘10’ as a result, instead we saw ‘0’. Page 2
  • 4. Test Driven Development Now are not going to like this solution, but our immediate goal is not to get the perfect answer but to pass the test. Let’s make small change in the above code. This could pass our test Int amount=10; If we run this modified code, the test will pass however. But this is not a generalized code. We can pass the test by the following code also Int amount=5*2; Here the code will be changed like this Int amount; Void times (int multiplier) { Amount =5*2; } The test still passes. TDD is not about taking tiny steps, it’s about able to take tiny steps. From the code we can have one question Where can we get the value ‘5’? That was passed to the constructor. So we have to save it in the ‘amount’ variable So the code will be like this Dollar (int amount) { This.amount=amount; } Then we can use this amount variable in times method Then the code will be like this Void times (int multiplier) { Amount=amount*2; } Here ‘2’ is the multiplier. Then we can replace the value ‘2’ by variable ‘multiplier’ Then the code will be like this Void times (int multiplier) { Page 3
  • 5. Test Driven Development Amount=amount*multiplier; } In short we replace “Amount=amount*multiplier” by “amount*=multiplier” Now the refactored code will be Void times (int multiplier) { Amount*=multiplier; } The Complete Code is Class Dollar { Int amount; Dollar (int amount) { This.amount=amount; } Void times (int multiplier) { Amount*=multiplier; } } We can use/call the method like Void Multiplication { Dollar Five=new dollar (5) Five.times (3); AssertEquals (15,five.amount); Dollar Three=new dollar (3) Three.times (3); AssertEquals (9,three.amount); } Page 4
  • 6. Test Driven Development Removing Dollar Side Effects: Consider the below case Void Multiplication { Dollar Five=new dollar (5) Five.times (2); AssertEquals (10, Five.amount); Five.times (3); AssertEquals (15, Five.amount); } In this case, the second assert statement will because after the first call to times (), five isn’t five anymore. It’s actually 10. To make the second assert statement pass we need to return a Dollar object instead of a value. This can be achieved as follows Void Multiplication { Dollar Five=new dollar (5) Dollar Product = Five.times (2); AssertEquals (10, Product.amount); Product = Five.times (3); AssertEquals (15, Product.amount); } The above test will not compile because our times () is returning value. To compile the test we need to change the times () function as follows: Dollar times (int multiplier) { Return new Dollar (amount*multiplier); } Page 5
  • 7. Test Driven Development Equality for All: If I have an integer and I add 1 to it, I don’t expect the original integer to change; I expect to use the new value. Objects usually don’t behave that way. We can use objects as values, as we are using our Dollar now. The pattern for this is Value Object. One of the constraints on Value Objects is that the values of the instance variables of the object never change once they have been set in the constructor. There is one huge advantage to using value objects; you don’t have to worry about aliasing problems. Say I have one Check and I set its amount to $5, and then I set another Check’s amount to the same $5. Some of the worst bugs in my career have come when changing the first Check’s value inadvertently changed the second Check’s value. This is aliasing. Public void testEquality () { AssertTrue (new Dollar (5).equals (new Dollar (5))); } Now we have to define equals () method with the fake result to make the code compiled Public Boolean equals (Object object) { Return true; } Now we have to refactor the equals () method. Public Boolean equals (Object object) { Dollar dollar = (Dollar) object; Return amount == dollar.amount; } This is the generalized code This results the correct output for the following code Public void testEquality () { AssertTrue (new Dollar (5).equals (new Dollar (5))); AssertTrue (new Dollar (5).equals (new Dollar (3))); } Page 6
  • 8. Test Driven Development Privacy: From the above section (Equality for All), the below can we be refactored Void DollarMultiplication { Dollar Five=new dollar (5) Dollar Product = Five.times (2); AssertEquals (10, Product.amount); Product = Five.times (3); AssertEquals (15, Product.amount); } We can refactor the above code like Void DollarMultiplication { Dollar Five=new dollar (5) Dollar Product = Five.times (2); AssertEquals (New Dollar (10), Product); Product = Five.times (3); AssertEquals (New Dollar (15), Product); } We can even refactor the code by removing the temporary variable ‘Product’ Void DollarMultiplication { Dollar Five=new dollar (5) AssertEquals (New Dollar (10), Five.times (2)); AssertEquals (New Dollar (15), Five.times (2)); } With these changes, the ‘Amount’ variable is only used by the instances of ‘Dollar’ class. So we can make the Amount variable as ‘Private’ Private int Amount; Page 7
  • 9. Test Driven Development Franc-ly Speaking: If we can get the object franc to work the way the object dollar works now, we’ll be closer to being able to write and run the mixed addition test. Let’s write the above DollarMultiplication code for FrancMultiplication Void FrancMultiplication { Franc Five=new dollar (5) AssertEquals (New Franc (10), Five.times (2)); AssertEquals (New Franc (15), Five.times (2)); } So now we got error again. So we have to follow all the above steps. Then finally we get the below code Class Franc { Private int Amount; Franc times (int multiplier) { Return new Franc (Amount*multiplier); } Public Boolean equals (Object object) { Franc franc = (Franc) object; Return amount == franc.Amount; } } Page 8
  • 10. Test Driven Development Equality for All, Redux: Now we have the same function equals () for different objects (Dollar, Franc). We need to generalize the equals () method which should be applicable for any objects. Actually we got a new test case for the object Franc. But it is similar to the Object Dollar. It is achieved just by Copying and Pasting the code. Now we have to clean up the code so that we make use of single function for various objects. One possibility is to make one of our Class extends the other. Instead, we are going to find a common super class for the two classes. Now from the above diagram, we have to create a superclass (i.e., Money), and then we have to create classes ‘Dollar’ and ‘Franc’ which extends ‘Money’ class. Class Money { } Now we have to Create Dollar class by extending to Money Class Dollar extends Money { Private Int Amount; } Will it run? No, the test still run but we have to declare the Amount Variable in ‘Money’ class as protected. Like, Class Money { Protected int Amount; } Now no need of declaring the ‘Amount’ Variable in ‘Dollar’ and ‘Franc’ classes as it this variable it made available from ‘Money’ Class Now the class declarations will be Class Dollar extends Money { } And Page 9
  • 11. Test Driven Development Class Dollar extends Money { } Now we have to work on equals () method in ‘Dollar’ class. Firstly we have to create a temporary variable of type ‘Money’ instead of ‘Dollar’ Dollar: Public Boolean equals (Object object) { Money dollar = (Dollar) object; Return amount == dollar.amount; } Now we have change the cast Dollar: Public Boolean equals (Object object) { Money dollar = (Money) object; Return amount == dollar.amount; } Now we have to change the variable name Dollar: Public Boolean equals (Object object) { Money money = (Money) object; Return amount == money.amount; } Finally we have to move the equals function from Dollar class to Money Class Money: Public Boolean equals (Object object) { Money money = (Money) object; Return amount == money.amount; } The same thing is applicable in Franc Class also. So we have reduced the plenty of code till now. Now we can use the same equals () method of Money class for both Dollar and Franc Class Public Void testEquality () { AssertTrue (new Dollar (5).Equals (new Dollar (5))); AssertTrue (new Dollar (5).Equals (new Dollar (6))); AssertTrue (new Franc (5).Equals (new Franc (5))); AssertTrue (new Franc (5).Equals (new Franc (6))); } Page 10
  • 12. Test Driven Development Comparing Dollars and Francs: Now we got another problem. What happens when we compare Franc with Dollar? Public Void testEquality () { AssertTrue (new Dollar (5).Equals (new Dollar (5))); AssertTrue (new Dollar (5).Equals (new Dollar (6))); AssertTrue (new Franc (5).Equals (new Franc (5))); AssertTrue (new Franc (5).Equals (new Franc (6))); AssertTrue (new Franc (5).Equals (new Dollar (5))); AssertTrue (new Dollar (5).Equals (new Franc (5))); } It results fail. The result is true when we compare 5 Dollars with 5 Francs, which should not. Now we have to fix this code in the equals () method of Money class. equals () method needs to check that whether it is comparing Dollars with Francs and vice versa. We can fix this by comparing class type of two objects. It means two Moneys are equal only when the amount and Amount Types are equal. So, we have to modify the code as follows Money: Public Boolean equals (Object object) { Money money = (Money) object; Return amount == money.amount && getClass (). Equals (money.getClass ()); } Page 11