Foundations/Webservice/TestingApiApplications

Not logged in - Log In / Register

Revision 8 as of 2010-10-08 19:19:34

Clear message

This page provides a guide to writing tests for Launchpad API clients.

Techniques for Testing API Clients

Point your application at the staging.launchpad.net web service

By writing data to the staging.launchpad.net web service you can observe how the application will behave against a copy of real, live data. You can also observe the application going over the network.

The data on staging.launchpad.net is copied from the production database every night. Copying overwrites any changes made to the staging data from the previous day.

Pros

Cons

An Example

You will want to provide an easy way to switch the service for developers and for operators, too. Command-line switches are one standard Unix way to accomplish this:

   1 import sys
   2 from launchpadlib.launchpad import Launchpad
   3 from optparse import OptionParser
   4 
   5 client_name = 'just testing'
   6 
   7 
   8 def main(argv):
   9   parser = OptionParser()
  10   parser.add_option('-S', '--service', default='production',
  11                     help="Set the Launchpad web service instance this script will "
  12                          "read and write to [default: %default]")
  13 
  14   options, args = parse_args(argv)
  15   
  16   print "Connecting to the Launchpad API {0} service.".format(options.service)
  17   launchpad = Launchpad.login_anonymously(client_name, options.service)
  18 
  19 
  20 if __name__ == '__main__':
  21   main(sys.argv)

An example usage:

$ myscript --service=staging
Connecting to the Launchpad API staging service.

Notes

The staging database always runs code that is released or soon-to-be released on the Launchpad production servers. The API version will be equal to or greater than the production API version.

The staging server goes down for approximately two hours on the weekend for automated database updates.


Give your application read-only access to the server

By giving your application read-only access to the server you can allow developers and users to test your application against live data without fear of it destroying data. By only reading data it also becomes possible to run the tests multiple times in a row.

Pros

Cons

An Example

A convenient trick to enforce read-only access for your script is to log in to the API anonymously. Anonymous users are not allowed to write data to Launchpad. If you do this, any point in your code that tries to write to the web service will raise an error: you can then modify the code to handle the read-only mode gracefully.

As before, we will use a command-line switch to make the functionality accessible to both developers and operators.

   1 import sys
   2 from launchpadlib.launchpad import Launchpad
   3 from optparse import OptionParser
   4 
   5 client_name = 'just testing'
   6 service = 'production'
   7 
   8 
   9 def main(argv):
  10   parser = OptionParser()
  11   parser.add_option('-d', '--dry-run', default=False, dest='readonly'
  12                     help="Only pretend to write data to the server.")
  13 
  14   options, args = parse_args(argv)
  15   
  16   print "Connecting to the Launchpad API {0} service.".format(service)
  17 
  18   if options.readonly:
  19     api_login = Launchpad.login_anonymously
  20   else:
  21     api_login = Launchpad.login_with
  22 
  23   launchpad = api_login(client_name, service)
  24 
  25   me = launchpad.people['my-user-name']
  26   me.display_name = 'A user who edits through the Launchpad web service.'
  27 
  28   # This would raise a ServerError if we are logged in anonymously
  29   # me.lp_save()
  30 
  31   # This is the correct way to do it:
  32   if not options.readonly:
  33     me.lp_save()
  34   else:
  35     print "Skipped saving display name: {0.display_name}".format(me)
  36 
  37 
  38 if __name__ == '__main__':
  39   main(sys.argv)

Notes


Have your application generate its own test data on the staging server

This is an advanced technique that builds upon 'test your application against staging'. You can have your application generate its own test data on the staging server, then run its tests against said data.

Pros

Cons

An Example

Notes