← Back to team overview

dolfin team mailing list archive

Re: Writing unit tests

 

On Mon, Aug 07, 2006 at 08:52:30PM +0200, Anders Logg wrote:
> I have added a simple example of some unit tests to src/test/.
> We should populate this directory with subdirectories for each module
> (like in src/demo, src/bench, src/kernel) with unit tests for the
> different modules. Place test.py for module foo in src/test/foo/.
> 
> Here's a simple example:
> 
> """Unit test for the mesh library"""
> 
> import unittest
> from dolfin import *
> 
> class SimpleShapes(unittest.TestCase):
> 
>     def testUnitSquare(self):
>         """Create mesh of unit square"""
>         mesh = NewUnitSquare(5, 7)
>         self.assertEqual(mesh.numVertices(), 48)
>         self.assertEqual(mesh.numCells(), 70)
> 
>     def testUnitCube(self):
>         """Create mesh of unit cube"""
>         mesh = NewUnitCube(5, 7, 9)
>         self.assertEqual(mesh.numVertices(), 480)
>         self.assertEqual(mesh.numCells(), 1890)
> 
> if __name__ == "__main__":
>     unittest.main()
> 
> It's very simple:
> 
> 1. Create a sub class of unittest.TestCase for each sub group of unit
> tests for the module in question (only one sub group here).
> 
> 2. Create a function named testFoo for each unit test.
> 
> 3. Call self.assertEqual (or one of the other available tests) for
> each unit test.
> 
> 4. Add the two lines at the bottom of the file.
> 
> This only works for PyDOLFIN, but maybe that's enough.
> 
> It's a good practice to write the tests before writing the code. That
> way, we won't forget to write them.
> 
> /Anders

This is an excellent practice, to write examples with expected
results. It also servers as an implicit documentation of the code,
which is very good since we don't normally write extensive
documentation for every function.

PyDOLFIN mirrors the C++ interface except for a few exceptions, so
performing the unit tests in Python should be sufficient.

  Johan


Follow ups

References