Unit testing in cerbere

This section is for cerbere developers, in particular when implementing a new mapper.

mapper testing

Cerbere unit testing is based on unittest python package. This section describes the procedure to test new or existing mappers.

test data

When providing a new mapper, test data files should be provided too. These data files can not be stored on cerbere git server and must be made available on some server.

As an example, the test data files for the mappers implemented by Ifremer are available at ftp://ftp.ifremer.fr/ifremer/cersat/projects/cerbere/test_data/

There should be one subfolder for each mapper module. For instance for the Sentinel-3 SLSTR mappers (cerberecontrib-s3 package, safeslfile module), the test data files are to be found at:

ftp://ftp.ifremer.fr/ifremer/cersat/projects/cerbere/test_data/safeslfile/

There should be at least one test file per class in the mapper module.

unit testing on mapper classes

An abtract test class is provided in cerbere.mapper.checker.Checker package.

To unit test a mapper class, you basically need to create a new test class, inheriting this class and unittest.TestCase class. A test class must be created for each class in your mapper module.

This class file must be in the tests repository cerbere or your contrib package.

For instance, for Sentinel-3 SLSTR SAFESLIRFIle mapper class for infra-red products (RBT and WCT products), a simple test class would look as follow:

"""

Test class for cerbere Sentinel-3 L1 mapper

:copyright: Copyright 2016 Ifremer / Cersat.
:license: Released under GPL v3 license, see :ref:`license`.

.. sectionauthor:: Jeff Piolle <jfpiolle@ifremer.fr>
.. codeauthor:: Jeff Piolle <jfpiolle@ifremer.fr>
"""
import unittest

from cerbere.mapper.checker import Checker


class S3L1RBTChecker(Checker, unittest.TestCase):
    """Test class for Sentinel-3 L1 RBT files"""

    def __init__(self, methodName="runTest"):
        super(S3L1RBTChecker, self).__init__(methodName)

    @classmethod
    def mapper(cls):
        """Return the mapper class name"""
        return 'safeslfile.SAFESLIRFile'

    @classmethod
    def datamodel(cls):
        """Return the related datamodel class name"""
        return 'Swath'

    @classmethod
    def test_file(cls):
        """Return the name of the test file for this test"""
        return "S3A_SL_1_RBT____20161015T155616_20161015T155916_20161015T175945_0179_010_011_4139_MAR_O_NR_002.SEN3"

    @classmethod
    def download_url(cls):
        """Return the URL of the data test repository where to get the test
        files
        """
        return "ftp://ftp.ifremer.fr/ifremer/cersat/projects/cerbere/test_data/"

Your test class must inherit from both the abstract class cerbere.mapper.checker.Checker and the generic test class unittest.TestCase:

  class S3L1RBTChecker(Checker, unittest.TestCase):

In the `:func:__init__` method of your test class, use the following code stub,
replacing ``S3L1RBTChecker`` with your test class name.
def __init__(self, methodName="runTest"):
    super(S3L1RBTChecker, self).__init__(methodName)

Then provide the main information needed to run the tests implemented in cerbere.mapper.checker.Checker class:

  • the mapper class name and associated datamodel class name to be passed to the parent constructor, returned by mapper() and datamodel() class methods

    @classmethod
    def mapper(cls):
        """Return the mapper class name"""
        return 'safeslfile.SAFESLIRFile'
    
    @classmethod
    def datamodel(cls):
        """Return the related datamodel class name"""
        return 'Swath'
    
  • which file to use for testing the class, returned by test_file() class method

    @classmethod
    def test_file(cls):
        """Return the name of the test file for this test"""
        return "S3A_SL_1_RBT____20161015T155616_20161015T155916_20161015T175945_0179_010_011_4139_MAR_O_NR_002.SEN3"
    
  • where to get this file, return by download_url() class method

    @classmethod
    def download_url(cls):
        """Return the URL of the data test repository where to get the test
        files
        """
        return "ftp://ftp.ifremer.fr/ifremer/cersat/projects/cerbere/test_data/"
    

testing example

In order for the test to run, the location of your test data must be known to the testing procedures. To ensure this, set the environment variable CERBERE_TESTDATA_REPOSITORY to your root repository for your test data.

Set the environment variable to locate your test data:

export CERBERE_TESTDATA_REPOSITORY=/repo/where/you/put/your/test/data/

To run the test suite, go into the tests folder where you wrote your test class and run:

nosetests -v your_class_test.py