Unit testing in cerbere

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

dataset testing

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

test data

When providing a new dataset, 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 dataset classes implemented by Ifremer are available at ftp://ftp.ifremer.fr/ifremer/cersat/projects/cerbere/test_data/

There should be one sub-folder for each dataset module. For instance for the Sentinel-3 SLSTR dataset classes (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 dataset module.

unit testing on dataset classes

An abtract test class is provided in cerbere.test.checker.Checker module.

To unit test a dataset 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 dataset module.

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

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

"""

Test class for cerbere Sentinel-3 L1 dataset

: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.test.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 dataset(cls):
        """Return the dataset class name"""
        return 'safeslfile.SAFESLIRFile'

    @classmethod
    def feature(cls):
        """Return the related feature 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.test.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.test.checker.Checker class:

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

    @classmethod
    def dataset(cls):
        """Return the dataset class name"""
        return 'safeslfile.SAFESLIRFile'
    
    @classmethod
    def feature(cls):
        """Return the related feature 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