Archive for the 'Unit Testing' Category

Running doctests from TextMate for Google App Engine modules

Developer emptor: I just lost a couple of hours to this: make sure you disable the Google App Engine doctest import in your apps when you're done testing a module lest you encounter _weird_ errors. I started having the login URL returned by users.create_login_url() being returned incorrectly when I forgot to remove the doctest import. It started forwarding to https://www.google.com/accounts/Login?continue=. Check out my forum post on it here.

I love Python's doctests. Basically, you test out your functions in the interactive shell and copy the results into the comments for a function. That's it! So simple.

Example:

def http_request(self, url, data, method=urlfetch.POST):
	"""
	Makes an API call to Triggermail and returns the response.
 
	>>> client = TriggerMail()
	>>> client.http_request('email', {'email':'EMAIL_REMOVED'}, urlfetch.GET)
	{u'blacklist': u'0', u'templates': {u'test2': 0}, u'verified': u'0', u'vars': {u'first_name': u'Aral', u'last_name': u'Balkan'}, u'optout': u'0'}
	"""

To run the doctests, you just need a main method in your module that looks like this:

if __name__ == "__main__":
	import doctest
	doctest.testmod()

And, if you're working with TextMate, you can run the current script and its doctests by pressing ⌘ R.

Sweet!

However, when working with Google App Engine, this doesn't work out of the box.

If you try it, you'll get an error similar to the following:

ImportError: No module named google.appengine.api

This is because the local GAE environment isn't set up properly. The same goes when trying to test your apps from the Python interactive shell.

(If you're using Django for your app, you're in luck, all you have to do is ./manage.py shell and you're up and running with an interactive shell that's configured for your GAE project.)

Thankfully, Duncan over at the GAE forums went to the trouble of finding out exactly which imports are necessary to get you up and running.

His code listing actually goes beyond setting up the environment to finding your modules and running the tests. For my purposes, I just want to be able to hit ⌘ R in TextMate and run the tests for my current module while developing it, so I took the top bit of his code and put it into a module called gae_doctests.py.

It looks like this:

# To enable doctests to run from TextMate, import this module
# (Use only when testing, then comment out.)
# From: http://groups.google.com/group/google-appengine/browse_thread/thread/fa81f6abd95aa8b9/efed988b302aafb4?lnk=gst&q=duncan+doctests#efed988b302aafb4
 
import sys
import os
sys.path = sys.path + ['/usr/local/google_appengine', '/usr/local/google_appengine/lib/django', '/usr/local/google_appengine/lib/webob', '/usr/local/google_appengine/lib/yaml/lib', '/usr/local/google_appengine/google/appengine','/Users/aral/singularity/']
 
from google.appengine.api import apiproxy_stub_map
from google.appengine.api import datastore_file_stub
from google.appengine.api import mail_stub
from google.appengine.api import urlfetch_stub
from google.appengine.api import user_service_stub
 
APP_ID = u'test_app'
AUTH_DOMAIN = 'gmail.com'
LOGGED_IN_USER = 't... {at} example(.)com'  # set to '' for no logged in user
 
# Start with a fresh api proxy.
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
 
# Use a fresh stub datastore.
stub = datastore_file_stub.DatastoreFileStub(APP_ID, '/dev/null', '/dev/null')
apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)
 
# Use a fresh stub UserService.
apiproxy_stub_map.apiproxy.RegisterStub('user',
user_service_stub.UserServiceStub())
os.environ['AUTH_DOMAIN'] = AUTH_DOMAIN
os.environ['USER_EMAIL'] = LOGGED_IN_USER
 
# Use a fresh urlfetch stub.
apiproxy_stub_map.apiproxy.RegisterStub(
    'urlfetch', urlfetch_stub.URLFetchServiceStub())
 
# Use a fresh mail stub.
apiproxy_stub_map.apiproxy.RegisterStub(
  'mail', mail_stub.MailServiceStub())
 

(Either replace the /usr/local/ bit with the actual path to your GAE install or use Duncans code which is neater -- I was lazy and copied the contents of the sys.path list from the Django interactive shell.)

To use it, simply:

import gae_doctests

And hit ⌘ R in TextMate. Sweet!

Once I'm done hacking away on a module, I simply comment out the import.

Could not embed video.

