Download to get rid of those pesky bugs.             MXUnit Unit Testing framework for ColdFusion developers                               Download       Details       Home       Support       Blog

Scheduling Tests I

So you're writing unit tests, maybe even dabbling in "real" test-driven development (i.e. writing tests before code, letting tests drive design, etc). And you have these tests sitting there, and you run them as you're developing, and angels sing and heralds trumpet. And you think to yourself, "you know, i should run these tests often". And that's good. And then you think "you know, i should let the computer run these tests for me and tell me what's what". And that's better.

Particularly when you're just starting out with unit testing, your creative energy is primarily directed at the test writing itself. It's new, and that's mentally exhausting. Then some test guru tells you that you need a continuous integration server and that if you don't have one you might as well pack your crap up and go program in php 'cause you's a n00b and we don't want you here.

Ignore that talk. Getting your tests running in a scheduled manner should take about 5 minutes to set up. I guarantee it will take you far longer to read this document than it will to automate your tests. So let's get started with some really simple test automation. Note... eventually, as your entire team gets test-infected, you're going to want more. You'll want metrics and logging and test history and reports and all that stuff. You'll want your most recent test runs on your intranet wiki front page. You'll hear "What bastard broke the app!?" lobbed over the cubes. For now, though, let's keep it simple.

Step 1: Create directory for storing your tests

My app was at c:\inetpub\wwwroot\AppCFCSite1, and I created a directory named "tests" under that. In "tests", I created a directory named "TestRuns"

Step 2: Create a file to run all your tests

Create a new file in the top level of your tests directory and call it "run.cfm"

Step 3a: Populate the run.cfm file

On your file system, go to your mxunit install directory (e.g., c:\inetpub\wwwroot\mxunit), go to "samples", and open ScheduledRun.cfm. Copy the contents.

Then, paste the contents into your new run.cfm file

Step 3b: add the ComponentPath to your DirectoryTestSuite

You don't need to do this, but it significantly improves performance of the DirectoryTestSuite. So do it.

In my example, I changed

<cfinvoke component="#DTS#" 
        method="run"
        directory="#dir#" 
        recurse="true" 
        excludes="#excludes#"
        returnvariable="Results"
        componentpath="????.?????.?????"><!---  <-- Fill this in!   --->

to this

<cfinvoke component="#DTS#" 
        method="run"
        directory="#dir#" 
        recurse="true" 
        excludes="#excludes#"
        returnvariable="Results"
        componentpath="AppCFCSite1.test"><!---  <-- Fill this in!   --->

Step 4: Run your new file in the browser

If you don't have any tests in your test directory (or its subdirectories), this will show 0 results

Step 6: Set up a scheduled Task

Now that your run file is set up, let's automate the running of it. Open CF Administrator, and go to "Scheduled Tasks". Click the "Schedule New Task" button

Name the task. Select the "daily every" radio, and tell it when you want to run. This sample has it running every 2 hours starting at 6 AM and ending at 5 PM

Add the URL to your run file into the "URL" box

Click the checkbox beside "save output to file".

In the "File" box, add a path to a file where your output will be stored. For my sample app, I entered c:\inetpub\wwwroot\AppCFCSite1\test\testruns\MostRecent.html

Click submit

Step 7: Run the scheduled task manually

In the scheduled tasks list, click the green button beside your new scheduled task. This will run it. This will put a new file at the location you specified.

Step 8: View the file that was created

On your file system (or in your IDE), find the new file that was created. Open it in the browser. You should see virtually the same thing you saw when you ran the file manually in the browser

Step 9: Create a new test component (skip if you already have tests)

in the mxunit install location, in the "samples" directory, open "TestCaseSkeleton.cfc". Copy the contents.

In your "tests" directory, create a new test component if you don't have any. Name it something like "MyNewTest.cfc". This is a throwaway, only for use in demonstrating the automation. Paste your contents from TestCaseSkeleton into your new component. Save it.

Step 10: Run your tests in the browser again

Re-run your new run.cfm file. You'll now see your new test component, which will contain a failure.

Step 11: Run your scheduled task again

Back in your CF Admin, manually run the scheduled task again. This will overwrite the existing "MostRecent.html" file

Step 12: View the file that was created

Open that MostRecent.html file and confirm that it now contains your new test

Step 13: Adding email to your scheduled task

Back in CFAdmin, open up that scheduled task. in the "URL" box, add "&email=true" to the end of the URL.

Step 14: set up your email addresses

The run.cfm file is, by default, purposely simple with its handling of email:

  1. At the top of the file, change the default for url.recipients to whatever you want. Note that if you want to manage the recipients in the scheduled task itself, just add "&[email protected],[email protected]" etc to the end of the URL in the "URL" box in the scheduled task.
  2. At the bottom fo the file, change [email protected]???.com to an appropriate "from" email address

 

Step 15: Run it manually one more time

Now that the email is set up, run your task manually one more time in CF Admin. Assuming your mail server and all that is set up correctly, you should get an email momentarily that contains your test results.

Voila... simple test automation.


2008 MXUnit.org