ERP5 KM

HowToUnitTests

Running existing tests

To run one test, you must run the script ERP5Type/tests/runUnitTest.py with as parameter the name of the files containing test definitions. The test runner will recreate an instance home in ../../../unit_tests relative to the runUnitTest file itself.

Example :

  ./ERP5Type/tests/runUnitTest.py testCMFActivity testOrder

Test runner is evolving, for an overview of all options, run:

  ./ERP5Type/tests/runUnitTest.py --help

Tips and tricks

Make sure that all the business templates created are in unit_tests/bt5 directory; otherwise, they will not get installed during runtime.

How to simulate file upload

If you application uses file upload feature, you can do the same in the test by creating a file-like object and supplying it as the appropriate argument to the script or method. Things you should take into account are:

  • normally, the uploaded file comes as a ZPublisher.HTTPRequest.FileUpload object which has some properties missing in a stock Python "file" object (like .filename)

  • access to unprotected subobject in Zope is disabled by default, so you have to enable them

This class works fine for most purposes:

   1 class FileUploadTest(file):
   2 
   3   __allow_access_to_unprotected_subobjects__=1
   4 
   5   def __init__(self, path, name):
   6     self.filename = name
   7     file.__init__(self, path)

Note that you should use Products.ERP5Type.tests.utils.FileUpload for this purpose.

How to simulate script-based customization

If the tested application provides space for script-based customisation (e.g. by delegating some tasks to scripts), we should test this too. We can do it by creating and setting appropriate scripts at runtime, within the unit test.

ERP5Type.tests.utils contains an utility function createZODBPythonScript to create Script (Python) objects programmatically and do all the necessary setup.

   1 from Products.ERP5Type.tests.utils import createZODBPythonScript
   2 
   3 script = createZODBPythonScript( portal.portal_skins.custom,
   4                         'Custom_script',
   5                         'scripts_params=None',
   6                         '# Script body\n'
   7                         'return "something"' )

Fast testing of changes in unit test

While writing unit tests you might not want to wait for installing all Business Templates and preparing site - you need to have as fast as possible ERP5 system setup. So first invoke

./ERP5Type/tests/runUnitTest.py --save testYours

to create initial site, then while testYours use:

./ERP5Type/tests/runUnitTest.py --load testYours

More information is available on runUnitTest.py help page.

Experimental stuffs

Profiler

ZopeTestCase supports collecting and printing profiling statistics. This is integrated in runUnitTests, using --profile command line switch. However, if you use python2.4 you will have assertion errors in profile.py module from standard python library (profile module is somehow obsolete). Commenting out those assert statement produce some results that are inaccurate, but it's usable if you just want a global idea.

Using --update_business_templates option

The --update_business_templates option for runUnitTests allows to load an existing site (created by --save for example), and install all business templates required by the unit test provided, plus base business templates (erp5_core, erp5_xhtml_style and a business template for the catalog). Upgrading products and business templates are the user's responsibility. It is also recommended to specify fixed portal id by --portal_id, then you can reuse an existing site for other unit test which requires the same set of business templates.

Examples:

Running with an updated version, which is lost once test is finished:

$ runUnitTests.py --portal_id=erp5 --save testCMFActivity
[creates initial site]
$ runUnitTests.py --portal_id=erp5 --load --update_business_templates testCMFActivity
[updates initial site and runs test]

Creating an updated version, saved for future use with --load:

$ runUnitTests.py --portal_id=erp5 --save testCMFActivity
[creates initial site]
$ runUnitTests.py --portal_id=erp5 --load --save --update_business_templates testCMFActivity
[updates initial site and saves the result]
$ runUnitTests.py --portal_id=erp5 --load testCMFActivity
[runs test]

Same example, using prepareUpdadeUnitTest.sh:

$ ls ~/update_testing/*
bar_revision:
bt5/  Products/
foo_revision:
bt5/  Products/
work:

$ cd ~/update_testing/work
$ prepareUpdateUnitTest.sh ~/update_testing/foo_revision \
                           ~/update_testing/bar_revision testCMFActivity
[creates initial site with Products and Business Temlates from foo_revision]
[updates initial site with Products and Business Temlates from bar_revision]
[saves result]
$ runUnitTests.py --portal_id=erp5 --load testCMFActivity
[runs test]

In addition, you can use --update_only option like following:

runUnitTests.py --portal_id=erp5 --load --save --update_business_templates --update_only="erp5_core,erp5_base" testCMFActivity

HowToUnitTests (last edited 2011-07-01 07:25:14 by Yusei)

Page
  • Immutable Page
  • Info
  • Attachments
User
Learn about new ERP5 releases,technical articles, events and more.

Subscribe to the monthly ERP5 Newsletter!