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
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
- Created by : Strodiot Pierre
- Date : 07/05/2008
- Comments :
- this is the cfc containing all the functions used for math
- --->
- <cfcomponent displayname="Math">
-
-
- <cffunction name="Sum" output="true" returntype="Numeric">
- <cfargument name="val1" type="Numeric" required="true">
- <cfargument name="val2" type="Numeric" required="true">
-
- <cfreturn #val1# + #val2#>
- </cffunction>
-
- </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
- Created by : Strodiot Pierre
- Date : 07/05/2008
- Comments :
- this is the cfc containing all tests on the functions used in cfc's math
- --->
- <cfcomponent displayname="MathTest" extends="net.sourceforge.cfunit.framework.TestCase">
-
- <cffunction name="TestSum" returntype="void" access="public">
- <cfset objMath = CreateObject("component","cfunitTest.cfc.Math")>
- <cfset result = objMath.Sum(1,3)>
- <cfinvoke method="assertEquals">
- <cfinvokeargument name="expected" value="4">
- <cfinvokeargument name="actual" value="#result#">
- </cfinvoke>
- </cffunction>
- </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
- <cfsilent>
- <cfset testClasses = ArrayNew(1)>
- <cfset ArrayAppend(testClasses, "cfunitTest.tests.MathTest")>
-
- <cfset suite = CreateObject("component", "net.sourceforge.cfunit.framework.TestSuite").init( testClasses )>
- </cfsilent>
- <cfoutput>
- <html>
- <head>
- <title>CFUnit Test</title>
- </head>
- <body
- <cfinvoke component="net.sourceforge.cfunit.framework.TestRunner" method="run">
- <cfinvokeargument name="test" value="#suite#">
- <cfinvokeargument name="name" value="">
- </cfinvoke>
- </body>
- </html>
- </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
- Created by : Strodiot Pierre
- Date : 07/05/2008
- Comments :
- this is the cfc containing all tests on the functions used in cfc's math
- --->
- <cfcomponent displayname="MathTest" extends="net.sourceforge.cfunit.framework.TestCase">
-
- <cffunction name="TestSum" returntype="void" access="public">
- <cfset objMath = CreateObject("component","cfunitTest.cfc.Math")>
- <cfset result = objMath.Sum(1,3)>
- <cfinvoke method="assertEquals">
- <cfinvokeargument name="expected" value="4">
- <cfinvokeargument name="actual" value="#result#">
- </cfinvoke>
- </cffunction>
-
- <cffunction name="TestSum2" returntype="void" access="public">
- <cfset objMath = CreateObject("component","cfunitTest.cfc.Math")>
- <cfset result = objMath.Sum(1,3)>
- <cfinvoke method="assertEquals">
- <cfinvokeargument name="expected" value="5">
- <cfinvokeargument name="actual" value="#result#">
- </cfinvoke>
- </cffunction>
- </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