AsProject: rails-like toolset for ActionScript

AsProject Video Demo Screenshot

Luke Bayes and Ali Mills of asunit fame just released AsProject, an open source ActionScript development toolset that streamlines the open source development process by giving you a rails-like experience.

Check out the AsProject demo video.

This is very cool. I must set aside time to see how Arp can be integrated into this.

Flash bytecode and PHP unit testing tools

A few tools I’m finding useful at the moment:

For working with SWF bytecode:

  • Flasm - disassembler/assembler.
  • swfdump - part of swftools; gives a really good assembly/hex listing of SWF contents with -Ddu flags.
  • 0xED - a free, simple hex editor for OS X.

For PHP:

For swfdump, there is a Linux version that compiles well on OS X. Just ./configure, make, and then sudo make install. Or, you can download the binaries I made from below (only tested on the latest Tiger; no warranties/guarantees/etc.)

Download Swftools OS X binaries (version 1.0.1beta; current stable release). MD5 hash: 75f904b9bc5133fc0e35fb521383a45d.

AsUnit-X gets an icon!

AsUnit-X updated with new icon I received an unexpected email from Jason Bednarik today with a lovely new icon for AsUnit-X attached to it. Jason apparently created his own application bundle for asunit previously and had an icon he made for it which he has generously let me use for AsUnit-X.

Thanks, Jason!

Download the updated AsUnit-X with new icon
(DMG file, OS X; 11MB).

FOTB slides: Memo to the CEO

I've just put my slides up from my Memo to the CEO session at Flash on the Beach. Download them here (PDF, 3MB).

I thought about exporting QuickTime and Flash versions of the presentation too but, frankly, I couldn't justify it. The QuickTime version has all the bells and whistles of the transitions but it staggeringly large. The Flash version, well, let's just say I'm sure that Apple didn't cripple the Flash export on purpose but in terms of size (it's large) and performance (it doesn't have all the effects), it really doesn't make much sense. Again, I'm sure it's not a case of Apple favoring QuickTime over Flash! At the end of the day, the PDF version seems to be the best bet. It contains the information and is small. There you go, you just read a whole paragraph on why you've got my session slides in PDF format. Truly, could there have been a better way for you to spend the last thirty seconds. Thirty one. Thirty two! :P

While searching around for a picture to include in this post, I also found some reviews of my session. Here are some links:

AsUnit-X: AsUnit XUL UI for OS X

AsUnit-X: AsUnit XUL UI for OS X

The XUL UI for the excellent AsUnit by Luke Bayes and Ali Mills has been missing an installer/application bundle for OS X for a while now so, this morning, I decided to create one. After all, now that I have a Mac, how could I pass up the chance to contribute both to the Mac community and to one of my favorite open source Flash projects at the same time?

If you're in a hurry, here's the download link:

Download AsUnit-X (10.9MB), the AsUnit XUL UI for OS X (see what I did there with the name? Clever, I am, I tell you!) :P

It's a standard Mac application bundle. To install it, just mount the DMG and drag the application icon to the Applications folder icon. In other words, just install it like any other Mac app.

Here are a quick series of steps for setting up a new project using AsUnit-X:

  1. Launch AsUnit-X
  2. Click the + icon next to Projects to create a new project using the Wizard
  3. Enter a name for the project. It doesn't matter what you enter here, it's a description solely for your benefit.
  4. Under templates, choose the version of ActionScript that you want. For ActionScript 2, there are two options. I selected the one for Flash 8, which uses the AsUnit 2.5 framework. Based on which version you select, make sure you include the correct framework in the classpath of your Flash movie later on.
  5. Select the source folder where you want AsUnit-X to create your class files.
  6. Select the folder that you want AsUnit-X to use to create your test cases. For simplicity, you can choose the same folder.
  7. Next, you need to tell AsUnit-X where the default Flash classes reside. Click the Add button to add the class path (don't be confused by the text at the top of the screen. I spent a few moments trying to paste the class path in there by mistake.) For a default Mac installation, the default Flash framework classes are found in Users/{your user name}/Library/Application Support/Macromedia/Flash 8/en/Configuration/Classes
  8. Congratulations, you've completed the wizard and set up your project! (You don't have to use the wizard to enter this information, you can use the main UI. You can also alter this information at any time from the main UI.)

