Test Driven Development using Test.AnotherWay and mxUnit
Time and time again, I have debugging bugs and redeveloping code which some I wrote but mostly done by others as well. Experience is still my best teacher in web development and I believe it would be a heavier burden later on if we continuously pursue rapid development without any testing at all.
You may have heard of writing test cases before diving into code which is well known as Test Driven Development. This methodology has been widely adapted in programming (Java thru JUnit, etc). Following Kent Beck’s Test Driven Development (TDD) we will follow this simple method:
- Write the test first
- Watch the test fail
- Write the component
- Watch the test pass
A. Test.AnotherWay. With the emerging trend in web development including javascript, I need a similar aproach which makes development consistent and debugging easier for the client/front-end. Since I’ve been doing mostly web mapping, I have adapted a testing methodology from OpenLayers which is practical but effective.
-
Download Test.AnotherWay-0.51.tar.gz
-
Let’s say my application is called myproject. Create a test folder under myproject.
-
Copy run-tests.html, list-tests.html into myproject/test. Don’t be surprised if you can’t find list-tests.html in Test.AnotherWay.
-
Writing the tests. To add a test, you should add it on list-tests.html. Test files should be named beginning with test_. For example, *test_index.html
*
function test_Map(t) { t.plan(3); //this should go to the Application\\'s Global Settings var baselayer = CS.globals.baselayer; //this should go to the Application\\'s Global Settings var options = CS.globals.options; map = new CS.Map("div-map", baselayer, options); // default options was set t.eq(map.getMap().CLASS_NAME, "OpenLayers.Map", "map object created successful"); layerPlaces = new CS.VectorLayer("Places Layer", options); t.eq(layerPlaces.getLayer().CLASS_NAME, "OpenLayers.Layer.Vector", "vector layer created successfully"); map.addVectorLayer(layerPlaces); selectControl = layerPlaces.getSelectControl(); t.eq(selectControl.CLASS_NAME, "OpenLayers.Control.SelectFeature", "selectFeatureControl added successfully"); } |
B. For the server side, particularly ColdFusion, I have been using mxunit.org upon following Sean Corfield’s blog post regarding ColdFusion Unit Testing.
-
Download from
-
Extract to your webroot
-
Write your testcase as a cfc.
var metaName = "gao li wu"; var search = createObject("component","platform.svc.search.GeoSearch"); search.setDSN('beijing_app'); search.setMetaName(metaName); actual = search.getMetaName(); assertEquals(actual,metaName); |
Notes:
-
You can show the cfdump on the test case iff the test-case passed. If your test-case fails, then you need to comment the assertion first so you could see the cfdump. Inserting cfdump (commented or not) is a convenient way to debug your CFM.
-
You can chain test cases by calling the related functions. This is very useful when you are doing a short-circuit test.