Skip to content

Rhaptos Software Development

Personal tools
You are here: Home » Developer Blog » Ed's Blog » Unit Testing Proposal

Unit Testing Proposal Unit Testing Proposal

Document Actions
Submitted by ew2. on 2008-06-26 11:18. Development
As we move toward having more outside development, automated unit tests could help keep our code in top shape.

Unit Testing


As we move toward having more outside development, automated unit tests could help keep our code in top shape. 

What is a unit test?

  1. It is a test of a single component within an application
  2. Tests the "happy path" through a class or component
  3. It is usually implemented using a test framework which helps make them easy to implement
  4. The use of a framework makes them easy to automate and makes them repeatable
  5. Once it’s written, it lives as part of the code base.
  6. Any developer can run it.

Why should we unit test?

  1. Unit tests encourage more concise and compact code.  Your code must be testable. 
  2. Unit tests give inexperienced developers a way to verify that their changes did not break the current code
  3. Provide an easy way to verify refactoring which is essential to keeping a code base alive.
  4. They help find defects earlier when they are cheaper and easier to fix
  5. Running an automated build system, such as Bitten, on our development code can provide a snapshot of the quality of our development efforts

Arguments against unit tests

  1. They are time consuming to create - they do take time to create, but the trade off of easy testing in the future makes it worth the effort
  2. They don't really prove the code will work - this is correct,.  They only provide a test on a certain path of functionality, but the testing is happening at a low level so any defects found are easier to fix
  3. I know what I am doing.  I don't need a unit test - If you think this way, then you need to look at the fact that the unit test is for those that work on the code after you.  They might not be as knowledgeable or experienced.

Unit test guidelines

There are many schools of thought on unit testing.  These are some guidelines I have found helpful in the past.

  • Do not try to test every method
    • Test the likely path of the code (public methods)
  • Keep dependencies to a minimum
    • If there are dependencies, provide setup code as part of the test
    • Use mock objects where applicable
  • Write code with test in mind or write test first
    • I think this is key.  It causes you to think and plan before or while writing code.

Problems with unit tests in Plone/Zope

  1. How to test py files used in templates? I don't think we can
  2. How to test functions outside of classes? Some are called by classes, so that could test them.  Some are called in several places under different contexts which might be a problem.
  3. UI and classes are tightly coupled.  Does this limit what can be unit tested?
  • RhaptosCollection
    • Collection
      • Archetypes
        • ArchetypeTestCase
        • Schema created outside of function or class
      • Printing
        • PyUnit test
      • Extended classes: CollectionBase and CollaborationManager
    • ContentPointer, PublishedContentPointer, AsyncPrint
      • How to test?


Re: Unit Testing Proposal

Posted by bnwest at 2008-06-26 11:54
The main problem I have seen with unit tests is that it is time consuming to _maintain_ them. Management must commit resources to keep the unit test in proper working order or we will end thousands of lines of non-working unit tests. Committing resources to write and maintain unit tests will extend the development cycle.

BTW I am a big fan of unit tests. In Extreme Programming, one would write unit tests for every development cycle iteration, with the unit test being the success criteria for cycle completion. In action, this usually means that unit tests with the corner cases will force the developer to write a more complete implementation for the get-go, which should shorten the development cycle.

Re: Re: Unit Testing Proposal

Posted by ew2 at 2008-06-30 08:15
I agree that there needs to be time allowed for unit tests to be created and maintained. It will be one of the factors of our scheduling if we decide this is something we want to do.

Unit Testing XSLT

Posted by cbearden at 2008-06-30 13:48
I haven't looked too much into unit testing for XSLT, but here are a few resources. I haven't had a chance to examine them in detail or to try them out.

Jeni Tennison (acknowledged XSLT expert) created unit-testing.zip, a Java package designed to work with Saxon >= 8.4b for unit testing stylesheets (http://www.jenitennison.com/xslt/utilities/unit-testing/);

Eric van der Vlist (acknowledged XML expert) created XSLTUnit, an XSLT-based system for XSLT unit tests (http://xsltunit.org/);

Jez Higgins gave a talk at a programmer's conference in 2005 on using XSLTUnit (http://www.jezuk.co.uk/accu2005/xslttesting/slide-1.html).