And some more steps for creating a class file and unit tests for it:

  1. Enter the name of a class in the Class Name text box (you can enter a fully-qualified class name that contains package information and AsUnit-X will create the package structure for you).
  2. Select the access modifier for the class constructor (or make it a Singleton) using the Constructor radio buttons.
  3. (Optional) Select a superclass at this point by either typing in the name of a class or select a class from the classpath by clicking the Choose button.
  4. (Optional) Add any interfaces that you want your class to implement by adding them to the Interfaces list box by using the Add button.
  5. If you want AsUnit-X to automatically create a test case for your class, check the Create Test Case checkbox. You can choose to make the test case a standard one (synchronous), a Mock Object or an asynchronous test (e.g., for testing classes that rely on externally loaded data).
  6. If you want AsUnit-X to automatically create test suites for your class, check the Generate Test Suites checkbox.
  7. Finally, click the Create button to create your class and your test case. Click the Run Now link next to Generate Test Suites to generate your test suite.

And, finally, some steps for actually running the tests. To run the tests, you will need to create an FLA that is known as the Test Runner. To do this:

  1. Open up Flash and create a new FLA. Call it MyTestRunner or something equally exciting.
  2. On Frame 1 of the FLA, add the following code: MyTestRunner.main();
  3. Before you do anything else, add the AsUnit framework to your classpath. To do this, open up Flash's preferences (Flash Professional → Preferences...), select ActionScript from the Category list box and click the ActionScript 2.0 Settings button in the Language field. Use the + button to add a path to your classpath. The AsUnit framework is included in the AsUnit-X application bundle so you can point to it from there. For a default installation, the path should be: {Your Computer's Name}:Applications:AsUnit-X.app:Contents:Frameworks:asunit:as25. Change the last folder to the version of the framework you want (or, even better, add all three -- you'll only need to do this once and the settings will work with all of your projects.)
  4. Next, actually create the MyTestRunner class. Here's how a simple one looks:
    [as]import asunit.textui.TestRunner;

    class MyTestRunner extends TestRunner
    {
    public function MyTestRunner()
    {
    start(AllTests);
    }

    public static function main():Void
    {
    var runner:MyTestRunner = new MyTestRunner();
    }
    }[/as]
    I'm not a big fan of using a static main() method in Flash but, in this case, it makes sense so that your test suites can be run by both the Flash IDE (Macromedia compiler) and MTASC.

  5. Test your MyTestRunner FLA and you should see the default AsUnit interface appear with the familiar red bar that tells you that your test case (with an automatically generated failing test) was added to your test suite correctly.

I realize that these are quick notes but I thought I'd jot them down here as I had a lot of difficulty trying to find documentation or tutorials for AsUnit, especially the 2.5 branch. I will try and create a more illustrated tutorial if I get time, with screenshots and whatnot.

If you're interested, here are the steps I followed to create the bundle:

  1. Install the Mac version of the XULRunner framework.
  2. Downloaded the OS X build of the AsUnit XUL UI from asunit.org.
  3. Create an application bundle (a folder called AsUnit-X.app) in Finder and populate it according to the excellent instructions on the Deploying XULRunner 1.8 page at Mozilla and Ricardo Pacheco's XULRunner on OS X blog post. The exact rsync command to run is rsync -rl /Library/Frameworks/XUL.framework/ /path/to/your/bundle/AsUnit-X.app/Contents/Frameworks/XUL.framework/ (the Mozilla docs are a bit confusing on which folder you need to copy.) Make sure you create the Info.plist file based on the one in Ricardo's blog post. If all goes well, you should a running Mac app at this point.
  4. Add the framework. I added this into a folder called asunit in the Frameworks folder in the bundle.
  5. Finally, package it into a DMG. I found these instructions for creating a DMG by Remko Tronçon to be very useful and owe a debt of gratitude to Adium, from which I ripped the background graphic and symlink for the Applications folder. AsUnit-X doesn't have its own icon yet but that may change in the near future (OS X-style icon donations are welcome!)

    While creating the DMG, I came across an issue that caused me some delay (which was completely unnecessary!) After converting the disk image to a compressed image using the Disk Utility, Finder would no longer open the mounted image in icon view (on my system, it defaulted to column view). I saw that others were having this problem also and initially attempted to address it by using the pkg-dmg perl script from Mozilla. Unfortunately, that didn't like the symlink to Applications and tried to copy the actual Applications folder over and failed. So, I decided to follow the instructions on this thread but that didn't work either (although I am digging the power of the hdiutil command.) Finally, I decided to see if throwing money at the problem would work and tried out Drop DMG and FireStorm, neither of which worked. It was only at this point that I realized that there wasn't anything wrong with the image files I was creating but that I had had "Open new windows in column view" checked in my Finder preferences. Doh! Unchecking this showed that the image files were displaying correctly after all!

I hope this makes AsUnit easier to access for developers on the Mac platform and that you enjoy using AsUnit-X.

Download AsUnit-X (10.9MB)

Flash on the Beach: BYOC (Bring Your Own CEO)

Isn't it crazy how time flies? It seems only yesterday that we were sitting around in a comedy bar with John and Pete talking about Flash on the Beach. Well, lots of sweat and tears later, John's pulled it off and we're about to have our first international Flash conference in Brighton. Flash on the Beach is next week!

I'm going to be presenting a session called Memo to the CEO. This is the session I've wanted to present for the longest time but somehow I kept finding myself presenting on some technical aspect of Flash development or other (AS2, AS3, Flex 2, etc.) Don't get me wrong -- I love developing and I love Flash and Flex but there's much more to creating web applications than typing code till it's done. Much more, in fact.

In my session, I talk about the high-level decisions that development houses can take to make the development process a fun and relaxed experience. Topics covered include User-Centered Development, usability design, user interface design patterns and usability testing, agile development, eXtreme Programming (XP), and software design patterns and application architecture.

Anyone who has anything to do with development will benefit from the session but the person who will get the most out of it -- and the person who can actually implement these processes, by allocating a budget for them -- is the CEO. So, people, grab your CEOs and come down to Flash on Beach!

If your CEO resists, give her my memo:

From: Aral Balkan
To: CEO, Web App Construction CO. (WACCO)

Houston we have a problem: According to recent studies, 50-70% of all IT projects fail. Development teams toil under unrealistic deadlines and implicit expectations for usability and accessibility that are impossible to satisfy. Many of us are stressed out and unhappy on a daily basis. And it doesn't have to be this way.

If you want happy developers and projects that succeed, there are three simple things you can ask them to do:

1. Use an agile development methodology such as eXtreme Programming (XP) and work in iterations.

2. Use a user-centered development process. Your teams must capture quantifiable usability requirements and you must budget to cover usability testing in every iteration of development.

3. Use software design patterns in the architecture of your applications to provide a common high-level language for your developers and take advantage of time-tested solutions to common problems.

All the best,
Aral

Slides: Supercharging Flash 8

Here are my slides from the Supercharging Flash 8 session I presented at FlashForward Austin 2006.

  • Flash format (coming soon) - 4.9MB (doesn't include the fancy 3D transitions)
  • PDF format - 6.4MB (just the slides)
  • Quicktime format - 34.1MB (the whole enchilada; all effects, etc., but heavy)
  • Keynote - 10.2MB (source, .zip)

The main extensions I talked about during the session were:

While preparing my talk, I noticed something weird with ASUnit, the MXP version, where it tried to write its AllTests.as files (the test suites) inside my various applications. ASUnit's commands write AllTests.as files in each folder of your project's package structure (something I don't normally enjoy) when you choose to add a new test to your test suite or to regenerate your test suites. On my machine at least (MacBook Pro), there appears to be a bug with this where the commands start writing inside the applications in my Applications folder (Applications on OS X are just bundles, like Zip files.)

The workaround is not to use the ASUnit commands from within the Flash IDE. Instead, use gProject to automatically create a unit test for you when you create a new class and add the unit test to your test suite manually.

The presentation is released under the Creative Commons Attribution 2.0 UK: England & Wales license except for two slides that use Flickr pictures released under the Creative Commons Attribution-NoDerivs 2.0 license. I've got a link to the Keynote source, below, so you can use specific slides if you want to in your own presentations (with attribution, of course, as per the terms of the Creative Commons license.)

And don't forget, you can find more free toys for you to play with on OSFlash!






Bad Behavior has blocked 0 access attempts in the last 7 days.