Utilisateur   Mot de passe  
Informaticien.be - Derniers blogs actifs - Liste des blogs
lemmings
Pérégrination d'un lemmings
Derniers messages
08/05/2008 @ 20:25:02: [Coldfusion]: le TDD avec CFUnit
Dans la cadre de mon travail j'ai réalisé une doc de base sur le dev en TDD avec CFUnit, je me suis dit pourquoi pas la partager avec tous le monde :wink:

par contre c'est en anglais car je travaille à la commission européenne

Usefulness of making unit tests
Just as the picture on the cover of a puzzle defines how the puzzle will look when it's finished, a unit testing framework is a definition of how the application will work when it is completed. TDD and unit tests are not new topics and are common practices in many programming languages. In TDD, a developer begins by defining how he expects his code to work, and then writes unit tests based on those expectations. The developer does not write any code until the tests are written. Once the tests are completed the developer will frequently run them as he builds an application to receive instant feedback on the code's current behaviour and progress. This can be a very different paradigm for many ColdFusion developers, who are often not accustomed to testing during the development process.

What is CFUnit
CFUnit is a unit testing framework for ColdFusion (CFML), modelled after the popular JUnit framework. The purpose of CFUnit is to validate the behaviours of your functions and methods frequently executed.


How to install CFUnit
You can download it form this web site : CFUnit
Then decompress it in your web server root directory, (ex: if you use coldfusion with his embedded web server you decompress cfunit in wwwroot).

It's done

how to make a unit test
Let's play together with CFUnit…

simple example :
for this example i create a directory "cfunittest" in the root of the web server
and inside I create 2 others directories "cfc" and "tests"
- create a page called Math.cfc in "cfc" directory
- create a cfc named Math.
- Insert a function called Sum, this function had two arguments type of "Numeric" and the result is the sum of the arguments.

You can take the example below
  1. <!---  
  2. Created by : Strodiot Pierre 
  3. Date : 07/05/2008 
  4. Comments : 
  5. this is the cfc containing all the functions used for math  
  6. ---> 
  7. <cfcomponent displayname="Math"
  8.  
  9. <!--- This function return the sum of the arguments ---> 
  10. <cffunction name="Sum" output="true" returntype="Numeric"
  11. <cfargument name="val1" type="Numeric" required="true"
  12. <cfargument name="val2" type="Numeric" required="true"
  13.  
  14. <cfreturn #val1# + #val2#> 
  15. </cffunction>  
  16.  
  17. </cfcomponent>


- then create the test page called MathTest.cfc in "tests" directory
- create a cfc named MathTest with the extends "net.sourceforge.cfunit.framework.testcase", that's depend where you installed the CFUnit framework
- as you can see, we create an object by calling the component "cfunitTest.cfc.Math"
- like when we use a component, we call the function and give parameters to the function
- we invoke the method "assertEquals" and give two arguments named "expected and actual"
- expected is the value you know is correct
- actual is the result of the function
- assertEquals is accessible by the extends

you can take the example below
  1. <!---  
  2. Created by : Strodiot Pierre 
  3. Date : 07/05/2008 
  4. Comments : 
  5. this is the cfc containing all tests on the functions used in cfc's math  
  6. ---> 
  7. <cfcomponent displayname="MathTest" extends="net.sourceforge.cfunit.framework.TestCase"
  8. <!--- Use the function "sum" to test the behave of it ---> 
  9. <cffunction name="TestSum" returntype="void" access="public"
  10. <cfset objMath = CreateObject("component","cfunitTest.cfc.Math")> 
  11. <cfset result = objMath.Sum(1,3)> 
  12. <cfinvoke method="assertEquals"
  13. <cfinvokeargument name="expected" value="4"
  14. <cfinvokeargument name="actual" value="#result#"
  15. </cfinvoke
  16. </cffunction
  17. </cfcomponent>

Now, create the cfm page called TestLauncher.cfm in "cfunittest" directory

- in this page, created a array that contain the list of test classes you want to run
- this list is passed to the component invoked by the argument called "test"

You can take the code below
  1. <cfsilent
  2. <cfset testClasses = ArrayNew(1)> 
  3. <cfset ArrayAppend(testClasses, "cfunitTest.tests.MathTest")> 
  4. <!--- Add as many test classes as you would like to the array ---> 
  5. <cfset suite = CreateObject("component", "net.sourceforge.cfunit.framework.TestSuite").inittestClasses )>  
  6. </cfsilent
  7. <cfoutput
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  9. <html> 
  10. <head> 
  11. <title>CFUnit Test</title> 
  12. </head> 
  13. <body 
  14. <cfinvoke component="net.sourceforge.cfunit.framework.TestRunner" method="run"> 
  15. <cfinvokeargument name="test" value="#suite#"> 
  16. <cfinvokeargument name="name" value="">  
  17. </cfinvoke> 
  18. </body> 
  19. </html> 
  20. </cfoutput>




Call the page TestLauncher in your browser

Here is the result in the browser

Tests : 1
Errors : 0
Failure : 0

Now if you replace the code in the page MathTest.cfc by this code
  1. <!---  
  2. Created by : Strodiot Pierre 
  3. Date : 07/05/2008 
  4. Comments : 
  5. this is the cfc containing all tests on the functions used in cfc's math  
  6. ---> 
  7. <cfcomponent displayname="MathTest" extends="net.sourceforge.cfunit.framework.TestCase"
  8. <!--- Use the function "sum" to test the behave of it ---> 
  9. <cffunction name="TestSum" returntype="void" access="public"
  10. <cfset objMath = CreateObject("component","cfunitTest.cfc.Math")> 
  11. <cfset result = objMath.Sum(1,3)> 
  12. <cfinvoke method="assertEquals"
  13. <cfinvokeargument name="expected" value="4"
  14. <cfinvokeargument name="actual" value="#result#"
  15. </cfinvoke
  16. </cffunction
  17.  
  18. <cffunction name="TestSum2" returntype="void" access="public"
  19. <cfset objMath = CreateObject("component","cfunitTest.cfc.Math")> 
  20. <cfset result = objMath.Sum(1,3)> 
  21. <cfinvoke method="assertEquals"
  22. <cfinvokeargument name="expected" value="5"
  23. <cfinvokeargument name="actual" value="#result#"
  24. </cfinvoke
  25. </cffunction
  26. </cfcomponent>

As you can see, we add a new test function with an error, the value expected is 5 instead 4


The result is different when you launch the test launch page in your browser

Tests : 1
Errors : 0
Failure : 1

It shows you that a test failed and it gives you the name of it

J'espère que cet article vous à donné une petite idée de ce qu'est le dev en TDD.

A bientôt pour un nouvel article
Commentaires
Salut l'ami,

J'ai aussi fait un peu de TDD avec CFUnit, si tu veux on peut en reparler :tongue:

T'as mon adresse e-mail

MuRDoCK
04/07/2008 @ 15:31:14: MuRDoCK: le TDD avec CFUnit
Poster un commentaire
Utilisateur
Mot de passe
 
Informaticien.be - © 2002-2024 AkretioSPRL  - Generated via Kelare
The Akretio Network: Akretio - Freedelity - KelCommerce - Votre publicité sur informaticien.be ?